Parsing JSON with JQ
Clash 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.
shell-script json jq
add a comment |Â
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.
shell-script json jq
1
show your$trafficEmployeeId
value
â RomanPerekhrest
Oct 20 '17 at 16:23
add a comment |Â
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.
shell-script json jq
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.
shell-script json jq
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
add a comment |Â
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
add a comment |Â
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"
On newer versions of jq, one could also use--argjson
instead oftonumber
.
â 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
add a comment |Â
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"
On newer versions of jq, one could also use--argjson
instead oftonumber
.
â 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
add a comment |Â
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"
On newer versions of jq, one could also use--argjson
instead oftonumber
.
â 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
add a comment |Â
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"
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"
answered Oct 20 '17 at 16:39
RomanPerekhrest
22.5k12145
22.5k12145
On newer versions of jq, one could also use--argjson
instead oftonumber
.
â 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
add a comment |Â
On newer versions of jq, one could also use--argjson
instead oftonumber
.
â 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
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
show your
$trafficEmployeeId
valueâ RomanPerekhrest
Oct 20 '17 at 16:23