问题描述
我的目标是将JSON文件转换为可以从Cloud Storage上传到BigQuery的格式(如此处所述)使用Python.
My goal is to convert JSON file into a format that can uploaded from Cloud Storage into BigQuery (as described here) with Python.
我尝试使用 newlineJSON 包进行转换,但是收到以下错误.
I have tried using newlineJSON package for the conversion but receives the following error.
JSONDecodeError: Expecting value or ']': line 2 column 1 (char 5)
有人对此有解决方案吗?
Does anyone have the solution to this?
这是示例JSON代码:
Here is the sample JSON code:
[{
"key01": "value01",
"key02": "value02",
...
"keyN": "valueN"
},
{
"key01": "value01",
"key02": "value02",
...
"keyN": "valueN"
},
{
"key01": "value01",
"key02": "value02",
...
"keyN": "valueN"
}
]
这是现有的python脚本:
And here's the existing python script:
with nlj.open(url_samplejson, json_lib = "simplejson") as src_:
with nlj.open(url_convertedjson, "w") as dst_:
for line_ in src_:
dst_.write(line_)
推荐答案
使用jq
的答案确实很有用,但是如果您仍然想使用Python(从问题中看来),可以这样做.带有内置的json
模块.
The answer with jq
is really useful, but if you still want to do it with Python (as it seems from the question), you can do it with built-in json
module.
import json
from io import StringIO
in_json = StringIO("""[{
"key01": "value01",
"key02": "value02",
"keyN": "valueN"
},
{
"key01": "value01",
"key02": "value02",
"keyN": "valueN"
},
{
"key01": "value01",
"key02": "value02",
"keyN": "valueN"
}
]""")
result = [json.dumps(record) for record in json.load(in_json)] # the only significant line to convert the JSON to the desired format
print('\n'.join(result))
{"key01": "value01", "key02": "value02", "keyN": "valueN"}
{"key01": "value01", "key02": "value02", "keyN": "valueN"}
{"key01": "value01", "key02": "value02", "keyN": "valueN"}
*我在这里使用StringIO
和print
只是为了使示例更易于本地测试.
* I'm using StringIO
and print
here just to make a sample easier to test locally.
或者,您可以使用 Python jq绑定将其与.
As an alternative, you can use Python jq binding to combine it with the other answer.
这篇关于在Python中将JSON转换为换行符分隔的JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!