如何最好地处理连接不良的 json

问题描述 投票:0回答:1

我从客户端收到一个不正确的 json 文件。
客户端将多个 json 响应连接到一个文件中:

{
    object1
    {
        ...
    }
}   
{
    object2
    {
        ...
    }
}
...

当我用 pyspark 中的数据帧解析它时,我总是得到只有一个正确的根对象的计数,因为它只读取第一个对象而不关心其余的对象。
我需要以某种方式处理这个问题,我试图找出性能方面最好的方法是什么?
dataframe 可以处理错误的 json,或者我可以用 python 轻松修复这个问题吗?

python json pyspark
1个回答
0
投票

您可以使用

jq
模块来解析数据。

>>> data = open("tmp.json").read()
>>> data
'{"foo": 1}\n{"bar": 2}\n'
>>> import jq
>>> jq.compile(".").input_text(data).all()
[{'foo': 1}, {'bar': 2}]
© www.soinside.com 2019 - 2024. All rights reserved.