Ubuntu 16.04 中的 JSON 验证器

Jaf*_*son 15 command-line software-recommendation json

我正在尝试使用一些 JSON 文件验证器。

我遇到过,jq但运行后jq . file.json我只得到了 JSON 格式的输出,而不是我文件中的 JSON 验证。

我想知道如何在 Ubuntu 中检查我的文件中的语法或验证 JSON 格式。请指教!

Byt*_*der 14

尝试jsonlint

sudo apt install jsonlint
Run Code Online (Sandbox Code Playgroud)

基本使用语法是

jsonlint YOUR-FILE.JSON
Run Code Online (Sandbox Code Playgroud)

您可以通过键入man jsonlint或访问其在线联机帮助页来找到其手册:

摘录:

NAME
       jsonlint - A JSON syntax validator and formatter tool

SYNOPSIS
       jsonlint [-v][-s|-S][-f|-F][-ecodec]inputfile.json...

[...]

OPTIONS
       The  return  status  will  be  0 if the file is legal JSON, or non-zero
       otherwise.  Use -v to see the warning details.

       [...]

       -v, --verbose
              Show details of lint checking
       -s, --strict
              Be strict in what is considered legal JSON (the default)
       -S, --nonstrict
              Be loose in what is considered legal JSON
       -f, --format
              Reformat the JSON (if legal) to stdout

[...]
Run Code Online (Sandbox Code Playgroud)

因此,您可以通过检查 .json 的返回码来查看您的 JSON 是否有效jsonlint。您可以通过在echo $?之后立即运行(0=OK,1=invalid)或使用&&,||或对其进行评估来查看它if

  • 当我刚刚在 Ubuntu 上安装 `jsonlint` 软件包时,由于某种原因,安装的二进制文件被称为 `jsonlint-php` 而不是 `jsonlint`。它似乎工作正常,但我必须记住 `dpkg-query -L` 命令来确定它实际安装了什么。 (11认同)

zat*_*ine 10

我尝试过jsonlint,但没有成功。

jq . may-file.json干得好!

希望此反馈有帮助。

  • 对于大型 JSON 文件,推荐使用 `jq` 而不是 `jsonlint`。在我的测试中,读取、分析和打印 600 MiB JSON 文件的速度提高了 10 倍以上。 (3认同)

ded*_*max 6

你可以使用 pythonjson.tool模块来做到这一点

echo '{"name": "dedunu", "country": "LKA"}' | python -m json.tool

如果您有文件,可以按如下方式使用它。

python -m json.tool file.json

但此命令的问题是您无法在 JSON 文件中获得有关问题的详细信息。我从这个链接找到了答案。