Search code examples
pythonjsonjsonpickle

Create nested json object using python jsonpickle


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


Solution

  • 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