Extracting Multiple JSON Objects from a Single File
In situations where you encounter a JSON file containing multiple objects, the task of isolating specific fields becomes essential. One approach involves utilizing the json.JSONDecoder.raw_decode method.
raw_decode offers the ability to iteratively decode large JSON strings. It continues parsing until a valid JSON object is encountered, while tracking the last non-whitespace position. This allows for repeated calls to raw_decode by providing the previous end position as an argument.
However, Python's JSON module requires whitespace-free input. Therefore, preprocessing is necessary to remove preceding whitespace. The provided code addresses this by utilizing the NOT_WHITESPACE regular expression to locate the initial non-empty character.
<code class="python"># Extract Multiple JSON Objects from json import JSONDecoder, JSONDecodeError import re NOT_WHITESPACE = re.compile(r'\S') def decode_stacked(document, pos=0, decoder=JSONDecoder()): while True: match = NOT_WHITESPACE.search(document, pos) if not match: return pos = match.start() try: obj, pos = decoder.raw_decode(document, pos) except JSONDecodeError: # Handle error accordingly raise yield obj # Example usage s = """ {"a": 1} [ 1 , 2 ] """ for obj in decode_stacked(s): print(obj)</code>
The output demonstrates the successful extraction of the multiple JSON objects from the input string:
{'a': 1} [1, 2]
The above is the detailed content of How to Extract Multiple JSON Objects from a Single File in Python?. For more information, please follow other related articles on the PHP Chinese website!