0

I need to append dictionary values to an already existing JSON file. How can I be able to do that?

My details.json File

{"name": "someName"}

Dictionary generated by my python script

list1 = {"name": "someOthername"}

with open("details.json") as r:
    data = json.load(r)
    desirableDict = data.append(list1) # It has to be something like this
    print(desirableDict)

Desirable Output: {"name": ["someName", "someOthername"]}

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
sani bani
  • 83
  • 1
  • 7
  • If it's possible for there to be more than one value for a key, use a list *even when there's only one*. That way your code can use consistent access patterns instead of needing to be written to handle two separate cases distinctly. – Charles Duffy Dec 19 '19 at 20:38
  • Note that there's a really [excellent answer by @jpp](https://stackoverflow.com/a/54108746/14122) on the closely related question [Merging dictionary value lists in Python](https://stackoverflow.com/questions/26910708/merging-dictionary-value-lists-in-python). – Charles Duffy Dec 19 '19 at 20:51
  • ...btw, "has to be something like this" doesn't really provide enough detail on what "something like this" means for people to judge if an answer is responsive or not. – Charles Duffy Dec 19 '19 at 20:53
  • @CharlesDuffy I don't know what could I put in there, although the output of the code is very clear. – sani bani Dec 19 '19 at 20:59
  • The output is indeed clear, but if you didn't mean the phrase "has to be something like this" to provide a further restriction on what kind of implementation (resulting in that output) would be acceptable, then why is it there? – Charles Duffy Dec 19 '19 at 21:09

2 Answers2

1

It seems like you need deep merging of structures. I would like to recommend you to use this awesome library https://pypi.org/project/deepmerge/.

There are a lot of examples like you want to achieve.

from deepmerge import always_merger

base = {"foo": ["bar"]}
next = {"foo": ["baz"]}

expected_result = {'foo': ['bar', 'baz']}
result = always_merger.merge(base, next)

assert expected_result == result
Alexandr Shurigin
  • 3,921
  • 1
  • 13
  • 25
1

You can check all keys within a for loop and put the values ​​of the json file and list1 inside a list like this:

import json

list1 = {"name": "someOthername"}

with open("details.json") as file:

    data = json.load(file)
    desirableDict = data.copy()

    for key in data:
        if key in list1:

            if type(data[key]) is list:
                data[key].append(list1[key])
            else:
                desirableDict[key] = [data[key],list1[key]]

print(desirableDict)
JeanExtreme002
  • 200
  • 3
  • 14
  • Much better. I still prefer Alexandr's answer insofar as it's encouraging good practices (like using data structures consistent with what contents one intends to hold rather than what values one has right this moment), but this one is no longer objectively wrong. – Charles Duffy Dec 19 '19 at 20:56
  • @JeanExtreme002 Hi, Thank you for this solution. It works perfectly, even if list1 has several different keys. – sani bani Dec 19 '19 at 21:08
  • 1
    `data[key].extend([list1[key],])` is a very strange way of writing `data[key].append(list1[key])`. Also, you can just do `for key in data`, no need to call `.keys()` – juanpa.arrivillaga Dec 19 '19 at 21:10