Python json.decoder.JSONArray() Function



The Python json.decoder.JSONArray() function is an internal helper function used to parse JSON arrays.

This function is primarily used by the JSON decoder when parsing array structures from a JSON-encoded string.

Syntax

Following is the syntax of the Python json.decoder.JSONArray() function βˆ’

json.decoder.JSONArray(s_and_end, scan_once, **kwargs)

Parameters

This function accepts the following parameters βˆ’

  • s_and_end: A tuple containing the JSON string and the index position to start parsing.
  • scan_once: A function used for scanning JSON values.
  • **kwargs: Additional keyword arguments.

Return Value

This function returns a tuple containing the parsed list and the index position where parsing stopped.

Example: Basic Usage

In this example, we use the json.decoder.JSONArray() function to parse a JSON array βˆ’

import json.decoder

# JSON-encoded array
json_string = '[1, 2, 3]'

# Define scan_once function properly
scan_once = json.decoder.JSONDecoder().scan_once

# Parse JSON array using JSONArray()
parsed_array, end_index = json.decoder.JSONArray((json_string, 1), scan_once)

print("Parsed Array:", parsed_array)
print("End Index:", end_index)

Following is the output of the above code βˆ’

Parsed Array: [1, 2, 3]
End Index: 9

Example: Parsing Nested JSON Arrays

The JSONArray() function can also handle nested JSON arrays βˆ’

import json.decoder

# JSON-encoded nested array
json_string = '[[1, 2], [3, 4]]'

# Get the scan_once function from JSONDecoder
scan_once = json.decoder.JSONDecoder().scan_once

# Parse JSON array using JSONArray()
parsed_array, end_index = json.decoder.JSONArray((json_string, 1), scan_once)

print("Parsed Nested Array:", parsed_array)
print("End Index:", end_index)

Following is the output obtained βˆ’

Parsed Nested Array: [[1, 2], [3, 4]]
End Index: 16

Example: Handling Empty JSON Arrays

The function correctly handles empty JSON arrays βˆ’

import json.decoder

# JSON-encoded empty array
json_string = '[]'

# Define scan_once function
def scan_once(s, idx):
   return json.decoder.scanstring(s, idx)

# Parse JSON array
parsed_array, end_index = json.decoder.JSONArray((json_string, 1), scan_once)

print("Parsed Empty Array:", parsed_array)
print("End Index:", end_index)

We get the output as shown below βˆ’

Parsed Empty Array: []
End Index: 2

Example: Handling Invalid JSON Arrays

If the input is not a valid JSON array, a ValueError is raised βˆ’

import json.decoder

# Invalid JSON-encoded array (missing closing bracket)
json_string = '[1, 2, 3'

# Get the scan_once function from JSONDecoder
scan_once = json.decoder.JSONDecoder().scan_once

try:
   # Attempt to parse an invalid JSON array
   parsed_array, end_index = json.decoder.JSONArray((json_string, 1), scan_once)
   print("Parsed Array:", parsed_array)
except ValueError as e:
   print("Error:", e)

The result produced is as follows βˆ’

Error: Expecting ',' delimiter: line 1 column 9 (char 8)
python_json.htm
Advertisements