This guide explains how to add JSON data to an array in Python. You'll utilize the built-in json module for this task.

Steps:

  1. Create an Empty Array:
array = []
  1. Parse JSON to a Python Dictionary:
import json

json_data = '{"name": "John", "age": 30, "city": "New York"}'
dict_data = json.loads(json_data)
  1. Append the Python Dictionary to the Array:
array.append(dict_data)

Complete Code Example:

import json

# Create an empty array
array = []

# Parse JSON data to a Python dictionary
json_data = '{"name": "John", "age": 30, "city": "New York"}'
dict_data = json.loads(json_data)

# Append the Python dictionary to the array
array.append(dict_data)

# Print the array
print(array)

Output:

[{'name': 'John', 'age': 30, 'city': 'New York'}]
Python: Adding JSON Data to an Array

原文地址: https://www.cveoy.top/t/topic/mOHl 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录