Skip to content

Using "json" module to write and load Json file in Python

Last Updated on 2020-10-07 by Clay

Display Python code on the screen

Introduction

Json format file is very easily to process, and many programming language support it. So this data format is quite suitable for joint access to a data and development between different programming languages.

Today I want to introduce how to use the Python package "json" to write and load a Json format file.

Json(JavaScript Object Notation) is a representation format of JavaScript objects and is a very lightweight data exchange language. If you prefer to read a clean Json format, you can browse the Json file on the following website: https://jsoneditoronline.org/.

If you want to know more about Python, maybe you can refer to: Easy Python Tutorial Speed Run —— Only one hour.

In short, file access via Json format can save us a lot of trouble in data preprocessing. The following is a description of the chapter:

  • dumps
  • loads
  • dump
  • load

dumps() and loads() deal with Json format directly in the program;
dump() and load() with very similar function names are for Json file reading and writing.


dumps

json module is a built-in module in Python, so we don't need to install it and we can import this module directly.

import json


The we look at a simple example:

data = {'today': 9, 'is': 7, 'nice': 8, 'day': 6}
output = json.dumps(data)
print(output)


Output:

{"today": 9, "is": 7, "nice": 8, "day": 6}

These data is a dict data type, one key corresponds to one value. we can use json.dumps() function to convert it into Json format. We can see the output is not much different from our raw data.

The only thing to note is that the string in Python can be represented by "single quotes". After converting to the Json format, it must to be used "double quotes" to represent.


In addition, we can sort the Json format data.

data = {'b': 10, 'a': 100}
output = json.dumps(data, sort_keys=True)
print(output)


Output:

{"a": 100, "b": 10}

The data is sorted alphabetically.


The output can also be adjusted to be easy to read:

data = {'a': 100, 'b': 1000, 'c': 10000}
output = json.dumps(data, separators=(',\n', ': '))
print(output)


Output:

{"a": 100,
 "b": 1000,
 "c": 10000}

loads

So how should we read the data converted into Json format in the code? The following is a demonstration of the Json format just generated:

jsonFile = \
'''{"a": 100,
"b": 1000,
"c": 10000}'''
output = json.loads(jsonFile)
print(output)


Output:

{'a': 100, 'b': 1000, 'c': 10000}

The Json file just converted has been converted back to Python's dict data type.


dump

Next, we start to introduce how to save our data as a Json file because it is a more general purpose. After all, we want to save it as a Json file for later reading.

If the above dumps() function is suitable for converting Python data type to Json format (in our program), then dump() function is to save Python data type to Json file.

以下簡單地進行示範:

data = {'a': 100, 'b': 1000, 'c': 10000}
with open('abc.json', 'w', encoding='utf-8') as f:
    json.dump(data, f)


Output:

We can see that the data stored in abc.json in Json format


load

Our last step is to read the stored data into the process.

with open('abc.json', 'r', encoding='utf-8') as f:
    output = json.load(f)
print(output)


Output:

{'a': 100, 'b': 1000, 'c': 10000}

Have you restored the data type of Python? As long as we are proficient in using the json package, we can store a lot of processed data, and then read it in when we continue to use it next time, and continue to process it, which is very convenient.

In addition, because Json is limited by the storage format, it can not support all Python data types. If you want to store all Python formats and working stages directly, you can refer to the introduction to the pickle module in Python.


References


Read More

Leave a ReplyCancel reply

Exit mobile version