Movatterモバイル変換


[0]ホーム

URL:


Python Tutorial

Python json.JSONDecoder.raw_decode() Method



The Pythonjson.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 Pythonjson.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 is0.

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 usejson.JSONDecoder.raw_decode() method to decode a JSON string with extra data at the end −

import json# JSON string with extra datajson_string = '{"name": "Alice", "age": 25} extra text'# Create JSONDecoder instancedecoder = json.JSONDecoder()# Decode JSON string using raw_decodedata, 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

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

import json# String with JSON data starting at index 5json_string = 'xxxxx{"name": "Bob", "age": 30}'# Create JSONDecoder instancedecoder = json.JSONDecoder()# Decode JSON string from index 5data, 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 useraw_decode() function to parse them sequentially −

import json# String with multiple JSON objectsjson_string = '{"id": 1}{"id": 2}{"id": 3}'# Create JSONDecoder instancedecoder = json.JSONDecoder()# Decode JSON objects one by oneidx = 0while 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 theraw_decode() function encounters an invalid JSON format, it raises ajson.JSONDecodeError

import json# Invalid JSON stringjson_string = "Invalid JSON Data"# Create JSONDecoder instancedecoder = 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
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp