-3

I have an output which is in XML format and I have converted that XML format output to JSON. After converting the data to JSON I need to print only selected output.

api = NaElement("snapmirror-get")


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

print ("Received:\n")
print (xo.sprintf())
data = json.loads(json.dumps(xmltodict.parse(xo.sprintf())))

This is the code I'm using to convert an XML file to JSON. The output will save in data variable and I need to print only selected output. Below is the output of data variable.

{'results': {'@status': 'passed', 'attributes': {'snapmirror-info': {'break-failed-count': '0', 'break-successful-count': '0', 'destination-location': 'svm-svm1-vault:volume1', 'destination-volume': 'volume1', 'destination-volume-node': 'svm1cdot1-02', 'destination-vserver': 'svm-svm1-vault', 'destination-vserver-uuid': '545fa72b-e84c-11e6-ae0a-00a098546e58', 'exported-snapshot': 'daily.2019-01-03_0010', 'exported-snapshot-timestamp': '1546454401', 'is-constituent': 'false', 'is-healthy': 'true', 'lag-time': '46182', 'last-transfer-duration': '759', 'last-transfer-end-timestamp': '1546469259', 'last-transfer-from': 'svm-svm1:volume1', 'last-transfer-network-compression-ratio': '1:1', 'last-transfer-size': '5192737823', 'last-transfer-type': 'update', 'max-transfer-rate': '0', 'mirror-state': 'snapmirrored', 'newest-snapshot': 'daily.2019-01-03_0010', 'newest-snapshot-timestamp': '1546454401', 'opmask': '18446744073709551615', 'policy': 'Daily_policy1', 'policy-type': 'vault', 'relationship-control-plane': 'v2', 'relationship-group-type': 'none', 'relationship-id': '4557665a-ebcd-11e6-ae0a-00a098546e58', 'relationship-status': 'idle', 'relationship-type': 'vault', 'resync-failed-count': '0', 'resync-successful-count': '0', 'schedule': Rep_Set_01', 'source-location': ''svm-svm1:volume1', 'source-volume': 'volume1', 'source-vserver': 'svm-scr1', 'source-vserver-uuid': 'acce074e-e14d-11e6-93ae-00a09899b4be', 'total-transfer-bytes': '27879000066', 'total-transfer-time-secs': '2043', 'update-failed-count': '0', 'update-successful-count': '8', 'vserver': 'svm-svm1-vault'}}}}

From the above output I need to print only lag-time value and source-volume name output.

Josh Friedlander
  • 10,870
  • 5
  • 35
  • 75
  • 1
    Welcome to SO. If you hope to get any help, you will have to edit your question to 1/ make sure it's readable (correct code formatting etc) and 2/ it contains all the relevant informations (cf https://stackoverflow.com/help/how-to-ask and https://stackoverflow.com/help/mcve) – bruno desthuilliers Jan 03 '19 at 08:06

1 Answers1

1

You have a typo in your output (I tried to edit your question but there is another edit pending.)

Accessing data in JSON is very simple. Using your example:

output = {'results': {'@status': 'passed', 'attributes': {'snapmirror-info': {'break-failed-count': '0', 'break-successful-count': '0', 'destination-location': 'svm-svm1-vault:volume1', 'destination-volume': 'volume1', 'destination-volume-node': 'svm1cdot1-02', 'destination-vserver': 'svm-svm1-vault', 'destination-vserver-uuid': '545fa72b-e84c-11e6-ae0a-00a098546e58', 'exported-snapshot': 'daily.2019-01-03_0010', 'exported-snapshot-timestamp': '1546454401', 'is-constituent': 'false', 'is-healthy': 'true', 'lag-time': '46182', 'last-transfer-duration': '759', 'last-transfer-end-timestamp': '1546469259', 'last-transfer-from': 'svm-svm1:volume1', 'last-transfer-network-compression-ratio': '1:1', 'last-transfer-size': '5192737823', 'last-transfer-type': 'update', 'max-transfer-rate': '0', 'mirror-state': 'snapmirrored', 'newest-snapshot': 'daily.2019-01-03_0010', 'newest-snapshot-timestamp': '1546454401', 'opmask': '18446744073709551615', 'policy': 'Daily_policy1', 'policy-type': 'vault', 'relationship-control-plane': 'v2', 'relationship-group-type': 'none', 'relationship-id': '4557665a-ebcd-11e6-ae0a-00a098546e58', 'relationship-status': 'idle', 'relationship-type': 'vault', 'resync-failed-count': '0', 'resync-successful-count': '0', 'schedule': 'Rep_Set_01', 'source-location': 'svm-svm1:volume1', 'source-volume': 'volume1', 'source-vserver': 'svm-scr1', 'source-vserver-uuid': 'acce074e-e14d-11e6-93ae-00a09899b4be', 'total-transfer-bytes': '27879000066', 'total-transfer-time-secs': '2043', 'update-failed-count': '0', 'update-successful-count': '8', 'vserver': 'svm-svm1-vault'}}}}

enc = json.dumps(output)
dec = json.loads(enc)

print(dec['results']['attributes']['snapmirror-info']['lag-time'])
print(dec['results']['attributes']['snapmirror-info']['source-volume'])

>46182
>volume1

If you want to iterate through them, you can do this:

rel = dec["results"]["attributes"]["snapmirror-info"]
for k in rel:
      # if k in ['lag-time', 'source-volume'] :
    print(k, rel[k])
Josh Friedlander
  • 10,870
  • 5
  • 35
  • 75