user2672385
user2672385

Reputation: 13

How to write json file with duplicate keys in Python?

I need help to write my data to json file on below format,

{
    "Employees": {
        "Employee": {
            "name": "john"
             ....
        },
        "Employee": {
            "name": "Taylor"
             ....   
        },
        "Employee": {

            "name": "Prab"
             ....
        }
    }
}

Here Employee key should be repeated for each node, since Python dictionary doesn't allow duplicate keys, I couldn't get this result.

I don't need any [ , ] brackets, and extra {, } braces , I need exactly the same format that mentioned above. I tried with json.dumps(..) method.

Upvotes: 0

Views: 276

Answers (1)

Prune
Prune

Reputation: 77857

You can't; that's a contradiction in terms. A Python dict, by definition, does not have duplicate keys. It appears to me that you might want to promote everything one level, so that you dict looks like:

{
"Employees": {
    "john": {
         ....
    },
    "Taylor": {
         ....   
    },
    "Prab": {
         ....
    }
}

Upvotes: 3

Related Questions