jq update list elements based on values
up vote
-1
down vote
favorite
Is it possible to use jq
to turn the following JSON data
[
"a": null,
"b": [
"c": "cc",
"d": "dd1"
,
"c": "cc",
"d": "dd1",
"e": "ee",
"f": "ff"
]
,
"b": [
"c": "cc",
"d": "dd2",
"e": "ee",
"f": "ff"
]
]
into
[
"a": null,
"b": [
[
"cc", "d1"
],
[
"cc", "d1", "ff"
]
]
,
"b": [
[
"cc", "d2", "ff"
]
]
]
?
Note that the purpose is to reduce the b
list with certain elements of its items based on a condition. The condition assigns the string d1
if the value of d
is dd1
, otherwise d2
is assigned if dd2
is present.
The following unsuccessful attempt demonstrates the idea:
$ jq -r '..b = [..b.c, ?, ..b.f?]'
json jq
add a comment |
up vote
-1
down vote
favorite
Is it possible to use jq
to turn the following JSON data
[
"a": null,
"b": [
"c": "cc",
"d": "dd1"
,
"c": "cc",
"d": "dd1",
"e": "ee",
"f": "ff"
]
,
"b": [
"c": "cc",
"d": "dd2",
"e": "ee",
"f": "ff"
]
]
into
[
"a": null,
"b": [
[
"cc", "d1"
],
[
"cc", "d1", "ff"
]
]
,
"b": [
[
"cc", "d2", "ff"
]
]
]
?
Note that the purpose is to reduce the b
list with certain elements of its items based on a condition. The condition assigns the string d1
if the value of d
is dd1
, otherwise d2
is assigned if dd2
is present.
The following unsuccessful attempt demonstrates the idea:
$ jq -r '..b = [..b.c, ?, ..b.f?]'
json jq
what have you tried so far?
– oguzismail
Nov 11 at 16:09
@oguzismail See the updated question. I am unaware how to address the conditional part and how to update each element individually.
– dfernan
Nov 11 at 16:34
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
Is it possible to use jq
to turn the following JSON data
[
"a": null,
"b": [
"c": "cc",
"d": "dd1"
,
"c": "cc",
"d": "dd1",
"e": "ee",
"f": "ff"
]
,
"b": [
"c": "cc",
"d": "dd2",
"e": "ee",
"f": "ff"
]
]
into
[
"a": null,
"b": [
[
"cc", "d1"
],
[
"cc", "d1", "ff"
]
]
,
"b": [
[
"cc", "d2", "ff"
]
]
]
?
Note that the purpose is to reduce the b
list with certain elements of its items based on a condition. The condition assigns the string d1
if the value of d
is dd1
, otherwise d2
is assigned if dd2
is present.
The following unsuccessful attempt demonstrates the idea:
$ jq -r '..b = [..b.c, ?, ..b.f?]'
json jq
Is it possible to use jq
to turn the following JSON data
[
"a": null,
"b": [
"c": "cc",
"d": "dd1"
,
"c": "cc",
"d": "dd1",
"e": "ee",
"f": "ff"
]
,
"b": [
"c": "cc",
"d": "dd2",
"e": "ee",
"f": "ff"
]
]
into
[
"a": null,
"b": [
[
"cc", "d1"
],
[
"cc", "d1", "ff"
]
]
,
"b": [
[
"cc", "d2", "ff"
]
]
]
?
Note that the purpose is to reduce the b
list with certain elements of its items based on a condition. The condition assigns the string d1
if the value of d
is dd1
, otherwise d2
is assigned if dd2
is present.
The following unsuccessful attempt demonstrates the idea:
$ jq -r '..b = [..b.c, ?, ..b.f?]'
json jq
json jq
edited Nov 11 at 16:32
asked Nov 11 at 15:39
dfernan
215716
215716
what have you tried so far?
– oguzismail
Nov 11 at 16:09
@oguzismail See the updated question. I am unaware how to address the conditional part and how to update each element individually.
– dfernan
Nov 11 at 16:34
add a comment |
what have you tried so far?
– oguzismail
Nov 11 at 16:09
@oguzismail See the updated question. I am unaware how to address the conditional part and how to update each element individually.
– dfernan
Nov 11 at 16:34
what have you tried so far?
– oguzismail
Nov 11 at 16:09
what have you tried so far?
– oguzismail
Nov 11 at 16:09
@oguzismail See the updated question. I am unaware how to address the conditional part and how to update each element individually.
– dfernan
Nov 11 at 16:34
@oguzismail See the updated question. I am unaware how to address the conditional part and how to update each element individually.
– dfernan
Nov 11 at 16:34
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
This is probably not the best way to do what you want, but it does get the desired output and it has the conditional you asked about.
jq '..b |= map(
[ .c,
(if .d == "dd1" then "d1" elif .d == "dd2" then "d2" else . end),
.f // empty
] )'
add a comment |
up vote
0
down vote
Without changing the value of the d
key, you would use this jq filter:
jq '..b|=(to_entries|map(.value))' file
That updates the b
array into all values of the inner objects.
If you want to update the d
, you could use this filter:
jq '..b |= (to_entries|
map(
if(.key=="d") then
.value|=sub("d+";"d")
else .
end
|.value)
)' file
that adds a check if the key is d
the associated value is updated to remove the duplicated d
character from the string. This requires jq to support for regex (to be able to use the sub
function).
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
This is probably not the best way to do what you want, but it does get the desired output and it has the conditional you asked about.
jq '..b |= map(
[ .c,
(if .d == "dd1" then "d1" elif .d == "dd2" then "d2" else . end),
.f // empty
] )'
add a comment |
up vote
0
down vote
This is probably not the best way to do what you want, but it does get the desired output and it has the conditional you asked about.
jq '..b |= map(
[ .c,
(if .d == "dd1" then "d1" elif .d == "dd2" then "d2" else . end),
.f // empty
] )'
add a comment |
up vote
0
down vote
up vote
0
down vote
This is probably not the best way to do what you want, but it does get the desired output and it has the conditional you asked about.
jq '..b |= map(
[ .c,
(if .d == "dd1" then "d1" elif .d == "dd2" then "d2" else . end),
.f // empty
] )'
This is probably not the best way to do what you want, but it does get the desired output and it has the conditional you asked about.
jq '..b |= map(
[ .c,
(if .d == "dd1" then "d1" elif .d == "dd2" then "d2" else . end),
.f // empty
] )'
answered Nov 11 at 19:10
user197693
50223
50223
add a comment |
add a comment |
up vote
0
down vote
Without changing the value of the d
key, you would use this jq filter:
jq '..b|=(to_entries|map(.value))' file
That updates the b
array into all values of the inner objects.
If you want to update the d
, you could use this filter:
jq '..b |= (to_entries|
map(
if(.key=="d") then
.value|=sub("d+";"d")
else .
end
|.value)
)' file
that adds a check if the key is d
the associated value is updated to remove the duplicated d
character from the string. This requires jq to support for regex (to be able to use the sub
function).
add a comment |
up vote
0
down vote
Without changing the value of the d
key, you would use this jq filter:
jq '..b|=(to_entries|map(.value))' file
That updates the b
array into all values of the inner objects.
If you want to update the d
, you could use this filter:
jq '..b |= (to_entries|
map(
if(.key=="d") then
.value|=sub("d+";"d")
else .
end
|.value)
)' file
that adds a check if the key is d
the associated value is updated to remove the duplicated d
character from the string. This requires jq to support for regex (to be able to use the sub
function).
add a comment |
up vote
0
down vote
up vote
0
down vote
Without changing the value of the d
key, you would use this jq filter:
jq '..b|=(to_entries|map(.value))' file
That updates the b
array into all values of the inner objects.
If you want to update the d
, you could use this filter:
jq '..b |= (to_entries|
map(
if(.key=="d") then
.value|=sub("d+";"d")
else .
end
|.value)
)' file
that adds a check if the key is d
the associated value is updated to remove the duplicated d
character from the string. This requires jq to support for regex (to be able to use the sub
function).
Without changing the value of the d
key, you would use this jq filter:
jq '..b|=(to_entries|map(.value))' file
That updates the b
array into all values of the inner objects.
If you want to update the d
, you could use this filter:
jq '..b |= (to_entries|
map(
if(.key=="d") then
.value|=sub("d+";"d")
else .
end
|.value)
)' file
that adds a check if the key is d
the associated value is updated to remove the duplicated d
character from the string. This requires jq to support for regex (to be able to use the sub
function).
answered Nov 12 at 9:10
oliv
8,2281130
8,2281130
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250318%2fjq-update-list-elements-based-on-values%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
what have you tried so far?
– oguzismail
Nov 11 at 16:09
@oguzismail See the updated question. I am unaware how to address the conditional part and how to update each element individually.
– dfernan
Nov 11 at 16:34