Parsing JSON with JQ

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












I'm using Bash to run the following script snippet on a Linux box.



JSON file contents: [ "id": 123456, "firstName": "John", "lastName": "Smith", "email": "user@domain.com" ]





The JSON file is stored in the $data[0] array which is piped into the Bash script.





Bash script:



trafficEmployeeId=123456
cat "$data[0]" | jq --arg employeeId $trafficEmployeeId '. | select(.id == $employeeId) | .firstName'





And the output from the script should be John. But I get nothing.







share|improve this question


















  • 1




    show your $trafficEmployeeId value
    – RomanPerekhrest
    Oct 20 '17 at 16:23














up vote
1
down vote

favorite












I'm using Bash to run the following script snippet on a Linux box.



JSON file contents: [ "id": 123456, "firstName": "John", "lastName": "Smith", "email": "user@domain.com" ]





The JSON file is stored in the $data[0] array which is piped into the Bash script.





Bash script:



trafficEmployeeId=123456
cat "$data[0]" | jq --arg employeeId $trafficEmployeeId '. | select(.id == $employeeId) | .firstName'





And the output from the script should be John. But I get nothing.







share|improve this question


















  • 1




    show your $trafficEmployeeId value
    – RomanPerekhrest
    Oct 20 '17 at 16:23












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I'm using Bash to run the following script snippet on a Linux box.



JSON file contents: [ "id": 123456, "firstName": "John", "lastName": "Smith", "email": "user@domain.com" ]





The JSON file is stored in the $data[0] array which is piped into the Bash script.





Bash script:



trafficEmployeeId=123456
cat "$data[0]" | jq --arg employeeId $trafficEmployeeId '. | select(.id == $employeeId) | .firstName'





And the output from the script should be John. But I get nothing.







share|improve this question














I'm using Bash to run the following script snippet on a Linux box.



JSON file contents: [ "id": 123456, "firstName": "John", "lastName": "Smith", "email": "user@domain.com" ]





The JSON file is stored in the $data[0] array which is piped into the Bash script.





Bash script:



trafficEmployeeId=123456
cat "$data[0]" | jq --arg employeeId $trafficEmployeeId '. | select(.id == $employeeId) | .firstName'





And the output from the script should be John. But I get nothing.









share|improve this question













share|improve this question




share|improve this question








edited Oct 20 '17 at 17:23









Jeff Schaller

32.1k849109




32.1k849109










asked Oct 20 '17 at 16:21









Berni

318




318







  • 1




    show your $trafficEmployeeId value
    – RomanPerekhrest
    Oct 20 '17 at 16:23












  • 1




    show your $trafficEmployeeId value
    – RomanPerekhrest
    Oct 20 '17 at 16:23







1




1




show your $trafficEmployeeId value
– RomanPerekhrest
Oct 20 '17 at 16:23




show your $trafficEmployeeId value
– RomanPerekhrest
Oct 20 '17 at 16:23










1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










Even if you assigned shell variable trafficEmployeeId with a number it will be passed into jq script as a string argument.
The solution is to parse the argument as a number with jq's tonumber function.

The second moment is that data[0] contains the array with only one object, so it's enough to access it directly with .[0] and apply simple if operator condition.



Complete solution:



trafficEmployeeId=123456
echo "$data[0]" | jq --arg employeeId "$trafficEmployeeId" '.[0]
| if .id == ($employeeId | tonumber) then .firstName else empty end'


The output:



"John"





share|improve this answer




















  • On newer versions of jq, one could also use --argjson instead of tonumber.
    – dhag
    Oct 20 '17 at 16:47










  • Thanks for the advice. I'll try this solution and get back to you.
    – Berni
    Oct 20 '17 at 18:53










  • Your answer led me to the correct solution. Thanks a lot!
    – Berni
    Oct 20 '17 at 19:23











  • @Berni, you're welcome
    – RomanPerekhrest
    Oct 20 '17 at 19:43










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f399382%2fparsing-json-with-jq%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










Even if you assigned shell variable trafficEmployeeId with a number it will be passed into jq script as a string argument.
The solution is to parse the argument as a number with jq's tonumber function.

The second moment is that data[0] contains the array with only one object, so it's enough to access it directly with .[0] and apply simple if operator condition.



Complete solution:



trafficEmployeeId=123456
echo "$data[0]" | jq --arg employeeId "$trafficEmployeeId" '.[0]
| if .id == ($employeeId | tonumber) then .firstName else empty end'


The output:



"John"





share|improve this answer




















  • On newer versions of jq, one could also use --argjson instead of tonumber.
    – dhag
    Oct 20 '17 at 16:47










  • Thanks for the advice. I'll try this solution and get back to you.
    – Berni
    Oct 20 '17 at 18:53










  • Your answer led me to the correct solution. Thanks a lot!
    – Berni
    Oct 20 '17 at 19:23











  • @Berni, you're welcome
    – RomanPerekhrest
    Oct 20 '17 at 19:43














up vote
2
down vote



accepted










Even if you assigned shell variable trafficEmployeeId with a number it will be passed into jq script as a string argument.
The solution is to parse the argument as a number with jq's tonumber function.

The second moment is that data[0] contains the array with only one object, so it's enough to access it directly with .[0] and apply simple if operator condition.



Complete solution:



trafficEmployeeId=123456
echo "$data[0]" | jq --arg employeeId "$trafficEmployeeId" '.[0]
| if .id == ($employeeId | tonumber) then .firstName else empty end'


The output:



"John"





share|improve this answer




















  • On newer versions of jq, one could also use --argjson instead of tonumber.
    – dhag
    Oct 20 '17 at 16:47










  • Thanks for the advice. I'll try this solution and get back to you.
    – Berni
    Oct 20 '17 at 18:53










  • Your answer led me to the correct solution. Thanks a lot!
    – Berni
    Oct 20 '17 at 19:23











  • @Berni, you're welcome
    – RomanPerekhrest
    Oct 20 '17 at 19:43












up vote
2
down vote



accepted







up vote
2
down vote



accepted






Even if you assigned shell variable trafficEmployeeId with a number it will be passed into jq script as a string argument.
The solution is to parse the argument as a number with jq's tonumber function.

The second moment is that data[0] contains the array with only one object, so it's enough to access it directly with .[0] and apply simple if operator condition.



Complete solution:



trafficEmployeeId=123456
echo "$data[0]" | jq --arg employeeId "$trafficEmployeeId" '.[0]
| if .id == ($employeeId | tonumber) then .firstName else empty end'


The output:



"John"





share|improve this answer












Even if you assigned shell variable trafficEmployeeId with a number it will be passed into jq script as a string argument.
The solution is to parse the argument as a number with jq's tonumber function.

The second moment is that data[0] contains the array with only one object, so it's enough to access it directly with .[0] and apply simple if operator condition.



Complete solution:



trafficEmployeeId=123456
echo "$data[0]" | jq --arg employeeId "$trafficEmployeeId" '.[0]
| if .id == ($employeeId | tonumber) then .firstName else empty end'


The output:



"John"






share|improve this answer












share|improve this answer



share|improve this answer










answered Oct 20 '17 at 16:39









RomanPerekhrest

22.5k12145




22.5k12145











  • On newer versions of jq, one could also use --argjson instead of tonumber.
    – dhag
    Oct 20 '17 at 16:47










  • Thanks for the advice. I'll try this solution and get back to you.
    – Berni
    Oct 20 '17 at 18:53










  • Your answer led me to the correct solution. Thanks a lot!
    – Berni
    Oct 20 '17 at 19:23











  • @Berni, you're welcome
    – RomanPerekhrest
    Oct 20 '17 at 19:43
















  • On newer versions of jq, one could also use --argjson instead of tonumber.
    – dhag
    Oct 20 '17 at 16:47










  • Thanks for the advice. I'll try this solution and get back to you.
    – Berni
    Oct 20 '17 at 18:53










  • Your answer led me to the correct solution. Thanks a lot!
    – Berni
    Oct 20 '17 at 19:23











  • @Berni, you're welcome
    – RomanPerekhrest
    Oct 20 '17 at 19:43















On newer versions of jq, one could also use --argjson instead of tonumber.
– dhag
Oct 20 '17 at 16:47




On newer versions of jq, one could also use --argjson instead of tonumber.
– dhag
Oct 20 '17 at 16:47












Thanks for the advice. I'll try this solution and get back to you.
– Berni
Oct 20 '17 at 18:53




Thanks for the advice. I'll try this solution and get back to you.
– Berni
Oct 20 '17 at 18:53












Your answer led me to the correct solution. Thanks a lot!
– Berni
Oct 20 '17 at 19:23





Your answer led me to the correct solution. Thanks a lot!
– Berni
Oct 20 '17 at 19:23













@Berni, you're welcome
– RomanPerekhrest
Oct 20 '17 at 19:43




@Berni, you're welcome
– RomanPerekhrest
Oct 20 '17 at 19:43

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f399382%2fparsing-json-with-jq%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay