Pandas read Json - Trailing Data

隐身守侯 提交于 2021-02-08 11:12:07

问题


I am trying to read a large Json file through Pandas pd.read_json, but an error is showing: ValueError: Trailing data

From my research here I was not successful, so I would like to ask for your help. Tried to run a Json validator and the output is below.

How can I fix this? Thank you


回答1:


The error messeage you presented contains precise location where the source of the problem is:

(At line #191), (At position #1)

Look at the indicated place in your JSON file.

Edit

A weird detail in your file is that the comma should be after "}" in line 190, not a the beginning of the next line, but I'm not sure whether this is actually any problem.

Attempt "partial reading", without the object starting at line 191.

One more detail to check: If "}" in line 190 terminates the whole above content, then:

  • your input file contains multiple JSON objects at the main level,
  • probably you should enclose the whole file with "[" and "]", so that the whole file will be a list of objects.

Edit 2

I made such an experiment:

Input file contains:

{
  "aa" : "aa1",
  "bb" : "bb1"
},
{
  "aa" : "aa2",
  "bb" : "bb2"
}

(2 JSON objects at the main level).

Then pd.read_json('Input.json') raised ValueError: Trailing data.

But when I changed the input file to:

[
  {
    "aa" : "aa1",
    "bb" : "bb1"
  },
  {
    "aa" : "aa2",
    "bb" : "bb2"
  }
]

(a list of 2 JSON objects), I got a proper result:

    aa   bb
0  aa1  bb1
1  aa2  bb2

Look at your input file, maybe in your case the problem is just like I showed.

Yet another experinent

Input file contains:

{ "aa" : "aa1", "bb" : "bb1" }
{ "aa" : "aa2", "bb" : "bb2" }
{ "aa" : "aa3", "bb" : "bb3" }

i.e. separate objects, without surrounding "[" and "]" either with or without comma after each object.

You can read it calling pd.read_json('Input.json', lines=True).

But the limitation here is that each line must contain complete JSON object, so in your case it is rather useless.



来源:https://stackoverflow.com/questions/61716765/pandas-read-json-trailing-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!