I
I
ikryloff2017-10-15 19:05:20
Python
ikryloff, 2017-10-15 19:05:20

How to convert JSON to readable form using Python?

Please help solve the problem:
The task is to write a script that takes the path to a file with arbitrary data in JSON format as input and outputs its contents to the console in a readable form: adds line breaks, left indents and spaces.
My code is not processing everything. Just a hint, thanks!

import json

stt = 'js.txt'
def load_data(filepath):
    with open(filepath,'r') as myfile:
        return json.load(myfile)

def pretty_print_json(data):
    for st in data:
        print(json.dumps(st, indent="   ",ensure_ascii=False, sort_keys=True))


pretty_print_json(load_data(stt))

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sim3x, 2017-10-15
@ikryloff

https://stackoverflow.com/questions/9105031/how-to... https://docs.python.org/3/library/json.html#json.dumps

>>> import json
>>> print(json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4))
{
    "4": 5,
    "6": 7
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question