skip to Main Content

so I need to make a little json database with Python and JSON and insert new data into it.

Here’s what I am trying to do:

{
    "One": "[email protected]:1234567",
    "Two": ["[email protected]:1234567", "[email protected]:1234567"]
}

So I want to insert multiple instances of like this:

{
    "One": "[email protected]:1234567",
    "Two": ["[email protected]:1234567", "[email protected]:1234567"],
    "One": "[email protected]:1234567",
    "Two": ["[email protected]:1234567", "[email protected]:1234567"],
    "One": "[email protected]:1234567",
    "Two": ["[email protected]:1234567", "[email protected]"],
    "One": "[email protected]:1234567",
    "Two": ["[email protected]:1234567", "[email protected]:1234567"],

}

(I know this is invalid json syntax, i’m not sure what the correct would be).

  1. Let’s say I want to insert "[email protected]:1234567" into the json, so it would add

    "One": "[email protected]:1234567",
    "Two": [""],

How would I do it?

  1. Let’s say I want to insert another row into Two, for [email protected], how would I do this in Python?

Thank you!

2

Answers


  1. As you said this isn’t a valid json so your question isn’t very clear. Jsons are built from keys and values, however keys need to be injective. This means that every two keys should be different from each other.

    Going back to your question. In python dictionary is used to store a json like data. So you actually need to store all your data in a dictionary and than convert it to json. for example:

    import json
    
    data = {"One": "[email protected]:1234567",
        "Two": ["[email protected]:1234567", "[email protected]:1234567"]
            }
    
    json_s = json.dumps(data)
    

    if you already have a json string and want to convert it to dictionary use loads:

    data2 = json.loads(json_s)
    
    Login or Signup to reply.
  2. This is the basic idea. You might consider having an "import_data" function that reads from your file and returns the data structure, and an "export_data" function that writes it back out.

    import json
    jjj = """
    [
        {
            "One": "[email protected]:1234567",
            "Two": ["[email protected]:1234567", "[email protected]:1234567"]
        }
    ]"""
    
    data = json.loads( jjj )
    
    data.append({
        "One": "[email protected]:1234567",
        "Two": ["[email protected]:1234567", "[email protected]:1234567"]
    })
    data.append({
        "One": "[email protected]:1234567",
        "Two": ["[email protected]:1234567", "[email protected]"]
    })
    data.append({
        "One": "[email protected]:1234567",
        "Two": ["[email protected]:1234567", "[email protected]:1234567"]
    })
    
    print(json.dumps(data, indent=4))
    

    Output:

    [
        {
            "One": "[email protected]:1234567",
            "Two": [
                "[email protected]:1234567",
                "[email protected]:1234567"
            ]
        },
        {
            "One": "[email protected]:1234567",
            "Two": [
                "[email protected]:1234567",
                "[email protected]:1234567"
            ]
        },
        {
            "One": "[email protected]:1234567",
            "Two": [
                "[email protected]:1234567",
                "[email protected]"
            ]
        },
        {
            "One": "[email protected]:1234567",
            "Two": [
                "[email protected]:1234567",
                "[email protected]:1234567"
            ]
        }
    ]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search