在 Python 中将 JSON 转换为换行符分隔的 JSON [英] Converting JSON into newline delimited JSON in Python

查看:46
本文介绍了在 Python 中将 JSON 转换为换行符分隔的 JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是将 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('
'.join(result))

{"key01": "value01", "key02": "value02", "keyN": "valueN"}
{"key01": "value01", "key02": "value02", "keyN": "valueN"}
{"key01": "value01", "key02": "value02", "keyN": "valueN"}

* 我在这里使用 StringIOprint 只是为了让示例更容易在本地进行测试.

* 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆