尝试使用jq shell脚本解析JSON数据时观察到语法错误 [英] Syntax error observed while trying to parse the JSON data by using jq shell script

查看:57
本文介绍了尝试使用jq shell脚本解析JSON数据时观察到语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的输入,我试图获取密钥的详细信息,并试图形成下面的json文件,但是当我尝试运行下面的命令时,它返回了语法错误,我不确定如何解决

I have the the below input and I'm trying to fetch the Key details and trying to form a below json file but when I tried to run the below command it returned syntax error and I'm not sure How can I resolve this

输入(Response.Json):

Input (Response.Json):

{
    "expand": "schema,names",
    "startAt": 0,
    "maxResults": 50,
    "total": 4,
    "issues": [{
            "expand": "operations,versionedRepresentations,editmeta,changelog,renderedFields",
            "id": "1999875",
            "self": "https://amazon.kindle.com/jira/rest/api/2/issue/1999875",
            "key": "KINDLEAMZ-67578"
        },
        {
            "expand": "operations,versionedRepresentations,editmeta,changelog,renderedFields",
            "id": "2019428",
            "self": "https://amazon.kindle.com/jira/rest/api/2/issue/2019428",
            "key": "KINDLEAMZ-68661"
        },
        {
            "expand": "operations,versionedRepresentations,editmeta,changelog,renderedFields",
            "id": "2010958",
            "self": "https://amazon.kindle.com/jira/rest/api/2/issue/2010958",
            "key": "KINDLEAMZ-68167"
        }
    ]
}

命令:

> getResponse=$( jq 'reduce (.issues | to_entries[]) as {$key,$value}
> ({}; .["JIRA-\($key + 1)"] = $value.key )' response.json );

错误:

错误:语法错误,意外的'{',期望减少'$'(.issues |to_entries [])as {$ key,$ value}({}; .." JIRA-($ key + 1)] = $ value.key)^ 1编译错误

error: syntax error, unexpected '{', expecting '$' reduce (.issues | to_entries[]) as {$key,$value} ({}; .["JIRA-($key + 1)"] = $value.key ) ^ 1 compile error

预期输出:

{
"JIRA-1":"KINDLEAMZ-67578",

"JIRA-2":"KINDLEAMZ-68661",

"JIRA-3":"KINDLEAMZ-68167"
}

任何人都可以帮助我解决此问题.我完全被封锁,一无所知...

Anyone please help me resolve this. I'm completely blocked and clueless...

推荐答案

我已验证以下内容适用于您的JSON示例;

I've verified that the following works with your JSON sample;

.issues | reduce to_entries[] as $kv ({}; .["JIRA-\($kv.key)"] = $kv.value.key )

这篇关于尝试使用jq shell脚本解析JSON数据时观察到语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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