Update JSON objects with Python script for AWS -


i have bunch of json objects need update in order use cli aws. here example of json format. need update lbtest, lbtest-cookie-pol , 80 different values.

{     "loadbalancername": "lbtest",      "policyname": "lbtest-cookie-pol",      "cookieexpirationperiod":80  } 

in cases, there multiple values here each load balancer name. output need this:

{     "loadbalancername": "lbtest",      "policyname": "lbtest-cookie-pol",      "cookieexpirationperiod":80  } {     "loadbalancername": "lbtest",      "policyname": "lbtest-cookie-pol2",      "cookieexpirationperiod":8080.  } 

suppose had csv file these entries, kind of python script can write loop through these , print out json output? part having issues printing of nested json object. print doesn't seem multiple lines or curly braces have. newbie here appreciate kind of solution.

you can use json.dumps method , it's options mentioned in documentations:
example using indent option on python 2.7 :

>>> dictionary = {     "loadbalancername": "lbtest",      "policyname": "lbtest-cookie-pol",      "cookieexpirationperiod":80 } #a dictionary object made csv >>> print dictionary {'policyname': 'lbtest-cookie-pol', 'cookieexpirationperiod': 80, 'loadbalancername': 'lbtest'} >>> import json >>> jobj = json.dumps(dictionary,indent=4, separators=(',', ': ')) >>> print jobj {     "policyname": "lbtest-cookie-pol",     "cookieexpirationperiod": 80,     "loadbalancername": "lbtest" } >>> f = open(r'jtest.txt','w') #save our json object file >>> json.dump(dictionary,fp,indent =4 , seperators = (',',': ')) >>> f.close() >>> f = open(r'jtest.txt!','r') #read our object file >>> test = json.load(f) >>> test {u'policyname': u'lbtest-cookie-pol', u'cookieexpirationperiod': 80, u'loadbalancername': u'lbtest'} >>> dict(test) {u'policyname': u'lbtest-cookie-pol', u'cookieexpirationperiod': 80, u'loadbalancername': u'lbtest'} 

here how our file looks like: jtest.txt file


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -