Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

Python json.JSONDecoder.raw_decode() Method



The Python json.JSONDecoder.raw_decode() method is used to decode a JSON-encoded string into a Python object without needing the entire string to be valid JSON.

This method is useful when parsing JSON from a stream or when dealing with extra data before or after a valid JSON object.

Syntax

Following is the syntax of the Python json.JSONDecoder.raw_decode() method −

json.JSONDecoder().raw_decode(s, idx=0)

Parameters

This method accepts the following parameters −

  • s: A JSON-encoded string that needs to be partially decoded.
  • idx (optional): The starting index from which JSON decoding should begin. Default is 0.

Return Value

This method returns a tuple containing two values:

  • Decoded object: The parsed JSON object.
  • Index: The position in the string where decoding stopped.

Example: Basic Usage

In this example, we use json.JSONDecoder.raw_decode() method to decode a JSON string with extra data at the end −

import json

# JSON string with extra data
json_string = '{"name": "Alice", "age": 25} extra text'

# Create JSONDecoder instance
decoder = json.JSONDecoder()

# Decode JSON string using raw_decode
data, index = decoder.raw_decode(json_string)

print("Decoded Data:", data)
print("Index where parsing stopped:", index)

Following is the output obtained −

Decoded Data: {'name': 'Alice', 'age': 25}
Index where parsing stopped: 28

Example: Parsing JSON from a Substring

The idx parameter allows us to start decoding from a specific index in the string −

import json

# String with JSON data starting at index 5
json_string = 'xxxxx{"name": "Bob", "age": 30}'

# Create JSONDecoder instance
decoder = json.JSONDecoder()

# Decode JSON string from index 5
data, index = decoder.raw_decode(json_string, idx=5)

print("Decoded Data:", data)
print("Index where parsing stopped:", index)

After executing the above code, we get the following output −

Decoded Data: {'name': 'Bob', 'age': 30}
Index where parsing stopped: 31

Example: Handling Multiple JSON Objects

Sometimes, multiple JSON objects are present in a single string. We can use raw_decode() function to parse them sequentially −

import json

# String with multiple JSON objects
json_string = '{"id": 1}{"id": 2}{"id": 3}'

# Create JSONDecoder instance
decoder = json.JSONDecoder()

# Decode JSON objects one by one
idx = 0
while idx < len(json_string):
   data, idx = decoder.raw_decode(json_string, idx)
   print("Decoded Object:", data)

We get the output as shown below −

Decoded Object: {'id': 1}
Decoded Object: {'id': 2}
Decoded Object: {'id': 3}

Example: Handling Invalid JSON Data

If the raw_decode() function encounters an invalid JSON format, it raises a json.JSONDecodeError

import json

# Invalid JSON string
json_string = "Invalid JSON Data"

# Create JSONDecoder instance
decoder = json.JSONDecoder()

try:
   data, index = decoder.raw_decode(json_string)
   print("Decoded Data:", data)
except json.JSONDecodeError as e:
   print("Error:", e)

Following is the output of the above code −

Error: Expecting value: line 1 column 1 (char 0)
python_json.htm
Advertisements