From 044a174b2b6d2742dfb09f9f88051d16ff780d0a Mon Sep 17 00:00:00 2001 From: Hari Krishna Date: Sun, 11 Feb 2024 16:04:07 +0530 Subject: [PATCH 1/3] enhanced set_json_key function to be able to take multiple keys --- shuffle-tools/1.2.0/api.yaml | 16 +++++------ shuffle-tools/1.2.0/src/app.py | 50 ++++++++++++---------------------- 2 files changed, 26 insertions(+), 40 deletions(-) diff --git a/shuffle-tools/1.2.0/api.yaml b/shuffle-tools/1.2.0/api.yaml index cfa1a55b..918c9a02 100644 --- a/shuffle-tools/1.2.0/api.yaml +++ b/shuffle-tools/1.2.0/api.yaml @@ -822,28 +822,28 @@ actions: example: "{'key2': 'value2'}" schema: type: string - - name: set_json_key + - name: set_json_keys description: Adds a JSON key to an existing object parameters: - name: json_object description: The object to edit multiline: true - example: '{"sender": "me@test.com"}' + example: '{"user": {"name": "John", "age": 25, "city": "New York"}}' required: true schema: type: string - - name: key + - name: keys_string description: The object to add - multiline: false - example: "recipients" + multiline: true + example: "user.address.city , user.name" required: true schema: type: string - - name: value - description: The value to set it to in the JSON object + - name: values_string + description: The values to set it to in the JSON object multiline: true required: true - example: "colleague@test.com" + example: "value1 , value2" schema: type: string - name: delete_json_keys diff --git a/shuffle-tools/1.2.0/src/app.py b/shuffle-tools/1.2.0/src/app.py index 03019ed7..beac18d8 100644 --- a/shuffle-tools/1.2.0/src/app.py +++ b/shuffle-tools/1.2.0/src/app.py @@ -302,8 +302,8 @@ def get_length(self, item): return str(len(item)) - def set_json_key(self, json_object, key, value): - self.logger.info(f"OBJ: {json_object}\nKEY: {key}\nVAL: {value}") + def set_json_keys(self, json_object, keys_string, values_string): + self.logger.info(f"OBJ: {json_object}\nKEY: {keys_string}\nVAL: {values_string}") if isinstance(json_object, str): try: json_object = json.loads(json_object) @@ -322,42 +322,28 @@ def set_json_key(self, json_object, key, value): "reason": "Item is valid JSON, but can't handle lists. Use .#" } - #if not isinstance(json_object, object): - # return { - # "success": False, - # "reason": "Item is not valid JSON (2)" - # } + keys = keys_string.split(",") + values = values_string.split(",") - - if isinstance(value, str): - try: - value = json.loads(value) - except json.decoder.JSONDecodeError as e: - pass + for key, value in zip(keys, values): + key = key.strip() + value = value.strip() - # Handle JSON paths - if "." in key: - base_object = json.loads(json.dumps(json_object)) - #base_object.output.recipients.notificationEndpointIds = ... + key_list = key.split(".") + current = json_object - keys = key.split(".") - if len(keys) >= 1: - first_object = keys[0] + for subkey in key_list[:-1]: + current = current.setdefault(subkey, {}) - # This is awful :) - buildstring = "base_object" - for subkey in keys: - buildstring += f"[\"{subkey}\"]" + last_key = key_list[-1] - buildstring += f" = {value}" - self.logger.info("BUILD: %s" % buildstring) + if isinstance(value, str): + try: + value = json.loads(value) + except json.decoder.JSONDecodeError as e: + value = str(value) - #output = - exec(buildstring) - json_object = base_object - #json_object[first_object] = base_object - else: - json_object[key] = value + current[last_key] = value return json_object From 774ca1c4768d8232c935949bcbfbf5e70298e8f3 Mon Sep 17 00:00:00 2001 From: Hari Krishna Date: Sun, 11 Feb 2024 20:14:03 +0530 Subject: [PATCH 2/3] modified variable names --- shuffle-tools/1.2.0/api.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/shuffle-tools/1.2.0/api.yaml b/shuffle-tools/1.2.0/api.yaml index 918c9a02..12212eaf 100644 --- a/shuffle-tools/1.2.0/api.yaml +++ b/shuffle-tools/1.2.0/api.yaml @@ -832,14 +832,14 @@ actions: required: true schema: type: string - - name: keys_string + - name: keys description: The object to add multiline: true example: "user.address.city , user.name" required: true schema: type: string - - name: values_string + - name: values description: The values to set it to in the JSON object multiline: true required: true From fe376a4732abda78c94d98c96635252da2484faa Mon Sep 17 00:00:00 2001 From: Hari Krishna Date: Thu, 15 Feb 2024 18:48:25 +0530 Subject: [PATCH 3/3] added bounds checking --- shuffle-tools/1.2.0/src/app.py | 52 +++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/shuffle-tools/1.2.0/src/app.py b/shuffle-tools/1.2.0/src/app.py index beac18d8..9ec8ba68 100644 --- a/shuffle-tools/1.2.0/src/app.py +++ b/shuffle-tools/1.2.0/src/app.py @@ -302,8 +302,8 @@ def get_length(self, item): return str(len(item)) - def set_json_keys(self, json_object, keys_string, values_string): - self.logger.info(f"OBJ: {json_object}\nKEY: {keys_string}\nVAL: {values_string}") + def set_json_keys(self, json_object, keys, values): + self.logger.info(f"OBJ: {json_object}\nKEY: {keys}\nVAL: {values}") if isinstance(json_object, str): try: json_object = json.loads(json_object) @@ -322,8 +322,52 @@ def set_json_keys(self, json_object, keys_string, values_string): "reason": "Item is valid JSON, but can't handle lists. Use .#" } - keys = keys_string.split(",") - values = values_string.split(",") + #if not isinstance(json_object, object): + # return { + # "success": False, + # "reason": "Item is not valid JSON (2)" + # } + + # if isinstance(value, str): + # try: + # value = json.loads(value) + # except json.decoder.JSONDecodeError as e: + # pass + + # Handle JSON paths + # if "." in key: + # base_object = json.loads(json.dumps(json_object)) + # base_object.output.recipients.notificationEndpointIds = ... + + # keys = key.split(".") + # if len(keys) >= 1: + # first_object = keys[0] + + # This is awful :) + # buildstring = "base_object" + # for subkey in keys: + # buildstring += f"[\"{subkey}\"]" + + # buildstring += f" = {value}" + # self.logger.info("BUILD: %s" % buildstring) + + # output = + # exec(buildstring) + # json_object = base_object + # json_object[first_object] = base_object + # else: + # json_object[key] = value + + # return json_object + + if len(keys) != len(values): + return { + "success": False, + "reason": "Lengths of keys and values are different" + } + + keys = keys.split(",") + values = values.split(",") for key, value in zip(keys, values): key = key.strip()