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"]}

5
  • 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. Commented Dec 19, 2019 at 20:38
  • Note that there's a really excellent answer by @jpp on the closely related question Merging dictionary value lists in Python. Commented Dec 19, 2019 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. Commented Dec 19, 2019 at 20:53
  • @CharlesDuffy I don't know what could I put in there, although the output of the code is very clear. Commented Dec 19, 2019 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? Commented Dec 19, 2019 at 21:09

2 Answers 2

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)
Sign up to request clarification or add additional context in comments.

3 Comments

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.
@JeanExtreme002 Hi, Thank you for this solution. It works perfectly, even if list1 has several different keys.
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()
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.