Reputation: 55
I have the following input json for jq:
{
"records": [
{
"uuid": "14e20b5a-6619-11ec-a838-0050568168f7",
"nas": {
"path": "/data/nlp/datasets/ds0"
}
},
{
"uuid": "799ce816-656c-11ec-a838-0050568168f7",
"nas": {
"path": "/data/nlp/datasets/ds1"
}
}
]
}
What is the reason the following jq duplicate the results:
[{junction:.records[].nas.path}+{uuid:.records[].uuid}]
Result:
[
{
"junction": "/data/nlp/datasets/ds0",
"uuid": "14e20b5a-6619-11ec-a838-0050568168f7"
},
{
"junction": "/data/nlp/datasets/ds1",
"uuid": "14e20b5a-6619-11ec-a838-0050568168f7"
},
{
"junction": "/data/nlp/datasets/ds0",
"uuid": "799ce816-656c-11ec-a838-0050568168f7"
},
{
"junction": "/data/nlp/datasets/ds1",
"uuid": "799ce816-656c-11ec-a838-0050568168f7"
}
]
Upvotes: 1
Views: 1024
Reputation: 36033
Both {junction:.records[].nas.path}
and {uuid:.records[].uuid}
produce a stream of two objects each due to the iteration caused by .records[]
. Adding up the cartesian product, this totals to four objects.
If you want to iterate jut once, pull out the iterator .records[]
up front to define a common input context for the subsequent filter:
.records[] | {junction:.nas.path} + {uuid:.uuid}
This can then further be simplified to
.records[] | {junction:.nas.path, uuid}
{
"junction": "/data/nlp/datasets/ds0",
"uuid": "14e20b5a-6619-11ec-a838-0050568168f7"
}
{
"junction": "/data/nlp/datasets/ds1",
"uuid": "799ce816-656c-11ec-a838-0050568168f7"
}
which is presumably what you were looking for.
Upvotes: 3