Passing bash variable to jq

I have written a script to retrieve certain value from file.json. It works if I provide the value to jq select, but the variable doesn’t seem to work (or I don’t know how to use it). #!/bin/sh #this works *** projectID=$(cat file.json | jq -r ‘.resource[] | select(.username==”[email protected]”) | .id’) echo “$projectID” [email protected] #this does … Read more

How to merge 2 JSON objects from 2 files using jq?

I’m using the jq tools (jq-json-processor) in shell script to parse json. I’ve got 2 json files and want to merge them into one unique file Here the content of files: file1 { “value1”: 200, “timestamp”: 1382461861, “value”: { “aaa”: { “value1”: “v1”, “value2”: “v2” }, “bbb”: { “value1”: “v1”, “value2”: “v2” }, “ccc”: { … Read more

How to use `jq` in a shell pipeline?

I can’t seem to get jq to behave “normally” in a shell pipeline. For example: $ curl -s https://api.github.com/users/octocat/repos | jq | cat results in jq simply printing out its help text*. The same thing happens if I try to redirect jq‘s output to a file: $ curl -s https://api.github.com/users/octocat/repos | jq > /tmp/stuff.json Is … Read more

How to count items in JSON object using command line?

I’m getting this kind of JSON reply from a curl command: [ { “cid”: 49, “pyn”: “yi4”, “hans”: “亿”, “hant”: “億”, “tid”: 68, “l10n”: “cent million”, “pid”: 1, “pos”: “num”, “pos_txt”: “” }, { “cid”: 50, “pyn”: “yi4”, “hans”: “亿”, “hant”: “億”, “tid”: 69, “l10n”: “100 millions”, “pid”: 1, “pos”: “num”, “pos_txt”: “” } ] … Read more

How to filter an array of objects based on values in an inner array with jq?

Given this input: [ { “Id”: “cb94e7a42732b598ad18a8f27454a886c1aa8bbba6167646d8f064cd86191e2b”, “Names”: [ “condescending_jones”, “loving_hoover” ] }, { “Id”: “186db739b7509eb0114a09e14bcd16bf637019860d23c4fc20e98cbe068b55aa”, “Names”: [ “foo_data” ] }, { “Id”: “a4b7e6f5752d8dcb906a5901f7ab82e403b9dff4eaaeebea767a04bac4aada19”, “Names”: [ “jovial_wozniak” ] }, { “Id”: “76b71c496556912012c20dc3cbd37a54a1f05bffad3d5e92466900a003fbb623”, “Names”: [ “bar_data” ] } ] I’m trying to construct a filter with jq that returns all objects with Ids that do not … Read more

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

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” … Read more

Select objects based on value of variable in object using jq

I have the following json file: { “FOO”: { “name”: “Donald”, “location”: “Stockholm” }, “BAR”: { “name”: “Walt”, “location”: “Stockholm” }, “BAZ”: { “name”: “Jack”, “location”: “Whereever” } } I am using jq and want to get the “name” elements of the objects where ‘location’ is ‘Stockholm’. I know I can get all names by … Read more

How to remove double-quotes in jq output for parsing json files in bash?

I’m using jq to parse a JSON file as shown here. However, the results for string values contain the “double-quotes” as expected, as shown below: $ cat json.txt | jq ‘.name’ “Google” How can I pipe this into another command to remove the “”? so I get $ cat json.txt | jq ‘.name’ | some_other_command … Read more