How can I combine a sequence of JSON with jq without using the slurp flag?
up vote
0
down vote
favorite
I have a ton of records (~4,500) that I've processed (using jq) down to a sequence of JSON grouped by hourly UTC time (~680 groups, all unique).
{
"2018-10-09T19:00:00.000Z":
}
{
"2018-10-09T20:00:00.000Z":
}
{
"2018-10-09T21:00:00.000Z":
}
I'm pretty sure you can see where this is going, but I want to combine all these into a single JSON object to hand over to another system for more fun.
{
"2018-10-09T19:00:00.000Z": ,
"2018-10-09T20:00:00.000Z": ,
"2018-10-09T21:00:00.000Z":
}
The last two things I'm doing before I get to the sequence of objects is:
group_by(.day) | { (.[0].day): . }
Where .day
is the ISO Date you see referenced above.
I've tried a few things around map
and reduce
functions, but can't seem to massage the data the way I want. I've spent a few hours on this and need to take a break, so any help or direction you can point me would be great!
javascript node.js json jq
add a comment |
up vote
0
down vote
favorite
I have a ton of records (~4,500) that I've processed (using jq) down to a sequence of JSON grouped by hourly UTC time (~680 groups, all unique).
{
"2018-10-09T19:00:00.000Z":
}
{
"2018-10-09T20:00:00.000Z":
}
{
"2018-10-09T21:00:00.000Z":
}
I'm pretty sure you can see where this is going, but I want to combine all these into a single JSON object to hand over to another system for more fun.
{
"2018-10-09T19:00:00.000Z": ,
"2018-10-09T20:00:00.000Z": ,
"2018-10-09T21:00:00.000Z":
}
The last two things I'm doing before I get to the sequence of objects is:
group_by(.day) | { (.[0].day): . }
Where .day
is the ISO Date you see referenced above.
I've tried a few things around map
and reduce
functions, but can't seem to massage the data the way I want. I've spent a few hours on this and need to take a break, so any help or direction you can point me would be great!
javascript node.js json jq
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a ton of records (~4,500) that I've processed (using jq) down to a sequence of JSON grouped by hourly UTC time (~680 groups, all unique).
{
"2018-10-09T19:00:00.000Z":
}
{
"2018-10-09T20:00:00.000Z":
}
{
"2018-10-09T21:00:00.000Z":
}
I'm pretty sure you can see where this is going, but I want to combine all these into a single JSON object to hand over to another system for more fun.
{
"2018-10-09T19:00:00.000Z": ,
"2018-10-09T20:00:00.000Z": ,
"2018-10-09T21:00:00.000Z":
}
The last two things I'm doing before I get to the sequence of objects is:
group_by(.day) | { (.[0].day): . }
Where .day
is the ISO Date you see referenced above.
I've tried a few things around map
and reduce
functions, but can't seem to massage the data the way I want. I've spent a few hours on this and need to take a break, so any help or direction you can point me would be great!
javascript node.js json jq
I have a ton of records (~4,500) that I've processed (using jq) down to a sequence of JSON grouped by hourly UTC time (~680 groups, all unique).
{
"2018-10-09T19:00:00.000Z":
}
{
"2018-10-09T20:00:00.000Z":
}
{
"2018-10-09T21:00:00.000Z":
}
I'm pretty sure you can see where this is going, but I want to combine all these into a single JSON object to hand over to another system for more fun.
{
"2018-10-09T19:00:00.000Z": ,
"2018-10-09T20:00:00.000Z": ,
"2018-10-09T21:00:00.000Z":
}
The last two things I'm doing before I get to the sequence of objects is:
group_by(.day) | { (.[0].day): . }
Where .day
is the ISO Date you see referenced above.
I've tried a few things around map
and reduce
functions, but can't seem to massage the data the way I want. I've spent a few hours on this and need to take a break, so any help or direction you can point me would be great!
javascript node.js json jq
javascript node.js json jq
asked Nov 21 at 21:33
Sam Bantner
9018
9018
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
If everything is already in memory, you could modify the group_by
line as follows:
reduce group_by(.day) as $in ({}; . + { ($in[0].day): $in }
Alternatives to group_by
Since group_by
entails a sort, it may be unnecessarily inefficient. You might like to consider using a variant such as the following:
# sort-free variant of group_by/1
# f must always evaluate to an integer or always to a string.
# Output: an array in the former case, or an object in the latter case
def GROUP_BY(f): reduce . as $x ({}; .[$x|f] += [$x] );
Everything is in memory and this is a very self-explanatory answer. Thanks for the help! I didn't think to bounce reducing up one level, which completely makes sense as my brain is clear :)
– Sam Bantner
Nov 23 at 13:11
add a comment |
up vote
0
down vote
If the stream of objects is already in a file, use inputs
with the -n command-line option.
This will avoid the overhead of "slurping" but will still require enough RAM for the entire result to fit into memory. If that doesn't work for you, then you will have to resort to desperate measures :-)
This might be a useful starting point:
jq -n 'reduce inputs as $in ({}; . + $in)'
I think you could also pipe the output as an array intoadd
.
– mustachioed
Nov 21 at 22:00
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
If everything is already in memory, you could modify the group_by
line as follows:
reduce group_by(.day) as $in ({}; . + { ($in[0].day): $in }
Alternatives to group_by
Since group_by
entails a sort, it may be unnecessarily inefficient. You might like to consider using a variant such as the following:
# sort-free variant of group_by/1
# f must always evaluate to an integer or always to a string.
# Output: an array in the former case, or an object in the latter case
def GROUP_BY(f): reduce . as $x ({}; .[$x|f] += [$x] );
Everything is in memory and this is a very self-explanatory answer. Thanks for the help! I didn't think to bounce reducing up one level, which completely makes sense as my brain is clear :)
– Sam Bantner
Nov 23 at 13:11
add a comment |
up vote
1
down vote
accepted
If everything is already in memory, you could modify the group_by
line as follows:
reduce group_by(.day) as $in ({}; . + { ($in[0].day): $in }
Alternatives to group_by
Since group_by
entails a sort, it may be unnecessarily inefficient. You might like to consider using a variant such as the following:
# sort-free variant of group_by/1
# f must always evaluate to an integer or always to a string.
# Output: an array in the former case, or an object in the latter case
def GROUP_BY(f): reduce . as $x ({}; .[$x|f] += [$x] );
Everything is in memory and this is a very self-explanatory answer. Thanks for the help! I didn't think to bounce reducing up one level, which completely makes sense as my brain is clear :)
– Sam Bantner
Nov 23 at 13:11
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
If everything is already in memory, you could modify the group_by
line as follows:
reduce group_by(.day) as $in ({}; . + { ($in[0].day): $in }
Alternatives to group_by
Since group_by
entails a sort, it may be unnecessarily inefficient. You might like to consider using a variant such as the following:
# sort-free variant of group_by/1
# f must always evaluate to an integer or always to a string.
# Output: an array in the former case, or an object in the latter case
def GROUP_BY(f): reduce . as $x ({}; .[$x|f] += [$x] );
If everything is already in memory, you could modify the group_by
line as follows:
reduce group_by(.day) as $in ({}; . + { ($in[0].day): $in }
Alternatives to group_by
Since group_by
entails a sort, it may be unnecessarily inefficient. You might like to consider using a variant such as the following:
# sort-free variant of group_by/1
# f must always evaluate to an integer or always to a string.
# Output: an array in the former case, or an object in the latter case
def GROUP_BY(f): reduce . as $x ({}; .[$x|f] += [$x] );
edited Nov 21 at 23:00
answered Nov 21 at 22:51
peak
28.9k73752
28.9k73752
Everything is in memory and this is a very self-explanatory answer. Thanks for the help! I didn't think to bounce reducing up one level, which completely makes sense as my brain is clear :)
– Sam Bantner
Nov 23 at 13:11
add a comment |
Everything is in memory and this is a very self-explanatory answer. Thanks for the help! I didn't think to bounce reducing up one level, which completely makes sense as my brain is clear :)
– Sam Bantner
Nov 23 at 13:11
Everything is in memory and this is a very self-explanatory answer. Thanks for the help! I didn't think to bounce reducing up one level, which completely makes sense as my brain is clear :)
– Sam Bantner
Nov 23 at 13:11
Everything is in memory and this is a very self-explanatory answer. Thanks for the help! I didn't think to bounce reducing up one level, which completely makes sense as my brain is clear :)
– Sam Bantner
Nov 23 at 13:11
add a comment |
up vote
0
down vote
If the stream of objects is already in a file, use inputs
with the -n command-line option.
This will avoid the overhead of "slurping" but will still require enough RAM for the entire result to fit into memory. If that doesn't work for you, then you will have to resort to desperate measures :-)
This might be a useful starting point:
jq -n 'reduce inputs as $in ({}; . + $in)'
I think you could also pipe the output as an array intoadd
.
– mustachioed
Nov 21 at 22:00
add a comment |
up vote
0
down vote
If the stream of objects is already in a file, use inputs
with the -n command-line option.
This will avoid the overhead of "slurping" but will still require enough RAM for the entire result to fit into memory. If that doesn't work for you, then you will have to resort to desperate measures :-)
This might be a useful starting point:
jq -n 'reduce inputs as $in ({}; . + $in)'
I think you could also pipe the output as an array intoadd
.
– mustachioed
Nov 21 at 22:00
add a comment |
up vote
0
down vote
up vote
0
down vote
If the stream of objects is already in a file, use inputs
with the -n command-line option.
This will avoid the overhead of "slurping" but will still require enough RAM for the entire result to fit into memory. If that doesn't work for you, then you will have to resort to desperate measures :-)
This might be a useful starting point:
jq -n 'reduce inputs as $in ({}; . + $in)'
If the stream of objects is already in a file, use inputs
with the -n command-line option.
This will avoid the overhead of "slurping" but will still require enough RAM for the entire result to fit into memory. If that doesn't work for you, then you will have to resort to desperate measures :-)
This might be a useful starting point:
jq -n 'reduce inputs as $in ({}; . + $in)'
edited Nov 22 at 6:38
answered Nov 21 at 21:59
peak
28.9k73752
28.9k73752
I think you could also pipe the output as an array intoadd
.
– mustachioed
Nov 21 at 22:00
add a comment |
I think you could also pipe the output as an array intoadd
.
– mustachioed
Nov 21 at 22:00
I think you could also pipe the output as an array into
add
.– mustachioed
Nov 21 at 22:00
I think you could also pipe the output as an array into
add
.– mustachioed
Nov 21 at 22:00
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53420766%2fhow-can-i-combine-a-sequence-of-json-with-jq-without-using-the-slurp-flag%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown