Adding a child node to a JSON node dynamically

Posted by Sai on Stack Overflow See other posts from Stack Overflow or by Sai
Published on 2014-08-22T21:57:04Z Indexed on 2014/08/22 22:20 UTC
Read the original article Hit count: 164

Filed under:
|
|

I have to create a nested multi level json depending on the resultset that I get from MYSQL. I created a json object initially. Now I want to add child nodes to the already child nodes in the object.

d = collections.OrderedDict()                                                           
jsonobj = {"test": dict(updated_at="today", ID="ID", ads=[])}       

for rows1 in rs:                                                                        
    jsonobj['list']["ads"].append(dict(unit = "1",                           
                                        type ="ad_type",                         
                                        id ="123",                         
                                        updated_at="today",                
                                        x_id="111",                          
                                        x_name="test"))                      


cur.execute("SELECT * from f_test")                                      
rs1 = cur.fetchall()                                                                
for rows2 in rs1:                                                                   
   propertiesObj = []                                                               
   d["name"]="propName"                                                             
   d["type"]="TypeName"                                                             
   d["value"]="Value1"                                                              
   propertiesObj.append(d)                                                          

   jsonobj['play_list']["ads"].append()

Here in the above line I want to add another child node to [play_list].[ads] which is a array list again.

the output should look like the following [list].[ads].[preferences].

© Stack Overflow or respective owner

Related posts about python

Related posts about mysql