Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhanced set_json_key function to be able to take multiple keys with comma seperated #371

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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": "me@test.com"}'
example: '{"user": {"name": "John", "age": 25, "city": "New York"}}'
required: true
schema:
type: string
- name: key
- name: keys
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
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
Expand Down
84 changes: 57 additions & 27 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, values):
self.logger.info(f"OBJ: {json_object}\nKEY: {keys}\nVAL: {values}")
if isinstance(json_object, str):
try:
json_object = json.loads(json_object)
Expand All @@ -328,36 +328,66 @@ def set_json_key(self, json_object, key, value):
# "reason": "Item is not valid JSON (2)"
# }


if isinstance(value, str):
try:
value = json.loads(value)
except json.decoder.JSONDecodeError as e:
pass
# 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 = ...
# 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]
# keys = key.split(".")
# if len(keys) >= 1:
# first_object = keys[0]

# This is awful :)
buildstring = "base_object"
for subkey in keys:
buildstring += f"[\"{subkey}\"]"
# This is awful :)
# buildstring = "base_object"
# for subkey in keys:
# buildstring += f"[\"{subkey}\"]"

buildstring += f" = {value}"
self.logger.info("BUILD: %s" % buildstring)
# 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
# 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()
value = value.strip()

key_list = key.split(".")
current = json_object

for subkey in key_list[:-1]:
current = current.setdefault(subkey, {})

last_key = key_list[-1]

if isinstance(value, str):
try:
value = json.loads(value)
except json.decoder.JSONDecodeError as e:
value = str(value)

current[last_key] = value

return json_object

Expand Down
Loading