jq无法使用包含破折号的键 [英] jq not working with key including dash

查看:90
本文介绍了jq无法使用包含破折号的键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个REST API,返回的内容如下:

I have a REST API, which returns something like this:

{
  "foo": 1,
  "bar": 2,
  "foo-bar": 3
}

当我执行`http/endpoint/url | jq'.foo-bar',它给出了以下错误:

when I do `http /endpoint/url | jq '.foo-bar', it gave the following error:

jq: error (at <stdin>:1): null (null) and boolean (true) cannot be subtracted

看来jq认为我正在尝试对foo-bar进行算术运算.

it looks like jq thinks I'm trying to do arithmetic operation with foo-bar.

如何正确形成这种路径?还是这是jq的错误?

How do I correctly form this kind of path? Or this is a bug of jq?

推荐答案

在JSON文本中,JSON密钥总是 双引号.也许您的REST API用双引号正确格式化了它,而您在上次编辑中的示例不正确.因为没有相同的jq不能将语法解析为有效的JSON.

In JSON text, JSON keys are always double-quoted. Perhaps your REST API was formatting it properly in double-quotes and your example in your last edit was incorrect. Because without the same jq cannot parse the syntax as a valid JSON.

就您所看到的问题而言,您需要将该字段放在引号中以让jq知道它是您正在访问的单个字段foo-bar而不是作为单独的字段

As far the issue you are seeing, you need to put the field within quotes to let jq know that it is a single field foo-bar you are accessing and not as separate fields

jq '."foo-bar"'

或更具体地说,将数组访问运算符用作jq '.["foo-bar"]'

Or more specifically use the array access operator as jq '.["foo-bar"]'

这篇关于jq无法使用包含破折号的键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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