Software Development Kit (SDK) and API Discussions

How to get result from NMSDK in json format or set content type to application/json

neha_T
7,088 Views

i am using NMSDK script  to get info of cluster,
but i want to get the output in json() format.

How can i set the Content type as "application/json"  

<snip>

import sys
import pprint
import json
sys.path.append("./NetApp")
from NaServer import *


s = NaServer("Array", 1 , 32)
s.set_server_type("FILER")
s.set_transport_type("HTTPS")
s.set_port(443)
s.set_style("LOGIN")
s.set_admin_user("admin", "password@123")
api = NaElement("cluster-identity-get")

xi = NaElement("desired-attributes")
api.child_add(xi)


xi1 = NaElement("cluster-identity-info")
xi.child_add(xi1)

xi1.child_add_string("cluster-contact","<cluster-contact>")
xi1.child_add_string("cluster-location","<cluster-location>")
xi1.child_add_string("cluster-name","<cluster-name>")
xi1.child_add_string("cluster-serial-number","<cluster-serial-number>")
xi1.child_add_string("cluster-uuid","<cluster-uuid>")
xi1.child_add_string("rdb-uuid","<rdb-uuid>")

xo = s.invoke_elem(api)
print (type(xo))
if (xo.results_status() == "failed") :
print ("Error:\n")
print (xo.sprintf())
sys.exit (1)


print ("Received:\n")
data = xo.sprintf()
print (data)

<snip>

1 ACCEPTED SOLUTION

gaurav_verma
5,468 Views

Initially you said, you want Application/JSON. May I know you need JSON or Application/JSON? 

 

If you need only JSON, you can not convert XML to JSON but from XML to Dict. Though there is work around to convert using Application/JSON using Response which is usable in Flask. 

If you just need JSON in order to access Dict key/values then xmltodict should work for you. You can use this way. 

 

obj = xmltodict.parse(xo.sprintf())
jd = json.dumps(obj)
jl = json.loads(jd)


if xo.results_status() == "failed":
reason = xo.results_reason()
print ("Failure: %s" %reason)
else:

for h in jl["results"]["attributes-list"]["aggr-attributes"]:
if int(h["aggr-space-attributes"]["percent-used-capacity"]) > 80:
print (h["aggregate-name"], readable_size(int(h["aggr-space-attributes"]["size-total"])), readable_size(int(h["aggr-space-attributes"]["size-used"])), readable_size(int(h["aggr-space-attributes"]["size-available"])), h["aggr-space-attributes"]["percent-used-capacity"])
else:
pass


@neha_T wrote:

yes, now it is giving json output, but only class instance

when i try to get data also it is giving instance details only.

 

example:

obj = xmltodict.parse(xo.sprintf())
jd = json.dumps(obj)
jl = json.loads(jd)
a = Response(jl, content_type='application/json')
#pprint.pprint(dir(a))

pprint.pprint(a)--> <Response likely-streamed [200 OK]>
print a.get_json-->  <bound method Response.get_json of <Response likely-streamed [200 OK]>>
print a.is_json---> True
print a._get_data_for_json--> <bound method Response._get_data_for_json of <Response likely-streamed [200 OK]>>
 
 
what i want is json data.

 

 

 

View solution in original post

12 REPLIES 12

deepj
6,789 Views

NMSDK recieves data in the XML format which is similar to JSON. They have key value pair, can be nested into other elements arbitarily. 

neha_T
6,775 Views

yes , i know

i want to know is there any way we can set content-type

deepj
6,775 Views

No, NMSDK only supports XML like formatting for the data getting sent and being recieved.

gaurav_verma
6,700 Views

Did you try this? 

 

obj = xmltodict.parse(xo.sprintf())
jd = json.dumps(obj)
a = Response(jd, content_type='application/json')

neha_T
6,625 Views

a = Response(jd, content_type='application/json')----> what us Response here??

gaurav_verma
6,605 Views

from flask import Response

 

neha_T
6,581 Views

No not working.

because xo.sprinf() return unicode type data.

and when we use below code

<snip>

obj = xmltodict.parse(xo.sprintf())
jd = json.dumps(obj)
a = Response(jd, content_type='application/json')
pprint.pprint(a.data)----> it return in dict format, but its data type is <str>
print data["cluster-uuid"] ---> so when try to access by it key it gives error

<snip>

Data i get: 

'{"results": {"@status": "passed", "attributes": {"cluster-identity-info": {"cluster-contact": null, "cluster-location": "B", "cluster-name": "IA", "cluster-serial-number": "1--7868", "cluster-uuid": "----uuid----", "rdb-uuid": "---uuid---"}}}}'

 

<error>
print data["cluster-uuid"]
TypeError: string indices must be integers, not str

<error>

gaurav_verma
6,509 Views

Try to add json loads. 

 

obj = xmltodict.parse(xo.sprintf())
jd = json.dumps(obj)
jl = json.loads(jd)
a = Response(jl, content_type='application/json')

neha_T
6,464 Views

yes, now it is giving json output, but only class instance

when i try to get data also it is giving instance details only.

 

example:

obj = xmltodict.parse(xo.sprintf())
jd = json.dumps(obj)
jl = json.loads(jd)
a = Response(jl, content_type='application/json')
#pprint.pprint(dir(a))

pprint.pprint(a)--> <Response likely-streamed [200 OK]>
print a.get_json-->  <bound method Response.get_json of <Response likely-streamed [200 OK]>>
print a.is_json---> True
print a._get_data_for_json--> <bound method Response._get_data_for_json of <Response likely-streamed [200 OK]>>
 
 
what i want is json data.

gaurav_verma
5,469 Views

Initially you said, you want Application/JSON. May I know you need JSON or Application/JSON? 

 

If you need only JSON, you can not convert XML to JSON but from XML to Dict. Though there is work around to convert using Application/JSON using Response which is usable in Flask. 

If you just need JSON in order to access Dict key/values then xmltodict should work for you. You can use this way. 

 

obj = xmltodict.parse(xo.sprintf())
jd = json.dumps(obj)
jl = json.loads(jd)


if xo.results_status() == "failed":
reason = xo.results_reason()
print ("Failure: %s" %reason)
else:

for h in jl["results"]["attributes-list"]["aggr-attributes"]:
if int(h["aggr-space-attributes"]["percent-used-capacity"]) > 80:
print (h["aggregate-name"], readable_size(int(h["aggr-space-attributes"]["size-total"])), readable_size(int(h["aggr-space-attributes"]["size-used"])), readable_size(int(h["aggr-space-attributes"]["size-available"])), h["aggr-space-attributes"]["percent-used-capacity"])
else:
pass


@neha_T wrote:

yes, now it is giving json output, but only class instance

when i try to get data also it is giving instance details only.

 

example:

obj = xmltodict.parse(xo.sprintf())
jd = json.dumps(obj)
jl = json.loads(jd)
a = Response(jl, content_type='application/json')
#pprint.pprint(dir(a))

pprint.pprint(a)--> <Response likely-streamed [200 OK]>
print a.get_json-->  <bound method Response.get_json of <Response likely-streamed [200 OK]>>
print a.is_json---> True
print a._get_data_for_json--> <bound method Response._get_data_for_json of <Response likely-streamed [200 OK]>>
 
 
what i want is json data.

 

 

 

neha_T
5,392 Views

thanks @gaurav_verma 

it is working fine.

gaurav_verma
5,345 Views

Can you please close this by accepting the solution. 

Public