My JSON input:
{ "Key": "Team", "Value": "AA" }
{ "Key": "Division", "Value": "BB" }
Desired output:
[
{ "Key": "Team", "Value": "AA" },
{ "Key": "Division", "Value": "BB" }
]
I cannot use the --slurp option because I'm in a middle of complex jq code.
The mentioned input is a result of my function and I need to convert it to an array for further processing.
The solution recommended in FAQ: https://github.com/stedolan/jq/wiki/FAQ#general-questions
cat json | jq 'reduce . as $i ([]; . + [$i])'
produces something different:
[
{ "Key": "Team", "Value": "AA" }
]
[
{ "Key": "Division", "Value": "BB" }
]
If your input really is irredeemably a stream, and if you cannot use the slurp option, then the simplest would probably be to use jq 1.5's inputs filter, along the lines of:
jq -n '[inputs]'
If you don't use the -n option, then the first JSON entity won't be seen by inputs. If you cannot use the -n option, then use [., inputs]
I think all you need is to use the jq array constructor syntax. I.e. enclose whatever complex expression you have that generates the stream in a pair of [ ]
Here is an example. For the sake of argument I'll use a function in place of whatever complex expression you have. e.g.
def some_complex_expression:
{ "Key": "Team", "Value": "AA" },
{ "Key": "Division", "Value": "BB" }
;
If we just execute this expression
some_complex_expression
the result is a stream of two objects as we expect:
{
"Key": "Team",
"Value": "AA"
}
{
"Key": "Division",
"Value": "BB"
}
But if we enclose the expression in [ ]
[ some_complex_expression ]
the result is a single array containing the objects from the stream
[
{
"Key": "Team",
"Value": "AA"
},
{
"Key": "Division",
"Value": "BB"
}
]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With