I have this Json
{
"users": [
{
"first": "Stevie",
"last": "Wonder"
},
{
"first": "Michael",
"last": "Jackson"
}
]
}
Using jq I'd like to display first and last name serially. Like so -
Stevie Wonder
Michael Jackson
This is how far I have gotten -
jq '.users[].first, .users[].last'
But it displays
"Stevie"
"Michael"
"Wonder"
"Jackson"
Notice the following -
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)
Requirement: To construct a url out below json
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)"'
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)
jq '.users[]|.first,.last' | paste - -
You can use addition to concatenate strings.
Strings are added by being joined into a larger string.
jq '.users[] | .first + " " + .last'
The above works when both first
and last
are 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.
jq '.users[] | .first + " " + (.number|tostring)'
This will produce an array of names
> jq '[ .users[] | (.first + " " + .last) ]' ~/test.json
[
"Stevie Wonder",
"Michael Jackson"
]
my approach will be (your json example is not well formed.. guess thats only a sample)
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.
Source: Stackoverflow.com