在不安装任何程序包的情况下验证shell脚本中的JSON文件语法 [英] Validate JSON file syntax in shell script without installing any package

查看:67
本文介绍了在不安装任何程序包的情况下验证shell脚本中的JSON文件语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在不安装任何程序包的情况下在shell脚本中验证JSON文件的语法?

How can I validate the syntax of a JSON file in shell script without installing any package?

推荐答案

我还没有找到python -mjson.tool无法正常工作的系统.因此,您可以这样做:

I have yet to find a system where python -mjson.tool doesn't work. So you can do:

python -mjson.tool "$somefile" > /dev/null

退出代码将为非零值,并且如果文件无效的JSON,则会在stderr上收到解析错误.

The exit code will be nonzero and you get the parse error on stderr if the file is not valid JSON.

注意:Python库不遵循JSON规范,并且允许NaNInfinity作为值.因此,使用json.tool将使一些错误略过.对于我的尽早发现人工文档中的错误的用例来说,它仍然足够.

Note: The Python libraries don't follow the JSON spec and allow NaN and Infinity as values. So using json.tool will let some errors slip through. Still it's good enough for my use case of catching errors in human-written documents early.

这篇关于在不安装任何程序包的情况下验证shell脚本中的JSON文件语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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