Commit 09f3471d674f668550f9ffd03247414e7a5f0154
1 parent
3e2abe83e3
Exists in
master
Now, you can add elements to and existing dictionary by giving an input dictionary (content).
Showing 1 changed file with 10 additions and 1 deletions Side-by-side Diff
volia/jsoneditor.py
... | ... | @@ -14,6 +14,7 @@ |
14 | 14 | |
15 | 15 | # Data variables |
16 | 16 | content_data = json.loads(content) |
17 | + | |
17 | 18 | data = {} |
18 | 19 | |
19 | 20 | # Load file if necessary |
20 | 21 | |
21 | 22 | |
... | ... | @@ -24,10 +25,18 @@ |
24 | 25 | # Walk through the json path |
25 | 26 | pointer = data |
26 | 27 | for i, way in enumerate(jsonpath): |
28 | + # End of the json path | |
27 | 29 | if i == len(jsonpath) - 1: |
30 | + | |
28 | 31 | if not force and way in pointer: |
29 | 32 | raise Exception(f"In your json file, {way} already exists in the path {jsonpath}. Add --force option.") |
30 | - pointer[way] = content_data | |
33 | + else: | |
34 | + # If dictionary in both cases, copy from content to pointer | |
35 | + if type(pointer[way]) is dict and type(content_data) is dict: | |
36 | + for key in content_data: | |
37 | + pointer[way][key] = content_data[key] | |
38 | + else: | |
39 | + pointer[way] = content_data | |
31 | 40 | else: |
32 | 41 | if way not in pointer: |
33 | 42 | pointer[way] = {} |