Formatting JSON output in Python

json formatter
python extract data from json file
python list to json
python create json array
python read json file line by line
python object to json
python json serialize
json to html python

How do I format my JSON output so that it looks human readable and I have a JSON file that looks like this:

        "China", "USA", "Nepal"
     "Name": [

        "Sweden", "China", "USA"
     "Name": [

        "India", "China", "USA"
     "Name": [

Here's my code:

user = raw_input("Enter user's name: ")
with open('Users.json') as f:
    data = json.load(f)

for k, v in data.items():
    if any(x in data[user]['Country'] for x in v['Country']):

So far my output looks like this:


I would like an output that looks like this, how do I go around that?:


You just need to change print(v['Name']) for print(v['Name'][0])

JSON Formatting in Python, It's nothing more than a standardized format the community uses to pass data around. Keep in mind, JSON isn't the only format available for this kind of work, but  The JSON (Java Script Object Notation) is light weight, well accepted data interchange format. Using JSON formatting techniques in Python, we can convert JSON strings to Python objects, and also convert Python Objects to JSON strings. To use these functionalities, we need to use the json module of Python.

The u in u'Jade' signifies that the string is encoded in Unicode. You can use print(unicode(v['Name'][0])) if you wish to print the bare string, but it makes no difference in the underlying data type.

You probably want to check out pretty-printing JSON if you will continue working on with JSON objects in Python.

Working With JSON Data in Python – Real Python, Pygmentize + Python json.tool = Pretty Print with Syntax Highlighting is str: print (json.dumps(json.loads(json_thing), sort_keys=sort, indent=indents)) else:  The json module in python allows you to dump a dict to json format directly.

Thank you for the various answers people! But I managed to find figure it out on my own, I tweaked my print statement into this!

userName = raw_input("Enter user's name: ")
with open('extracted_data.json') as f:
    data = json.load(f)

for k, v in data.items():
    if any(x in data[userName]['Acceptable_country'] for x in v['Acceptable_country']):
        print(", ".join(v['Name']))

How to prettyprint a JSON file?, The indent parameter is used to define the indent level for the formatted string. 2. Python Pretty Print JSON File. Let's see what happens when we try to print a  Formatting JSON output in Python. Ask Question How do I format my JSON output so that it looks human readable and I have a JSON file that looks like this:

Python Pretty Print JSON, JSON Pretty Print using Python is required frequently for testing, It takes parameters indent parameter which formats the JSON string. Python JSON Pretty Print Using ipdb module The one more option of pretty print in python is that if you are using Ipython debugger then ipdb module provide you to do pretty print of JSON data. ipdb is a Python Debugger i.e., IPython-enabled. It is a third party interative debugger and also having all pdb’s functionality.

JSON Pretty Print using Python- with Examples, Example. Convert Python objects into JSON strings, and print the values: import json Use the indent parameter to define the numbers of indents: json.dumps(x   Python pretty print JSON To analyze and debug JSON data, we may need to print it in a more readable format. This can be done by passing additional parameters indent and sort_keys to json.dumps () and json.dump () method. Example 5: Python pretty print JSON

Python JSON, import json >>> print json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4, Serialize obj as a JSON formatted stream to fp (a .write() -supporting file-like  Formatting output using format method : The format () method was added in Python (2.6). Format method of strings requires more manual effort. User use {} to mark where a variable will be substituted and can provide detailed formatting directives, but user also needs to provide the information to be formatted.