Skip to content

Commit

Permalink
enhanced set_json_key function to be able to take multiple keys
Browse files Browse the repository at this point in the history
  • Loading branch information
satti-hari-krishna-reddy authored Feb 11, 2024
1 parent 2fb2483 commit 044a174
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 40 deletions.
16 changes: 8 additions & 8 deletions shuffle-tools/1.2.0/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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": "[email protected]"}'
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: "[email protected]"
example: "value1 , value2"
schema:
type: string
- name: delete_json_keys
Expand Down
50 changes: 18 additions & 32 deletions shuffle-tools/1.2.0/src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand Down

0 comments on commit 044a174

Please sign in to comment.