jq: translate array of objects to object
[EDIT: The following response was in answer to the question when it described (a) the mapping as shown below, and (b) the input data as having the form:
[ { "list": [ { "value": 1, "id1": 12 }, { "value": 15, "id2": 13 }, { "value": -4, "id3": 14 } ] }]
END OF EDIT]
In the following I'll assume that the mapping is available via the following function, but that is an inessential assumption:
def mapping: { "id1": "newId1", "id2": "newId2", "id3": "newId3"} ;
The following jq filter will then produce the desired output:
map( .list |= (map( to_entries[] | (mapping[.key]) as $mapped | select($mapped) | {($mapped|tostring): .value} ) | add) )
There's plenty of ways to skin a cat. I'd do it like this:
.[].list |= reduce .[] as $i ({}; ($i.id|tostring) as $k | (select($mapping | has($k))[$mapping[$k]] = $i.value) // .)
You would just provide the mapping through a separate file or argument.
$ cat program.jq.[].list |= reduce .[] as $i ({}; ($i.id|tostring) as $k | (select($mapping | has($k))[$mapping[$k]] = $i.value) // .)$ cat mapping.json{ "12": "newId1", "13": "newId2", "14": "newId3"}$ jq --argfile mapping mapping.json -f program.jq input.json[ { "list": { "newId1": 1, "newId2": 15, "newId3": -4 } }]
Here is a reduce-free solution to the revised problem.
In the following I'll assume that the mapping is available via the following function, but that is an inessential assumption:
def mapping:{ "12": "newId1", "13": "newId2", "14": "newId3"} ;map( .list |= (map( mapping[.id|tostring] as $mapped | select($mapped) | {($mapped): .value} ) | add) )
The "select" is for safety (i.e., it checks that the .id under consideration is indeed mapped). It might also be appropriate to ensure that $mapped
is a string by writing {($mapped|tostring): .value}
.