Goal is to search for null values of part_description and insert in part field value.
[
{
"part": "brake-01982",
"part_description": null,
}
]
Expected Output
[
{
"part": "brake-01982",
"part_description": "brake-01982",
}
]
Command:
jq ‘(.[] | select(.part_description==null).part_description) |= .part’
Results in no change
What does work is if I try to insert a string value. The double quoted string results in what I would expect. Demo below. How do I pass another fields value into this command vs a quoted string?
Command: jq ‘(.[] | select(.part_description==null).part_description) |= "test"’
Output
[
{
"part": "brake-01982",
"part_description": "test"
}
]
>Solution :
The alternative operator // takes on the second value if the first one is null, false, or empty (i.e. missing). Combined with the update operator |=, this can be contracted to //=.
jq '.[] |= (.part_description //= .part)'
[
{
"part": "brake-01982",
"part_description": "brake-01982"
}
]