5

For a project I need to write in a json file with python but all I have already seen (json.dump) don't match with what I want to do...

I have a structure and I just want to add something inside. I want to add a service with a input for example :

{
"Serial_011": "011",
"Servers_011":
    [
        {
            "hostname": "srv-a.11",
            "ipv4_address": "0.0.0.0",
            "services":
                [
                    {
                        "uri": "http://www.google.fr/1",
                        "expected_code": 200
                    },
                    {
                        "uri": "http://www.google.fr/2",
                        "expected_code": 200
                    }
                ]
        },
        {
            "hostname": "nsc-srv-b.11",
            "ipv4_address": "0.0.0.0",
            "services":
                [
                    {
                        "uri": "http://www.google.fr/3",
                        "expected_code": 200
                    },
                    {
                        "uri": "http://www.google.fr/4",
                        "expected_code": 200
                    }
                ]
        }
    ]
}

Thanks in advance

7
  • 3
    Read the JSON into an object, add your info to that object and serialize it again (with prettyprinting if needed). Commented Aug 2, 2016 at 9:14
  • Where I find this ? Thanks for your quick answer Commented Aug 2, 2016 at 9:15
  • 3
    google.com Commented Aug 2, 2016 at 9:16
  • docs.python.org/2.7/library/json.html ? Commented Aug 2, 2016 at 9:16
  • 1
    If you don't have a special file format then the usual way of adding something not-just-at-the-end is loading it, modifying it and then writing back the whole thing. (On large files you try to use a streaming approach) Commented Aug 2, 2016 at 9:18

1 Answer 1

2

I keep in mind 4 methods while I work with JSON objects in python.

  • json.dumps(<a python dict object>) - gives a string representation of json formed out of the python dict object
  • json.dump( <a python dict object>,<file obj>) - writes a json file in file object
  • json.loads(<a string>) - reads a json object from a string
  • json.load(<a json file>) - reads a json object from the file.

The next important thing to keep in mind is that json's and dict in python are equivalent.

So let us say, the file contents reside inside a file addThis.json. You have a already existing json object inside the file existing.json.

The below code should be able to do the job

import json

existing = json.load(open("/tmp/existing.json","r"))
addThis = json.load(open("/tmp/addThis.json","r"))

for key in addThis.keys():
     existing[key] = addThis[key]

json.dump(exist,open("/tmp/combined.json","w"),indent=4)

Edit: Assuming the contents of the addThis is not in a file but is to be read from the console.

import json

existing = json.load(open("/tmp/existing.json","r"))

addThis = input()
# paste your json here.
# addThis is now simply a string of the json content of what you to add

addThis = json.loads(addThis) #converting a string to a json object.
# keep in mind we are using loads and not load

for key in addThis.keys():
     existing[key] = addThis[key]

json.dump(exist,open("/tmp/combined.json","w"),indent=4)
Sign up to request clarification or add additional context in comments.

7 Comments

So in existing.json I will put the current json, in addThis.json I write what I want to add with an input() and the result will be conbined.json ?
To clarify, the contents (the one that is to be added) is to be read from input()?
@M_S I've added the code for reading the new json contents from input() as well. Please up vote and confirm the answer if this works. Thank you.
It's turn in loop... What is exist in json.dump ?
Could you be a little more clearer?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.