如何让json.loads处理Names-Values结构的DataTable序列化JSON?若无则求替代解析器
json.loads() parse the Names/Values JSON structure? Absolutely! Python's built-in json.loads() has no problem parsing this structure at all—because it's valid standard JSON; the unique Names/Values organization is just a space-efficient data layout choice, not a deviation from JSON syntax.
How to parse and use the data
Here's a quick example of loading the structure and converting it into a more familiar row-based format:
import json # Your sample JSON string sample_json = '''{ "Names" : ["summaryDate","count"], "Values" : [["2020-01-15T00:00:00",10],["2020-01-16T00:00:00",12],["2020-01-17T00:00:00",16]] }''' # Parse the JSON into a Python dict parsed_data = json.loads(sample_json) # Extract column names and row values column_names = parsed_data["Names"] row_values = parsed_data["Values"] # Convert to a list of dictionaries (matching standard row-wise JSON) table_rows = [dict(zip(column_names, row)) for row in row_values] print(table_rows) # Output: # [{'summaryDate': '2020-01-15T00:00:00', 'count': 10}, {'summaryDate': '2020-01-16T00:00:00', 'count': 12}, {'summaryDate': '2020-01-17T00:00:00', 'count': 16}]
For tabular data handling
If you're working with this data as a table, libraries like pandas make conversion even simpler:
import pandas as pd df = pd.DataFrame(parsed_data["Values"], columns=parsed_data["Names"]) print(df) # Output: # summaryDate count # 0 2020-01-15T00:00:00 10 # 1 2020-01-16T00:00:00 12 # 2 2020-01-17T00:00:00 16
Alternative parsers for better performance
If you need faster parsing (for very large datasets), third-party JSON libraries work just as well with this structure:
ujson: A fast, lightweight alternative to the standard libraryorjson: Even faster thanujson, with support for more Python types
Example using ujson:
import ujson parsed_data = ujson.loads(sample_json) # Same processing steps as above apply
All of these libraries will correctly parse your Names/Values structure because it adheres strictly to JSON standards.
内容的提问来源于stack exchange,提问作者CestLaGalere

