使用 jq 检查字符串是否是有效的 JSON

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46954692/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 19:02:20  来源:igfitidea点击:

Check if string is a valid JSON with jq

jsonbashjq

提问by Milkncookiez

I need to catch an error when lifting a service. The response can be null, a string error message like

提升服务时我需要捕获错误。响应可以是null一个字符串错误消息,如

error services-migration/foobar: Not found: services-migration/foobar

error services-migration/foobar: Not found: services-migration/foobar

or a valid JSON when everything is fine. I was wondering if there is a way with jqto simply check if the provided string is a valid JSON. I could ofc check the string for some keywords like errorf.e., but I'm looking for a more robust option, where eg. I get a true/falseor 1/0from jq. I was looking through the docs of jqand also some questions here on SO but everything was about finding and picking out key-values from a JSON, but nothing about simply validating a string.

或者当一切正常时有效的 JSON。我想知道是否有一种方法jq可以简单地检查提供的字符串是否是有效的 JSON。我可以检查某些关键字的字符串,例如errorfe,但我正在寻找更强大的选项,例如。我从 jq得到 atrue/false1/0。我正在查看jq关于 SO的文档和一些问题,但一切都是关于从 JSON 中查找和挑选键值,而不是简单地验证字符串。

UPDATE:

更新:

I've got this:

我有这个:

 result=$(some command)

from which the result is the string error services-migration/foobar: Not found: services-migration/foobar

结果是字符串 error services-migration/foobar: Not found: services-migration/foobar

And then the if statement:

然后是 if 语句:

 if jq -e . >/dev/null 2>&1 <<<"$result"; then
    echo "it catches it"
 else
    echo "it doesn't catch it"
 fi

And it always ends up in the elseclause.

它总是以else子句结尾。

采纳答案by Tom Fenech

From the manual:

从手册:

-e / --exit-status:

Sets the exit status of jq to 0 if the last output values was neither false nor null, 1 if the last output value was either false or null, or 4 if no valid result was ever produced. Normally jq exits with 2 if there was any usage problem or system error, 3 if there was a jq program compile error, or 0 if the jq program ran.

-e / --exit-status:

如果最后一个输出值既不是假也不是空,则将 jq 的退出状态设置为 0,如果最后一个输出值是假或空,则设置为 1,如果没有产生有效结果,则设置为 4。通常,如果出现任何使用问题或系统错误,jq 以 2 退出,如果存在 jq 程序编译错误,则以 3 退出,如果 jq 程序运行则以 0 退出。

So you can use:

所以你可以使用:

if jq -e . >/dev/null 2>&1 <<<"$json_string"; then
    echo "Parsed JSON successfully and got something other than false/null"
else
    echo "Failed to parse JSON, or got false/null"
fi

In fact, if you don't care about distinguishing between the different types of error, then you can just lose the -eswitch. In this case, anything considered to be valid JSON (including false/null) will be parsed successfully by the filter .and the program will terminate successfully, so the ifbranch will be followed.

事实上,如果您不在乎区分不同类型的错误,那么您可能会失去-e开关。在这种情况下,任何被认为是有效 JSON(包括 false/null)的东西都将被过滤器成功解析.,程序将成功终止,因此if将遵循分支。

回答by Reddy SK

This is working for me

这对我有用

echo $json_string | jq -e . >/dev/null 2>&1  | echo ${PIPESTATUS[1]}

that returns return code:

返回返回码:

  • 0 - Success
  • 1 - Failed
  • 4 - Invalid
  • 0 - 成功
  • 1 - 失败
  • 4 - 无效

Then you can evaluate the return code by further code.

然后您可以通过进一步的代码评估返回码。