How to load and parse a JSON file in Python

Published on Aug. 22, 2023, 12:15 p.m.

To load and parse a JSON file in Python

To load and parse a JSON file in Python, you can use the json module that comes with Python. Here’s an example:

  1. Import the json module:
import json
  1. Open the JSON file and load its contents into a Python object:
with open('filename.json', 'r') as f:
data = json.load(f)

This assumes that the JSON file is in the same directory as the Python script. If it’s in a different directory, you’ll need to specify the full path to the file.
3. Once you have the Python object, you can access its properties and values like you would with any other Python object:

print(data['key'])

You can also use the json.loads() method to parse a JSON string instead of a file, like this:

json_string = '{"name": "John", "age": 30, "city": "New York"}'
data = json.loads(json_string)

This will give you a Python dictionary object that you can work with.

To solve the problem of writing Chinese characters to a JSON file in Python and getting garbled characters,

To solve the problem of writing Chinese characters to a JSON file in Python and getting garbled characters, you can use the json module’s dump() or dumps() method with the ensure_ascii=False parameter. Here’s an example:

import json

data = {
    "name": "张三",
    "age": 30,
    "city": "北京"
}

with open('data.json', 'w', encoding='utf-8') as f:
    json.dump(data, f, ensure_ascii=False)

In this example, the ensure_ascii=False parameter ensures that the JSON data is encoded in UTF-8, which supports Chinese characters without garbling.

If you prefer to write the JSON data to a string instead of a file, you can use the dumps() method instead of dump(), like this:

import json

data = {
    "name": "张三",
    "age": 30,
    "city": "北京"
}

json_string = json.dumps(data, ensure_ascii=False)
print(json_string)

This will output a JSON string with properly encoded Chinese characters.

If your JSON file is large, you may want to consider reading and writing it line-by-line to reduce memory usage

If your JSON file is large, you may want to consider reading and writing it line-by-line to reduce memory usage. You can accomplish this in Python using the json module and the built-in io module. Here’s an example:

import json
import io

# Reading a large JSON file line by line
with io.open('large_file.json', 'r') as f:
    for line in f:
        data = json.loads(line)
        # Do something with the data...

# Writing to a JSON file line by line
with io.open('output_file.json', 'w') as f:
    for data in data_list:
        json_string = json.dumps(data)
        f.write(json_string + '\n')

In this example, we use the io module to open the file and the json module to parse or write the JSON data. The json.loads() method is used to parse the JSON data when reading the file line-by-line. When writing back to the file, we convert each JSON object to a string using json.dumps() method and append a newline character before writing it to the output file.

Tags: