Movatterモバイル変換


[0]ホーム

URL:


Python Tutorial

Python json.decoder.scanstring() Function



The Pythonjson.decoder.scanstring() function is an internal helper function used to scan and decode a JSON string.

This function is useful when parsing JSON data and extracting string values efficiently.

Syntax

Following is the syntax of the Pythonjson.decoder.scanstring() function −

json.decoder.scanstring(s, end, strict=True)

Parameters

This function accepts the following parameters −

  • s: The JSON-encoded string to be scanned.
  • end: The starting index where the scanning should begin.
  • strict (optional): If True (default), the function will enforce strict JSON compliance.

Return Value

This function returns a tuple containing the decoded string and the index position where parsing stopped.

Example: Basic Usage

In this example, we use thejson.decoder.scanstring() function to extract a string from a JSON-encoded value −

import json.decoder# JSON-encoded stringjson_string = '"Hello, World!"'# Decode the stringdecoded_string, end_index = json.decoder.scanstring(json_string, 1)print("Decoded String:", decoded_string)print("End Index:", end_index)

Following is the output obtained −

Decoded String: Hello, World!End Index: 15

Example: Handling Unicode Characters

Thescanstring() function correctly decodes Unicode escape sequences −

import json.decoder# JSON-encoded string with Unicode escapejson_string = '"Hello, \u2603!"'  # Unicode for snowman ()# Decode the stringdecoded_string, end_index = json.decoder.scanstring(json_string, 1)print("Decoded String:", decoded_string)print("End Index:", end_index)

Following is the output of the above code −

Decoded String: Hello, !End Index: 11

Example: Handling Escape Sequences

The function also processes escape sequences such as newline and tab correctly −

import json.decoder# JSON-encoded string with properly escaped sequencesjson_string = r'"Line 1\nLine 2"'  # Using raw string to avoid interpretation# Decode the stringdecoded_string, end_index = json.decoder.scanstring(json_string, 1)print("Decoded String:")print(decoded_string)print("End Index:", end_index)

We get the output as shown below −

Decoded String:Line 1Line 2End Index: 16

Example: Handling Control Characters

The json.decoder.scanstring() function is used to decode JSON strings while handling control characters. Since control characters (like \x00) are not allowed in strict JSON parsing, this example demonstrates how to catch errors and clean the string before decoding −

import json.decoderimport re# JSON-encoded string with a control character (invalid in JSON)json_string = '"Hello\x00World"'try:   # Attempt to decode using scanstring() in strict mode (default)   decoded_string, end_index = json.decoder.scanstring(json_string, 1)   print("Decoded String (Strict Mode):", decoded_string)except ValueError as e:   print("Error:", e)# Workaround: Remove control characters before decodingcleaned_json_string = re.sub(r'[\x00-\x1F]', '', json_string)  # Remove control chars# Decode the cleaned stringdecoded_string, end_index = json.decoder.scanstring(cleaned_json_string, 1)print("Decoded String (After Cleaning):", decoded_string)

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

Error: Invalid control character at: line 1 column 7 (char 6)Decoded String (After Cleaning): HelloWorld
python_json.htm
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp