使用jq对一个json中的多个字段进行串行解析和显示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28164849/
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 17:43:27  来源:igfitidea点击:

Using jq to parse and display multiple fields in a json serially

jsonjq

提问by San

I have this Json

我有这个 Json

{
    "users": [
        {
            "first": "Stevie",
            "last": "Wonder"
        },
        {
            "first": "Michael",
            "last": "Hymanson"
        }
    ]
}

Using jq I'd like to display first and last name serially. Like so -

使用 jq 我想连续显示名字和姓氏。像这样——

Stevie Wonder
Michael Hymanson

This is how far I have gotten -

这是我得到的程度 -

jq '.users[].first, .users[].last'

But it displays

但它显示

"Stevie"
"Michael"
"Wonder"
"Hymanson"

Notice the following -

请注意以下事项——

  1. The double quotes that I do not want.
  2. The carriage return that I do not want.
  3. It's jumbled up. My query displays all the first names first, and then all the last names. However, I want first-last, first-last pair.
  1. 我不想要的双引号。
  2. 我不要的回车。
  3. 乱七八糟了 我的查询首先显示所有名字,然后显示所有姓氏。但是,我想要倒数第一对。

回答by Eric Hartford

I recommend using String Interpolation:

我建议使用字符串插值:

jq '.users[] | "\(.first) \(.last)"'

reference

参考

回答by abraham

You can use additionto concatenate strings.

您可以使用加法来连接字符串。

Stringsare added by being joined into a larger string.

字符串是通过连接成一个更大的字符串来添加的。

jq '.users[] | .first + " " + .last'

The above works when both firstand lastare string. If you are extracting different datatypes(number and string), then we need to convert to equivalent types. Referring to solution on this question. For example.

firstlast都是字符串时,上述方法有效。如果您要提取不同的数据类型(数字和字符串),那么我们需要转换为等效类型。参考这个问题的解决方案。例如。

jq '.users[] | .first + " " + (.number|tostring)'

回答by optman

jq '.users[]|.first,.last' | paste - -

回答by machzqcq

While both of the above answers work well if key,value are strings, I had a situation to append a string and integer (jq errors using the above expressions)

虽然如果 key,value 是字符串,上述两个答案都可以很好地工作,但我遇到了附加字符串和整数的情况(使用上述表达式的 jq 错误)

Requirement: To construct a url out below json

要求:在json下面构造一个url

pradeep@seleniumframework>curl http://192.168.99.103:8500/v1/catalog/service/apache-443 | jq .[0]
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   251  100   251    0     0   155k      0 --:--:-- --:--:-- --:--:--  245k
{
  "Node": "myconsul",
  "Address": "192.168.99.103",
  "ServiceID": "4ce41e90ede4:compassionate_wozniak:443",
  "ServiceName": "apache-443",
  "ServiceTags": [],
  "ServiceAddress": "",
  "ServicePort": 1443,
  "ServiceEnableTagOverride": false,
  "CreateIndex": 45,
  "ModifyIndex": 45
}

Solution:

解决方案:

curl http://192.168.99.103:8500/v1/catalog/service/apache-443 |
jq '.[0] | "http://" + .Address + ":" + "\(.ServicePort)"'

回答by TinyRoy

This will produce an array of names

这将产生一个名称数组

> jq '[ .users[] | (.first + " " + .last) ]' ~/test.json

[
  "Stevie Wonder",
  "Michael Hymanson"
]

回答by ThorSummoner

I got pretty close to what I wanted by doing something like this

通过做这样的事情,我非常接近我想要的

cat my.json | jq '.my.prefix[] | .primary_key + ":", (.sub.prefix[] | "    - " + .sub_key)' | tr -d '"' 

The output of which is close enough to yaml for me to usually import it into other tools without much problem. (I am still looking for a way to basicallt export a subset of the input json)

其输出与 yaml 足够接近,我通常可以将其导入其他工具而不会出现太大问题。(我仍在寻找一种方法来基本导出输入 json 的子集)

回答by Ganesh Chandrasekaran

my approach will be (your json example is not well formed.. guess thats only a sample)

我的方法将是(您的 json 示例格式不正确.. 猜测那只是一个示例)

jq '.Front[] | [.Name,.Out,.In,.Groups] | join("|")'  front.json  > output.txt

returns something like this

返回这样的东西

"new.domain.com-80|8.8.8.8|192.168.2.2:80|192.168.3.29:80 192.168.3.30:80"
"new.domain.com -443|8.8.8.8|192.168.2.2:443|192.168.3.29:443 192.168.3.30:443"

and grep the output with regular expression.

并使用正则表达式 grep 输出。