0

I want to build nested json object using python jsonpickle,

something like this

 {"key": "value",    
  "key2": {
          "key2": "value2", 
          "key": "value"}
          }  
 }

using jsonpickele set value to variable name and

def __init__(self, value):
        self.key1 = value
        self.key2 =' {key:value,key2,value}'

and

 jsonpickle.encode(obj, unpicklable=False)

can generate json object but I need way to create json object like putting key value pair, like concatenate key value to nested json object inside loop

UdayaLakmal
  • 4,035
  • 4
  • 29
  • 40
  • Why are you using a string for self.key2? If it is an actual Python dict, things will work fine (e.g. `self.key2={'key': 'value', 'key2':'value'}`) – John Paulett Apr 03 '12 at 00:55
  • yes, but i need something to append key value to existing json object using jsonpickle – UdayaLakmal Apr 03 '12 at 08:09
  • I don't think this is not necessarily a jsonpickle-related issue. Couldn't you decode the existing json object (`json.loads('{key:value,key2,value)}')`), add additional keys to the resulting Python dictionary, then re-encode to json (via `json.dumps``) – John Paulett Apr 03 '12 at 14:34
  • thank you @John Paulett bt i'm able to solved, i put that in the answer – UdayaLakmal Apr 04 '12 at 07:55

1 Answers1

0

It can solved as following

def __init__(self, value):
        self.key1 = value
        arr={}
        #append value to arr
        arr.update({'key2':'value2'})
        arr.update({'key':'value'})
        self.key2 = arr

Then when jsonpickle.encode(obj, unpicklable=False) will produce a nested json object we required

UdayaLakmal
  • 4,035
  • 4
  • 29
  • 40