Query mysql from json came from angularjs - Python -
hi guys json data , json data angularjs, can me on this?. i'm stuck on it. thank you.
{u'ischecked': {u'49871': false, u'49870': true, u'113634': false}}
then in python want update mysql when id found in json data
right here code updating , want connect json data
updatetable = """update table_x set value = '1' """ db.session.execute(updatetable) db.session.commit()
here solution
#!/usr/bin/env python import platform import sys import urllib2 import simplejson json def update_table(id): sqlupdatestr = "update table_x set value = '1' id="+id print "executing update: " + sqlupdatestr def test_parse_json(): print "loading json ..." req = urllib2.request("http://localhost/example.json") opener = urllib2.build_opener() f = opener.open(req) # json.load() deserialize json document , return python object. data = json.load(f) print data['ischecked'] print "" id in data['ischecked']: id_val = str2bool(data['ischecked'][id]) if id_val == true: print "found id update: " + id update_table(id) else: print "ignoring record id=" + id def str2bool(v): return v.lower() in ("yes", "true", "t", "1") def main(): test_parse_json() return if __name__ == '__main__': main()
and content of example.json is:
{ "ischecked":{ "49870":"true", "49871":"false", "113634":"false" } }
Comments
Post a Comment