Working with JSON in Python

Advertisement

Advertisement

Introduction

Python has a built-in package for converting Python objects to and from JSON strings. We will look at how to load JSON from a string or a file, and dump a Python object to a JSON string or file. We will also look at how to minify the output and save space, or make it more human readable with ordering and indentation.

For the official documentation, refer to https://docs.python.org/3/library/json.html.

JSON data in strings

You can convert JSON data to and from string variables. Let's look at both directions.

Convert a JSON string to a Python object

You can convert a JSON string to a Python dictionary or list using json.loads() which is short for "load string".

Full options for json.loads():

json.loads(s, *, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)

Example usage:

import json

json_string = '{"someKey": "someValue"}'
my_dict = json.loads(json_string)

print(type(my_dict))
print(my_dict)

Example output:

<class 'dict'>
{'someKey': 'someValue'}

Convert Python object to JSON string

You can dump a Python dictionary or a list to a JSON string using json.dumps() which is short for "dump string".

Full options for json.dumps():

json.dumps(obj, *, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kw)

Example usage:

import json

my_dict = {"someKey": "someValue"}
json_string = json.dumps(my_dict)

print(type(json_string))
print(json_string)

Example output:

{"someKey": "someValue"}

JSON data in files

You can read and write JSON data directly to file pointers. This can be a traditional file or any file pointer like sys.stdin or sys.stdout.

Dump directly to a file pointer

If you want to write the JSON string directly to a file without having to temporarily store a string, you can call json.dump().

Full options for json.dump():

json.dump(obj, fp, *, skipkeys=False, ensure_ascii=True,
          check_circular=True, allow_nan=True, cls=None,
          indent=None, separators=None, default=None,
          sort_keys=False, **kw)

Example usage:

import json

my_object = {'someKey': 'someValue'}

with open('test.txt', 'w') as my_open_file:
    json.dump(my_object, my_open_file)

# It does not have to be a traditional file, any file pointer will work
# like sys.stdout Standard Output
import sys
json.dump(my_object, sys.stdout)

Read JSON directly from a file pointer

You can load JSON data directly from a file pointer using json.load() which can be convenient.

Full options for json.load():

json.load(fp, *, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)

Example usage:

import json

with open('test.txt') as my_open_file:
    print(json.load(my_open_file))

Output options

Minify output (save space)

By default, the JSON output string has a space between the separators like commas and colons. You can override the default separators and remove the space to make the output more compact.

import json

my_object = {'key1': 'val1', 'key2': 'val2'}

# With default separators
json_string = json.dumps(my_object)
print(json_string)

# With compact separators
json_string = json.dumps(my_object, separators=(',', ':'))
print(json_string)

Example output:

{"key1": "val1", "key2": "val2"}
{"key1":"val1","key2":"val2"}

Format output (easier to read)

You can make the JSON output easier to read by sorting the keys alphabetically and by adding indentation.

import json

my_object = {'key1': 'val1', 'key2': 'val2'}

json_string = json.dumps(my_object, sort_keys=True, indent=2)
print(json_string)

Example output:

{
  "key1": "val1",
  "key2": "val2"
}

Conclusion

After reading this guide, you should understand how to load JSON from a string or from a file pointer, and dump a Python object to a JSON string variable or file. You should also know how to format the output to save space or make it more human readable.

References

Advertisement

Advertisement