jQuery的不解析JSON与空格 [英] jq not parsing json with spaces

查看:90
本文介绍了jQuery的不解析JSON与空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试运行以下命令,该命令从文件中读取JSON并使用jq对其进行格式化:

I’m trying to run the following command that reads JSON from a file and formats it with jq :

jq -n -r --arg m $(<$1) '$m | fromjson | {records:[{value:.}]}'

当输入JSON不包含空格(例如{"test":"helloworld"}:

It produces the desired output when the input JSON does not contain spaces, such as {"test":"helloworld"} :

{
  "records": [
    {
       "value": {
        "test": "helloworld"
      }
     }
  ]
}

但是,对于像{"test":"hello world"}这样的输入,它将给出以下错误:

However, for an input like {"test":"hello world"} it would give the following error:

jq: error: syntax error, unexpected QQSTRING_START, expecting $end (Unix shell quoting issues?) at <top-level>, line 1:
world"}     
jq: 1 compile error

无法找出导致此问题的原因.

Can’t figure out what’s causing this problem.

感谢您的帮助:)

推荐答案

这不是jq问题,而是报价问题(错误中突出显示).

It's not a jq problem but a quoting issue (as highlighted in the error).

更改--arg选项以使值包含在双引号内:

Change the --arg option to have the value within double quote:

arg='{"test":"hello world"}'
jq -n -r --arg m "$arg" '$m | fromjson | {records:[{value:.}]}'

这篇关于jQuery的不解析JSON与空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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