0

I can't find any info on how can I update original JSON message values with complex paths with different depths.

I tried the jsonpath lib, but it's documentation is all about getting values, and not setting them.

For example I have the following JSON:

{"very":{"long":{"path":{"A":"1"}}},"other":{"path":{"even":{"deeper":{"B":"2"}}}}}

Two paths can be specified outside a function. This one maps to "1":

very.long.path.A

And this one to "2":

other.path.even.deeper.B

jsonpath can extract values from provided paths, but my goal is to modify them.

Something that allows the following would be ideal :

old_json = '{"very":{"long":{"path":{"A":"1"}}},"other":{"path":{"even":{"deeper":{"B":"2"}}}}}'
path = "other.path.even.deeper.B"
value = "100500"
new_json = jsonparser.update_value(old_json, path, value)

Is there a library that can provide such functionalities ?

Charles
  • 324
  • 1
  • 13

1 Answers1

0

Read

To decode json you can use the builtin json library. It is very easy to use. It transfroms json data into valid python objects. See Encoders and decoders

In your case it would be mainly a dict. To Access Elements in a dict see python dictionaries.

Update

Now you Need to split your path into seperate keys keys = path.split(".") and iterate to the last one.

element = json_decoded_root
for key in keys[:-1]:
    element = element.setdefault(key, {})
element[keys[-1]] = new_value

Write back

json.dump(file, data) # Writing to file

or

new_json = json.dumps(data) # Writing in string

Extra: Why you don't lose data

I don't know if I understand you, but this example Shows why you don't lose data when you change a value in a dict.

>>> example = {"A" : 20, "B": 10}
>>> example["A"] = 30
>>> example
{'A': 30, 'B': 10}

You can 'lose' data when you overwrite it. e.g.

>>> example = {"A" : {"C": 10}, "B": 10}
>>> example["A"] = 30
>>> example
{'A': 30, 'B': 10}

Here you 'lost' The dict that conatains the "C" key

Uli Sotschok
  • 1,206
  • 1
  • 9
  • 19
  • how thats hepls to **set** new value in original json? my first idea was the same, but this solution isn't resolve my issue. – yugezegohu Aug 12 '19 at 12:53
  • You can then use [json.dump or json.dumps](https://docs.python.org/3.7/library/json.html?highlight=json#json.dumps) to update the json in the file or string – Uli Sotschok Aug 12 '19 at 12:55
  • i mean this works only with simple json. if i had {"a":"1"} that will work. but when path is not thiat simple you can't set it through element[keys[-1]]. – yugezegohu Aug 12 '19 at 13:05
  • and more than that json can contains several fields at the end of the node. we can't lose them – yugezegohu Aug 12 '19 at 13:16
  • Because of this you update the element object in the for Loop till the end of the path. I took parts of my answer from this answer: https://stackoverflow.com/a/36929668/8411228 – Uli Sotschok Aug 12 '19 at 13:18
  • You dont lose data, except you overwrite it with other data. I suggest you to try it with a very simple example and debug it. You only Need to understand how dicts work. Try to Experiment with them first – Uli Sotschok Aug 12 '19 at 13:20
  • Thanks! Worked for me. I still didn't get how i don't lose my other values, but i know where to dig. – yugezegohu Aug 12 '19 at 13:32
  • I added a short example why you don't lose data. Hopefully it helps. otherwise you are welcome to ask again – Uli Sotschok Aug 12 '19 at 13:52