12/6/16:
1.
Reading json response:
import json
import urllib, urllib2
response = '{"status":"Success",
"cust_data": [{"cat": "c1",
"variables":{"name": "t1","value":
"0" }}]}'
# Your code goes here
pretty_resp = json.loads(response)
print "Print json data: " ,pretty_resp
varval=pretty_resp["status"]
print "Valu of the variable: ", varval
O/P:
Print json data: {u'status':
u'Success', u'cust_data': [{u'variables': {u'name': u't1', u'value': u'0'},
u'cat': u'c1'}]}
Valu of the variable: Success
2.
Reading json from an array
import json
import urllib, urllib2
response = '{"status":"Success", "cust_data":
[{"cat": "c1", "variables":{"name":
"t1","value": "0" }}]}'
# Your code goes here
pretty_resp = json.loads(response)
print "Print json data: " ,pretty_resp
varval=pretty_resp["status"]["cust_data"]
print "Valu of the variable: ", varval
O/P:
Traceback (most recent call last):
File "prog.py", line 8,
in <module>
TypeError: string indices must be integers
<Resolution> the above error could be removed by adding [0] after
cust_data, as cust_data holds data in [].
3.
Iterating
through key and values:
a.
import json
import urllib, urllib2
response = '{"status":"Success",
"cust_data": [{"cat": "c1",
"variables":{"name": "t1","value":
"0" }}]}'
# Your code goes here
pretty_resp = json.loads(response)
print "Print json data: " ,pretty_resp
for key, value in pretty_resp.iteritems():
print key
O/P:
Print json data: {u'status':
u'Success', u'cust_data': [{u'variables': {u'name': u't1', u'value': u'0'},
u'cat': u'c1'}]}
status
cust_data
b.
import json
import urllib, urllib2
response =
'{"status":"Success", "cust_data":
[{"cat": "c1", "variables":{"name":
"t1","value": "0" }}]}'
# Your code goes here
pretty_resp = json.loads(response)
print "Print json data:
" ,pretty_resp
cust_datavals=
pretty_resp["cust_data"][0]["variables"]
for key, value in
cust_datavals.iteritems():
print key
O/P:
Print json data: {u'status': u'Success', u'cust_data':
[{u'variables': {u'name': u't1', u'value': u'0'}, u'cat': u'c1'}]}
name
value
4.
Extracting
data from the [] and {} in json … I think indexes [0],[1]….while extracting
data
from []
import json
import urllib, urllib2
response = '{"status":"Success",
"cust_data": [{"cat": "c1",
"variables":{"name": "t1","value":
"0" }}]}'
# Your code goes here
pretty_resp = json.loads(response)
print "Print json data: " ,pretty_resp
#for key, value in pretty_resp.iteritems():
#
print key
varval=pretty_resp["cust_data"][0]["variables"]["name"]
print "Value of the variable: ", varval
O/P:
Print json data: {u'status':
u'Success', u'cust_data': [{u'variables': {u'name': u't1', u'value': u'0'},
u'cat': u'c1'}]}
Value of the variable: t1
Note:
l['type']
gives you a list, not an object. So you have
to access it like a listl['type'][0]["references"]
Note: Key could be variable name as aswell like this:
custdata="cust_data"
cust_datavals=
pretty_resp[custdata][0]["variables"
- 5.
# Validate values of the variabls in API response. But no validation is done on category and sub category. import json import urllib response = '{"status":"Success", "cust_data": [{"cat": "c1", "variables":[{"name": "dfdf","value": "0" },{"name": "mo_snc","value": "10" }]},{"cat": "c2", "variables":[{"name": "trd_losed","value": "0" },{"name": "trd_lse_d","value": "10" }]}]}'# Your code goes herepretty_resp = json.loads(response) print("Print json data:pretty_resp= " ,pretty_resp) #for key, value in pretty_resp.iteritems(): # print key#i is number of cust data layersicount=len(pretty_resp["cust_data"]) print("Number of cust data layers: ",icount) for i in range(0,icount): print("i= ",i) jcount = len(pretty_resp["cust_data"][i]["variables"]) for j in range(0,jcount): print("j= ", j) varname=pretty_resp["cust_data"][i]["variables"][j]["name"] print("variable name: ",varname) if(varname=="mo_snc"): varvalue=pretty_resp["cust_data"][i]["variables"][j]["value"] if(varvalue=="10"): print("Name: ",varname,"& value is: ",varvalue) print("Script has passed") else: print("Name: ",varname, "value is: ",varvalue,", so not matching") break breakprint('Script has completed running')
No comments:
Post a Comment