Biks
Biks

Reputation: 21

How to work with JSON in python

My Python Script gives a JSON output. How can I see it in the proper JSON format?

I tried with parsing with json.dumps() and json.loads(), but could not achieve the desired result.

======= Myscript.py ========

import sys
import jenkins
import json
import credentials

# Credentails 
username = credentials.login['username']
password = credentials.login['password']


# Print the number of jobs present in jenkins
server = jenkins.Jenkins('http://localhost:8080', username=username, password=password)


# Get the installed Plugin info
plugins = server.get_plugins_info()
#parsed = json.loads(plugins)   # take a string as input and returns a dictionary as output.
parsed = json.dumps(plugins)    # take a dictionary as input and returns a string as output.
#print(json.dumps(parsed, indent=4, sort_keys=True))
print(plugins)
print(parsed)

Upvotes: 0

Views: 779

Answers (1)

GracefulRestart
GracefulRestart

Reputation: 781

It sounds like you want to pretty-print your JSON. You would need to pass the correct parameters to json.dumps():

parsed = json.dumps(plugins, sort_keys=True, indent=4)

Check and see if that is what you are looking for.

Upvotes: 3

Related Questions