You shouldn’t try to manipulate them as strings – this will be fragile and very specific to the strings. Instead, parse the strings into dictionaries, do whatever manipulations you want, and then dump them back to JSON strings:
import json
s1 = '{"step":"example step", "data":"example data", "result":"example result"}'
s2 = '{"attachments":[ { "data":"gsddfgdsfg...(base64) ", "filename":"example1.txt", "contentType":"plain/text" } ] }'
d1 = json.loads(s1)
d2 = json.loads(s2)
d1.update(d2)
json.dumps(d1)
This will give you a clean json string:
'{"step": "example step", "data": "example data", "result": "example result", "attachments": [{"data": "gsddfgdsfg...(base64) ", "filename": "example1.txt", "contentType": "plain/text"}]}'
of if you want the object rather than the string, d1
is now:
{'step': 'example step',
'data': 'example data',
'result': 'example result',
'attachments': [{'data': 'gsddfgdsfg...(base64) ',
'filename': 'example1.txt',
'contentType': 'plain/text'}]}
2
solved combining two json strings without ” or ‘