0

I have the following data in my c.json file:

{
    "192.168.0.129": {
        "username": "me", 
        "streaming": "Spotify", 
        "name": "John", 
        "email": "[email protected]"
    }
}

and this other data that I want to append it to:

new_data = {'next_songs': ['song1', 'song2']}

for that purpose I'm doing this:

with open('c.json', 'r') as json_data: 
    data = json.load(json_data)

data.update(new_data)

with open('c.json', 'w') as json_data: 
    json.dump(data, json_data, indent=4)

this works, but no quite, because I get:

{
    "next_songs": [
        "song1", 
        "song2"
    ], 
    "192.168.0.129": {
        "username": "me", 
        "streaming": "Spotify", 
        "name": "John", 
        "email": "[email protected]"
    }
}

and I want appended data to be a value under the key "192.168.0.129", like so:

{
    "192.168.0.129": {
        "username": "me", 
        "streaming": "Spotify", 
        "name": "John", 
        "email": "[email protected]"
        "new_data": ["song1", "song2"], 
    }
}

how do I achieve this?

0

2 Answers 2

1

Only a certain dict property "192.168.0.129"(which is the inner dict) should be updated, not the whole main dict:

...
data["192.168.0.129"].update(new_data)
Sign up to request clarification or add additional context in comments.

Comments

1

It looks like you may be updating the wrong dictionary

data.update(new_data) should be data["192.168.0.129"].update(new_data)

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.