调试一行中的大型 json 文件

Debug a large json file that is in one line

我有一个 2MB json 文件,它只有 一行 ,现在我使用 jq:

时出错
$ jq .<nodes.json

parse error: Invalid literal at line 1, column 377140

如何在控制台上进行调试?要查看提到的专栏,我试过这个:

head -c 377139 nodes.json|tail -c 1000

但是我找不到任何错误 t 在那里,所以它似乎不是到达文件中位置的正确方法。

如何调试这样的一行代码?

使用

将文件分成更多行
cat nodes.json|cut -f 1- -d} --output-delimiter=$'}\n'>/tmp/a.json

并分析 /tmp/a.json ,然后你会在第 nr 行出现错误:

parse error: Invalid literal at line 5995, column 47

使用less -N /tmp/a.json找到那一行

我看到您在 shell 提示符下。所以您可以试试 perl,因为您的操作系统可能已经预装了它。

cat nodes.json | json_xs -f json -t json-pretty

这告诉 json_xs 命令行程序解析文件并美化它。

如果您没有安装 json_xs,您可以尝试 json_pp(pp 用于 pure-perl)。

如果两者都没有,则必须使用以下命令安装 JSON::XS perl 模块:

sudo cpanm JSON::XS
[sudo] password for knb: 
--> Working on JSON::XS
Fetching http://www.cpan.org/authors/id/M/ML/MLEHMANN/JSON-XS-3.01.tar.gz ... OK
Configuring JSON-XS-3.01 ... OK
Building and testing JSON-XS-3.01 ... OK
Successfully installed JSON-XS-3.01 (upgraded from 2.34)
1 distribution installed

这会安装 JSON::XS 和一些辅助脚本,其中包括 json_xs 和 json_pp。

然后你可以运行这个简单的一行:

cat dat.json | json_xs -f json -t json-pretty

在错放括号以强制在有效 json 文件中的某处出现嵌套错误后 dat.json 我得到了这个:

cat dat.json | json_xs -f json -t json-pretty
'"' expected, at character offset 1331 (before "{"A_DESC":"density i...") at /usr/local/bin/json_xs line 181, <STDIN> line 1.

也许这比 jq 输出提供更多信息。