How to parse Json using Python module

How to parse Json using Python module

Satya Python solution , ,

How to Parse Json using Python module : Python has many modules to parse different kind of files, like excel, XML, yaml, etc. In this post I will explain how we can parse json script using python module, known as json.

Json is known as java script object notation. It is a text in Java object notation, which is being used to store or exchange data. This helps in transmitting data object, which consist of attribute value pair and array data type. It was initially created for java script but now a days it is being supported by all programming language. Therefore, it is language independent. It has syntax, inspired by Java script. So, it is not strict subset of java script

About json module

json module is encode and decode API. We can use this to encode or decode json. To import json module into python script use import.

import json

Lets see how to parse Json using Python module

 

Json encoding.

Following code shows how we can encode data structure into json format. Here I have used json.dumps() function to convert data structure into json.

>>> import json
>>> json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}])
'["foo", {"bar": ["baz", null, 1.0, 2]}]

Json Decoding

In order to do decoding use json.loads() function. This function accept json data as parameter.

>>> import json
>>> json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]')
['foo', {'bar': ['baz', None, 1.0, 2]}]

 

I have used ucd CLI command to get agent information from udeploy in json format. This json response, I will use with python function to parse it.

 

/apps/ibm-ucd3/agent/opt/udclient/udclient -username admin -weburl https://localhost:8445 getAgent -agent myAgent
Password not passed as command line property password or set in environment, DS_PASSWORD
Password : 
{
  "propSheet": {
    "id": "0b2c81d5-643f-4f52-bb8c-de541d533423",
    "path": "agents\/9b631e4c-6771-4817-a718-d7c473d31732\/propSheet",
    "version": 11,
    "versionCount": 11,
    "commit": 124,
    "versioned": true
  },
  "extendedSecurity": {
    "read": true,
    "Add to Agent Pool": true,
    "Create Resources": true,
    "Delete": true,
    "Edit Basic Settings": true,
    "Manage Impersonation": true,
    "Manage Properties": true,
    "Manage Teams": true,
    "Manage Version Imports": true,
    "View Agents": true,
    "teams": []
  },
  "security": {
    "read": true,
    "Add to Agent Pool": true,
    "Create Resources": true,
    "Delete": true,
    "Edit Basic Settings": true,
    "Manage Impersonation": true,
    "Manage Properties": true,
    "Manage Teams": true,
    "Manage Version Imports": true,
    "View Agents": true
  },
  "id": "9b631e4c-6771-4817-a718-d7c473d31732",
  "securityResourceId": "be689610-a0cc-494f-8f34-09cf05630208",
  "name": "myAgent",
  "active": true,
  "licensed": false,
  "licenseType": "NONE",
  "status": "ONLINE",
  "version": "6.2.2.0.825094",
  "lastContact": 1534430463523,
  "dateCreated": 1527705135984,
  "workingDirectory": "\/apps\/ibm-ucd3\/agent\/var\/work\/",
  "impersonationPassword": "****",
  "impersonationUseSudo": false,
  "impersonationForce": false,
  "tags": []
}

I have created following script to describe how we can use python model to parse json. Here, in this script we have run ucd client command to get agent information. We run this by using python os.popen() function. After we get response, it is passed as a parameter to json.load() function, this function actually parse it and store into a dictionary in a key-value pair.

import json
import getpass
import os
import re

pasw = getpass.getpass('Password:')

cmd="/apps/ibm-ucd3/agent/opt/udclient/udclient -username admin -password "+ pasw + " -weburl https://localhost:8445 getAgent -agent myAgent"
response=os.popen(cmd).read()

parsed_json=json.loads(response)

print response
print "Agent Name: " , parsed_json["name"]
print "Agent Status: " , parsed_json["status"]
print "Agent id: " , parsed_json["id"]
print "Agent Name: " , parsed_json["propSheet"]["version"]

Following is the output we get after we run preceding code. You can see that, we have printed dictionary value correspond to particular key.

Agent Name: myAgent
Agent Status: ONLINE
Agent id: 9b631e4c-6771-4817-a718-d7c473d31732
Agent Name: 11

For more detail refer, python doc.

 

Also, see how we can parse json using Perl program.

 

You May Also Like..

How to concatenate string in Python

How to concatenate string in Python

How to concatenate string in Python: Concatenating a string is very important feature in any computer programming language. Python provide […]

Multi threading program in python

Multi threading program in python

Multi threading program in python: Multi-Thread is a multiple processes running concurrently in one CPU core processor. It is different […]

Read and write CSV file using python

Read and write CSV file using python : CSV file, which is comma separated file, is the most popular form […]

Leave a Reply

Your email address will not be published. Required fields are marked *