Get values for a given key and its parent with jq
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I've got a JSON array like so:
"1":
"available_memory": 1086419656.0,
"available_memory_no_overbooking": 1086419656.0,
"conns": 1.0
,
"2":
"available_memory": 108641236.0,
"available_memory_no_overbooking": 10861216.0,
"conns": 2.0
I want to retrieve the value of "conns" atribute for each object id. I am new at jq and I cant found clear examples.
I have tried the following:
echo "$OUTPUT" | jq -r ..conns
Which returns all the values for conns, but thats is not what I needed. The spected output would be:
1 1.0
2 2.0
Any ideas?
json jq
New contributor
add a comment |
up vote
0
down vote
favorite
I've got a JSON array like so:
"1":
"available_memory": 1086419656.0,
"available_memory_no_overbooking": 1086419656.0,
"conns": 1.0
,
"2":
"available_memory": 108641236.0,
"available_memory_no_overbooking": 10861216.0,
"conns": 2.0
I want to retrieve the value of "conns" atribute for each object id. I am new at jq and I cant found clear examples.
I have tried the following:
echo "$OUTPUT" | jq -r ..conns
Which returns all the values for conns, but thats is not what I needed. The spected output would be:
1 1.0
2 2.0
Any ideas?
json jq
New contributor
There is no array in the data.
– Kusalananda
14 hours ago
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I've got a JSON array like so:
"1":
"available_memory": 1086419656.0,
"available_memory_no_overbooking": 1086419656.0,
"conns": 1.0
,
"2":
"available_memory": 108641236.0,
"available_memory_no_overbooking": 10861216.0,
"conns": 2.0
I want to retrieve the value of "conns" atribute for each object id. I am new at jq and I cant found clear examples.
I have tried the following:
echo "$OUTPUT" | jq -r ..conns
Which returns all the values for conns, but thats is not what I needed. The spected output would be:
1 1.0
2 2.0
Any ideas?
json jq
New contributor
I've got a JSON array like so:
"1":
"available_memory": 1086419656.0,
"available_memory_no_overbooking": 1086419656.0,
"conns": 1.0
,
"2":
"available_memory": 108641236.0,
"available_memory_no_overbooking": 10861216.0,
"conns": 2.0
I want to retrieve the value of "conns" atribute for each object id. I am new at jq and I cant found clear examples.
I have tried the following:
echo "$OUTPUT" | jq -r ..conns
Which returns all the values for conns, but thats is not what I needed. The spected output would be:
1 1.0
2 2.0
Any ideas?
json jq
json jq
New contributor
New contributor
edited 14 hours ago
Rui F Ribeiro
38.1k1475123
38.1k1475123
New contributor
asked 15 hours ago
A1t0r
1034
1034
New contributor
New contributor
There is no array in the data.
– Kusalananda
14 hours ago
add a comment |
There is no array in the data.
– Kusalananda
14 hours ago
There is no array in the data.
– Kusalananda
14 hours ago
There is no array in the data.
– Kusalananda
14 hours ago
add a comment |
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
$ jq -r 'keys as $k | "($k) (.[$k].conns)"' file.json
1 1
2 2
Seems like jq translates 1.0 to 1 and 2.0 to 2. Altering the input for clarity:
$ cat file.json
"1a":
"available_memory": 1086419656.0,
"available_memory_no_overbooking": 1086419656.0,
"conns": 1.1
,
"2b":
"available_memory": 108641236.0,
"available_memory_no_overbooking": 10861216.0,
"conns": 2.2
$ jq -r 'keys as $k | "($k) (.[$k].conns)"' file.json
1a 1.1
2b 2.2
Refs:
- https://stedolan.github.io/jq/manual/#Variable/SymbolicBindingOperator:...as$identifier|...
https://stedolan.github.io/jq/manual/#Stringinterpolation-(foo)
Thanks! Worked like a charm!
– A1t0r
15 hours ago
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
$ jq -r 'keys as $k | "($k) (.[$k].conns)"' file.json
1 1
2 2
Seems like jq translates 1.0 to 1 and 2.0 to 2. Altering the input for clarity:
$ cat file.json
"1a":
"available_memory": 1086419656.0,
"available_memory_no_overbooking": 1086419656.0,
"conns": 1.1
,
"2b":
"available_memory": 108641236.0,
"available_memory_no_overbooking": 10861216.0,
"conns": 2.2
$ jq -r 'keys as $k | "($k) (.[$k].conns)"' file.json
1a 1.1
2b 2.2
Refs:
- https://stedolan.github.io/jq/manual/#Variable/SymbolicBindingOperator:...as$identifier|...
https://stedolan.github.io/jq/manual/#Stringinterpolation-(foo)
Thanks! Worked like a charm!
– A1t0r
15 hours ago
add a comment |
up vote
3
down vote
accepted
$ jq -r 'keys as $k | "($k) (.[$k].conns)"' file.json
1 1
2 2
Seems like jq translates 1.0 to 1 and 2.0 to 2. Altering the input for clarity:
$ cat file.json
"1a":
"available_memory": 1086419656.0,
"available_memory_no_overbooking": 1086419656.0,
"conns": 1.1
,
"2b":
"available_memory": 108641236.0,
"available_memory_no_overbooking": 10861216.0,
"conns": 2.2
$ jq -r 'keys as $k | "($k) (.[$k].conns)"' file.json
1a 1.1
2b 2.2
Refs:
- https://stedolan.github.io/jq/manual/#Variable/SymbolicBindingOperator:...as$identifier|...
https://stedolan.github.io/jq/manual/#Stringinterpolation-(foo)
Thanks! Worked like a charm!
– A1t0r
15 hours ago
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
$ jq -r 'keys as $k | "($k) (.[$k].conns)"' file.json
1 1
2 2
Seems like jq translates 1.0 to 1 and 2.0 to 2. Altering the input for clarity:
$ cat file.json
"1a":
"available_memory": 1086419656.0,
"available_memory_no_overbooking": 1086419656.0,
"conns": 1.1
,
"2b":
"available_memory": 108641236.0,
"available_memory_no_overbooking": 10861216.0,
"conns": 2.2
$ jq -r 'keys as $k | "($k) (.[$k].conns)"' file.json
1a 1.1
2b 2.2
Refs:
- https://stedolan.github.io/jq/manual/#Variable/SymbolicBindingOperator:...as$identifier|...
https://stedolan.github.io/jq/manual/#Stringinterpolation-(foo)
$ jq -r 'keys as $k | "($k) (.[$k].conns)"' file.json
1 1
2 2
Seems like jq translates 1.0 to 1 and 2.0 to 2. Altering the input for clarity:
$ cat file.json
"1a":
"available_memory": 1086419656.0,
"available_memory_no_overbooking": 1086419656.0,
"conns": 1.1
,
"2b":
"available_memory": 108641236.0,
"available_memory_no_overbooking": 10861216.0,
"conns": 2.2
$ jq -r 'keys as $k | "($k) (.[$k].conns)"' file.json
1a 1.1
2b 2.2
Refs:
- https://stedolan.github.io/jq/manual/#Variable/SymbolicBindingOperator:...as$identifier|...
https://stedolan.github.io/jq/manual/#Stringinterpolation-(foo)
answered 15 hours ago
glenn jackman
49.2k469106
49.2k469106
Thanks! Worked like a charm!
– A1t0r
15 hours ago
add a comment |
Thanks! Worked like a charm!
– A1t0r
15 hours ago
Thanks! Worked like a charm!
– A1t0r
15 hours ago
Thanks! Worked like a charm!
– A1t0r
15 hours ago
add a comment |
A1t0r is a new contributor. Be nice, and check out our Code of Conduct.
A1t0r is a new contributor. Be nice, and check out our Code of Conduct.
A1t0r is a new contributor. Be nice, and check out our Code of Conduct.
A1t0r is a new contributor. Be nice, and check out our Code of Conduct.
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%2f481288%2fget-values-for-a-given-key-and-its-parent-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
There is no array in the data.
– Kusalananda
14 hours ago