DougKruger
DougKruger

Reputation: 4624

How to create json by nesting two dictionaries

say I have two dictionaries; orig and inner how to create json object from them such that inner is embedded insider orig:

orig = {
   'A': 1,
   'B': 2,
   'C': 3
}

inner = {
   'D': 4,
   'E': 5
}

#embed inner insider orig
new = {
    'A': 1,
    'B': 2,
    'C': 3,
    'inner':{
              'D': 4,
              'E': 5
     }
}

Upvotes: 0

Views: 144

Answers (2)

Ishaan
Ishaan

Reputation: 886

new=dict(orig)
new.update({"inner":inner})

Upvotes: 1

Iain Dwyer
Iain Dwyer

Reputation: 184

I don't think that's possible. There would be no value to access the inner dictionary from.

You could do:

new = {
    'A': 1,
    'B': 2,
    'C': 3,
    'inner': {
       'D': 4,
       'E': 5
    }
}

with: orig['inner'] = inner but with structure you posted there would be no way to access the inner dict.

Upvotes: 1

Related Questions