I’m trying to transform array to object by specific key. It works fine without using stream, but not possible when stream is applied.
Data:
[
{
"id": "1",
"userId": "fa51531d"
}
,
{
"id": "2",
"userId": "a167869a"
}
]
I tried running this command but it throws an error.
jq -n –stream ‘fromstream(1|truncate_stream(inputs)) | INDEX(.id)’ test.json > result.json
Data above should be transformed to:
{
"1": {
"userId": "fa51531d",
"id": "1"
},
"2": {
"userId": "a167869a",
"id": "2"
},
}
I want to achieve the same result as with jq 'INDEX(.id)
but I need to use stream (because of big JSON file).
3
Answers
To transform your JSON Array to JSON Object maybe you can use this
but if you want to stream the JSON, i don’t have the solutions
cmiiw
If your stream really looks like in your question, this should do:
Output:
If you are trying to recreate the whole input object, the stream-based approach is rendered pointless. That said, using this approach, there’s no need to truncate. So either replace
1
with0
:Or just omit it entirely (which reveals its futility):
What would make more sense, is to output a stream of objects, each indexed as with
INDEX
. Maybe you were looking for this: