Merging four new line delimited json files using jq
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I am trying to merge 4 JSON files (in the future maybe two more).
The only common factor between the newline delimited JSON files is "vulnid".
File 1:
"vulnid":"cve1", "product":"product1"
"vulnid":"cve2", "product":"product2"
"vulnid":"cve3", "product":"product3"
"vulnid":"cve4", "product":""
"vulnid":"cve5", "product":""
"vulnid":"cve6", "product":""
File 2:
"vulnid":"cve1", "version":"version1"
"vulnid":"cve2", "version":"version2"
"vulnid":"cve3", "version":"version3"
"vulnid":"cve4", "version":"version4"
File 3:
"vulnid":"cve1", "patch":"patch1"
"vulnid":"cve2", "patch":"patch2"
"vulnid":"cve3", "patch":"patch3"
"vulnid":"cve4", "patch":""
File 4:
"vulnid":"cve1", "speed":"speed1"
"vulnid":"cve2", "power":"power2"
"vulnid":"cve3", "amps":"amps3"
"vulnid":"cve4", "product":"product4"
"vulnid":"cve4", "patch":"patch4"
Required output:
"vulnid":"cve1", "product":"product1", "version":"version1", "patch":"patch1", "speed":"speed1"
"vulnid":"cve2", "product":"product2", "version":"version2", "patch":"patch2", "power":"power2"
"vulnid":"cve3", "product":"product3", "version":"version3", "patch":"patch3", "amps":"amps3"
"vulnid":"cve4", "product":"product4", "version":"version4", "patch":"patch4"
"vulnid":"cve5", "product":""
"vulnid":"cve6", "product":""
What I have tried so far:
jq -s '.[0] * .[1] * .[3] * .[4]' json1 json2 json3 json4
jq -s '.[0] + .[1] + .[3] + .[4]' json1 json2 json3 json4
... and multiple different combinations of jq, all of them provided incorrect results and proved the point that I am not understand the tool.
Is this even possible to accomplish such merging using the field "vulnid" with jq?
json jq
add a comment |Â
up vote
1
down vote
favorite
I am trying to merge 4 JSON files (in the future maybe two more).
The only common factor between the newline delimited JSON files is "vulnid".
File 1:
"vulnid":"cve1", "product":"product1"
"vulnid":"cve2", "product":"product2"
"vulnid":"cve3", "product":"product3"
"vulnid":"cve4", "product":""
"vulnid":"cve5", "product":""
"vulnid":"cve6", "product":""
File 2:
"vulnid":"cve1", "version":"version1"
"vulnid":"cve2", "version":"version2"
"vulnid":"cve3", "version":"version3"
"vulnid":"cve4", "version":"version4"
File 3:
"vulnid":"cve1", "patch":"patch1"
"vulnid":"cve2", "patch":"patch2"
"vulnid":"cve3", "patch":"patch3"
"vulnid":"cve4", "patch":""
File 4:
"vulnid":"cve1", "speed":"speed1"
"vulnid":"cve2", "power":"power2"
"vulnid":"cve3", "amps":"amps3"
"vulnid":"cve4", "product":"product4"
"vulnid":"cve4", "patch":"patch4"
Required output:
"vulnid":"cve1", "product":"product1", "version":"version1", "patch":"patch1", "speed":"speed1"
"vulnid":"cve2", "product":"product2", "version":"version2", "patch":"patch2", "power":"power2"
"vulnid":"cve3", "product":"product3", "version":"version3", "patch":"patch3", "amps":"amps3"
"vulnid":"cve4", "product":"product4", "version":"version4", "patch":"patch4"
"vulnid":"cve5", "product":""
"vulnid":"cve6", "product":""
What I have tried so far:
jq -s '.[0] * .[1] * .[3] * .[4]' json1 json2 json3 json4
jq -s '.[0] + .[1] + .[3] + .[4]' json1 json2 json3 json4
... and multiple different combinations of jq, all of them provided incorrect results and proved the point that I am not understand the tool.
Is this even possible to accomplish such merging using the field "vulnid" with jq?
json jq
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am trying to merge 4 JSON files (in the future maybe two more).
The only common factor between the newline delimited JSON files is "vulnid".
File 1:
"vulnid":"cve1", "product":"product1"
"vulnid":"cve2", "product":"product2"
"vulnid":"cve3", "product":"product3"
"vulnid":"cve4", "product":""
"vulnid":"cve5", "product":""
"vulnid":"cve6", "product":""
File 2:
"vulnid":"cve1", "version":"version1"
"vulnid":"cve2", "version":"version2"
"vulnid":"cve3", "version":"version3"
"vulnid":"cve4", "version":"version4"
File 3:
"vulnid":"cve1", "patch":"patch1"
"vulnid":"cve2", "patch":"patch2"
"vulnid":"cve3", "patch":"patch3"
"vulnid":"cve4", "patch":""
File 4:
"vulnid":"cve1", "speed":"speed1"
"vulnid":"cve2", "power":"power2"
"vulnid":"cve3", "amps":"amps3"
"vulnid":"cve4", "product":"product4"
"vulnid":"cve4", "patch":"patch4"
Required output:
"vulnid":"cve1", "product":"product1", "version":"version1", "patch":"patch1", "speed":"speed1"
"vulnid":"cve2", "product":"product2", "version":"version2", "patch":"patch2", "power":"power2"
"vulnid":"cve3", "product":"product3", "version":"version3", "patch":"patch3", "amps":"amps3"
"vulnid":"cve4", "product":"product4", "version":"version4", "patch":"patch4"
"vulnid":"cve5", "product":""
"vulnid":"cve6", "product":""
What I have tried so far:
jq -s '.[0] * .[1] * .[3] * .[4]' json1 json2 json3 json4
jq -s '.[0] + .[1] + .[3] + .[4]' json1 json2 json3 json4
... and multiple different combinations of jq, all of them provided incorrect results and proved the point that I am not understand the tool.
Is this even possible to accomplish such merging using the field "vulnid" with jq?
json jq
I am trying to merge 4 JSON files (in the future maybe two more).
The only common factor between the newline delimited JSON files is "vulnid".
File 1:
"vulnid":"cve1", "product":"product1"
"vulnid":"cve2", "product":"product2"
"vulnid":"cve3", "product":"product3"
"vulnid":"cve4", "product":""
"vulnid":"cve5", "product":""
"vulnid":"cve6", "product":""
File 2:
"vulnid":"cve1", "version":"version1"
"vulnid":"cve2", "version":"version2"
"vulnid":"cve3", "version":"version3"
"vulnid":"cve4", "version":"version4"
File 3:
"vulnid":"cve1", "patch":"patch1"
"vulnid":"cve2", "patch":"patch2"
"vulnid":"cve3", "patch":"patch3"
"vulnid":"cve4", "patch":""
File 4:
"vulnid":"cve1", "speed":"speed1"
"vulnid":"cve2", "power":"power2"
"vulnid":"cve3", "amps":"amps3"
"vulnid":"cve4", "product":"product4"
"vulnid":"cve4", "patch":"patch4"
Required output:
"vulnid":"cve1", "product":"product1", "version":"version1", "patch":"patch1", "speed":"speed1"
"vulnid":"cve2", "product":"product2", "version":"version2", "patch":"patch2", "power":"power2"
"vulnid":"cve3", "product":"product3", "version":"version3", "patch":"patch3", "amps":"amps3"
"vulnid":"cve4", "product":"product4", "version":"version4", "patch":"patch4"
"vulnid":"cve5", "product":""
"vulnid":"cve6", "product":""
What I have tried so far:
jq -s '.[0] * .[1] * .[3] * .[4]' json1 json2 json3 json4
jq -s '.[0] + .[1] + .[3] + .[4]' json1 json2 json3 json4
... and multiple different combinations of jq, all of them provided incorrect results and proved the point that I am not understand the tool.
Is this even possible to accomplish such merging using the field "vulnid" with jq?
json jq
edited Jul 5 at 8:35
ilkkachu
47.3k668130
47.3k668130
asked Jul 4 at 17:56
Anna
183
183
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
2
down vote
The shortest jq
solution:
jq -sc 'group_by(.vulnid) | add' file*.json
The output:
"vulnid":"cve1","product":"product1","version":"version1","patch":"patch1","speed":"speed1"
"vulnid":"cve2","product":"product2","version":"version2","patch":"patch2","power":"power2"
"vulnid":"cve3","product":"product3","version":"version3","patch":"patch3","amps":"amps3"
"vulnid":"cve4","product":"product4","version":"version4","patch":"patch4"
"vulnid":"cve5","product":""
"vulnid":"cve6","product":""
ah, I just couldn't wrap my head aroundgroup_by
, but that does make sense.
â ilkkachu
Jul 5 at 8:35
add a comment |Â
up vote
1
down vote
Yeah, I think so.
If we change the data so that we have the vulnid
strings as keys, with the object itself as the corresponding value (e.g. "cve1": "vulnid": "cve1", "product": "product1"
), then we can use reduce
and *
to combine the ones with the same keys. The first map()
below produces the correct format for the reduce
.
So, with your sample input in the file json
:
$ < json jq -s 'map((.vulnid): . ) | reduce . as $item (; . * $item) '
{
"cve1":
"vulnid": "cve1",
"product": "product1",
"version": "version1",
"patch": "patch1",
"speed": "speed1"
,
...
Then it's rather simple to drop the fluff around the objects to get the expected output:
$ < json jq -s 'map((.vulnid): . ) |
reduce . as $item (; . * $item) | map(.) | .'
"vulnid": "cve1",
"product": "product1",
"version": "version1",
"patch": "patch1",
"speed": "speed1"
...
I wouldn't be surprised if there was a better way to do all of this, though.
@Anna, uhh, comments don't work too well for formatted text, but if I got that right, I think you changed the input to"cve1": ...
manually. The firstmap(...)
in my script was there to do just that, and doing it again doesn't work (since the new objects don't have thevulnid
key). I meant to just push the sample data in your question to the command as-is (without theFile N
headers, of course).
â ilkkachu
Jul 4 at 20:24
I am sorry, worked! I don't have to parse as you suggested because the jq does that! :) Amazing, thanks!
â Anna
Jul 4 at 20:36
1
I spy a useless cat.
â steve
Jul 4 at 22:21
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
The shortest jq
solution:
jq -sc 'group_by(.vulnid) | add' file*.json
The output:
"vulnid":"cve1","product":"product1","version":"version1","patch":"patch1","speed":"speed1"
"vulnid":"cve2","product":"product2","version":"version2","patch":"patch2","power":"power2"
"vulnid":"cve3","product":"product3","version":"version3","patch":"patch3","amps":"amps3"
"vulnid":"cve4","product":"product4","version":"version4","patch":"patch4"
"vulnid":"cve5","product":""
"vulnid":"cve6","product":""
ah, I just couldn't wrap my head aroundgroup_by
, but that does make sense.
â ilkkachu
Jul 5 at 8:35
add a comment |Â
up vote
2
down vote
The shortest jq
solution:
jq -sc 'group_by(.vulnid) | add' file*.json
The output:
"vulnid":"cve1","product":"product1","version":"version1","patch":"patch1","speed":"speed1"
"vulnid":"cve2","product":"product2","version":"version2","patch":"patch2","power":"power2"
"vulnid":"cve3","product":"product3","version":"version3","patch":"patch3","amps":"amps3"
"vulnid":"cve4","product":"product4","version":"version4","patch":"patch4"
"vulnid":"cve5","product":""
"vulnid":"cve6","product":""
ah, I just couldn't wrap my head aroundgroup_by
, but that does make sense.
â ilkkachu
Jul 5 at 8:35
add a comment |Â
up vote
2
down vote
up vote
2
down vote
The shortest jq
solution:
jq -sc 'group_by(.vulnid) | add' file*.json
The output:
"vulnid":"cve1","product":"product1","version":"version1","patch":"patch1","speed":"speed1"
"vulnid":"cve2","product":"product2","version":"version2","patch":"patch2","power":"power2"
"vulnid":"cve3","product":"product3","version":"version3","patch":"patch3","amps":"amps3"
"vulnid":"cve4","product":"product4","version":"version4","patch":"patch4"
"vulnid":"cve5","product":""
"vulnid":"cve6","product":""
The shortest jq
solution:
jq -sc 'group_by(.vulnid) | add' file*.json
The output:
"vulnid":"cve1","product":"product1","version":"version1","patch":"patch1","speed":"speed1"
"vulnid":"cve2","product":"product2","version":"version2","patch":"patch2","power":"power2"
"vulnid":"cve3","product":"product3","version":"version3","patch":"patch3","amps":"amps3"
"vulnid":"cve4","product":"product4","version":"version4","patch":"patch4"
"vulnid":"cve5","product":""
"vulnid":"cve6","product":""
answered Jul 5 at 6:05
RomanPerekhrest
22.4k12144
22.4k12144
ah, I just couldn't wrap my head aroundgroup_by
, but that does make sense.
â ilkkachu
Jul 5 at 8:35
add a comment |Â
ah, I just couldn't wrap my head aroundgroup_by
, but that does make sense.
â ilkkachu
Jul 5 at 8:35
ah, I just couldn't wrap my head around
group_by
, but that does make sense.â ilkkachu
Jul 5 at 8:35
ah, I just couldn't wrap my head around
group_by
, but that does make sense.â ilkkachu
Jul 5 at 8:35
add a comment |Â
up vote
1
down vote
Yeah, I think so.
If we change the data so that we have the vulnid
strings as keys, with the object itself as the corresponding value (e.g. "cve1": "vulnid": "cve1", "product": "product1"
), then we can use reduce
and *
to combine the ones with the same keys. The first map()
below produces the correct format for the reduce
.
So, with your sample input in the file json
:
$ < json jq -s 'map((.vulnid): . ) | reduce . as $item (; . * $item) '
{
"cve1":
"vulnid": "cve1",
"product": "product1",
"version": "version1",
"patch": "patch1",
"speed": "speed1"
,
...
Then it's rather simple to drop the fluff around the objects to get the expected output:
$ < json jq -s 'map((.vulnid): . ) |
reduce . as $item (; . * $item) | map(.) | .'
"vulnid": "cve1",
"product": "product1",
"version": "version1",
"patch": "patch1",
"speed": "speed1"
...
I wouldn't be surprised if there was a better way to do all of this, though.
@Anna, uhh, comments don't work too well for formatted text, but if I got that right, I think you changed the input to"cve1": ...
manually. The firstmap(...)
in my script was there to do just that, and doing it again doesn't work (since the new objects don't have thevulnid
key). I meant to just push the sample data in your question to the command as-is (without theFile N
headers, of course).
â ilkkachu
Jul 4 at 20:24
I am sorry, worked! I don't have to parse as you suggested because the jq does that! :) Amazing, thanks!
â Anna
Jul 4 at 20:36
1
I spy a useless cat.
â steve
Jul 4 at 22:21
add a comment |Â
up vote
1
down vote
Yeah, I think so.
If we change the data so that we have the vulnid
strings as keys, with the object itself as the corresponding value (e.g. "cve1": "vulnid": "cve1", "product": "product1"
), then we can use reduce
and *
to combine the ones with the same keys. The first map()
below produces the correct format for the reduce
.
So, with your sample input in the file json
:
$ < json jq -s 'map((.vulnid): . ) | reduce . as $item (; . * $item) '
{
"cve1":
"vulnid": "cve1",
"product": "product1",
"version": "version1",
"patch": "patch1",
"speed": "speed1"
,
...
Then it's rather simple to drop the fluff around the objects to get the expected output:
$ < json jq -s 'map((.vulnid): . ) |
reduce . as $item (; . * $item) | map(.) | .'
"vulnid": "cve1",
"product": "product1",
"version": "version1",
"patch": "patch1",
"speed": "speed1"
...
I wouldn't be surprised if there was a better way to do all of this, though.
@Anna, uhh, comments don't work too well for formatted text, but if I got that right, I think you changed the input to"cve1": ...
manually. The firstmap(...)
in my script was there to do just that, and doing it again doesn't work (since the new objects don't have thevulnid
key). I meant to just push the sample data in your question to the command as-is (without theFile N
headers, of course).
â ilkkachu
Jul 4 at 20:24
I am sorry, worked! I don't have to parse as you suggested because the jq does that! :) Amazing, thanks!
â Anna
Jul 4 at 20:36
1
I spy a useless cat.
â steve
Jul 4 at 22:21
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Yeah, I think so.
If we change the data so that we have the vulnid
strings as keys, with the object itself as the corresponding value (e.g. "cve1": "vulnid": "cve1", "product": "product1"
), then we can use reduce
and *
to combine the ones with the same keys. The first map()
below produces the correct format for the reduce
.
So, with your sample input in the file json
:
$ < json jq -s 'map((.vulnid): . ) | reduce . as $item (; . * $item) '
{
"cve1":
"vulnid": "cve1",
"product": "product1",
"version": "version1",
"patch": "patch1",
"speed": "speed1"
,
...
Then it's rather simple to drop the fluff around the objects to get the expected output:
$ < json jq -s 'map((.vulnid): . ) |
reduce . as $item (; . * $item) | map(.) | .'
"vulnid": "cve1",
"product": "product1",
"version": "version1",
"patch": "patch1",
"speed": "speed1"
...
I wouldn't be surprised if there was a better way to do all of this, though.
Yeah, I think so.
If we change the data so that we have the vulnid
strings as keys, with the object itself as the corresponding value (e.g. "cve1": "vulnid": "cve1", "product": "product1"
), then we can use reduce
and *
to combine the ones with the same keys. The first map()
below produces the correct format for the reduce
.
So, with your sample input in the file json
:
$ < json jq -s 'map((.vulnid): . ) | reduce . as $item (; . * $item) '
{
"cve1":
"vulnid": "cve1",
"product": "product1",
"version": "version1",
"patch": "patch1",
"speed": "speed1"
,
...
Then it's rather simple to drop the fluff around the objects to get the expected output:
$ < json jq -s 'map((.vulnid): . ) |
reduce . as $item (; . * $item) | map(.) | .'
"vulnid": "cve1",
"product": "product1",
"version": "version1",
"patch": "patch1",
"speed": "speed1"
...
I wouldn't be surprised if there was a better way to do all of this, though.
edited Jul 5 at 8:28
answered Jul 4 at 19:08
ilkkachu
47.3k668130
47.3k668130
@Anna, uhh, comments don't work too well for formatted text, but if I got that right, I think you changed the input to"cve1": ...
manually. The firstmap(...)
in my script was there to do just that, and doing it again doesn't work (since the new objects don't have thevulnid
key). I meant to just push the sample data in your question to the command as-is (without theFile N
headers, of course).
â ilkkachu
Jul 4 at 20:24
I am sorry, worked! I don't have to parse as you suggested because the jq does that! :) Amazing, thanks!
â Anna
Jul 4 at 20:36
1
I spy a useless cat.
â steve
Jul 4 at 22:21
add a comment |Â
@Anna, uhh, comments don't work too well for formatted text, but if I got that right, I think you changed the input to"cve1": ...
manually. The firstmap(...)
in my script was there to do just that, and doing it again doesn't work (since the new objects don't have thevulnid
key). I meant to just push the sample data in your question to the command as-is (without theFile N
headers, of course).
â ilkkachu
Jul 4 at 20:24
I am sorry, worked! I don't have to parse as you suggested because the jq does that! :) Amazing, thanks!
â Anna
Jul 4 at 20:36
1
I spy a useless cat.
â steve
Jul 4 at 22:21
@Anna, uhh, comments don't work too well for formatted text, but if I got that right, I think you changed the input to
"cve1": ...
manually. The first map(...)
in my script was there to do just that, and doing it again doesn't work (since the new objects don't have the vulnid
key). I meant to just push the sample data in your question to the command as-is (without the File N
headers, of course).â ilkkachu
Jul 4 at 20:24
@Anna, uhh, comments don't work too well for formatted text, but if I got that right, I think you changed the input to
"cve1": ...
manually. The first map(...)
in my script was there to do just that, and doing it again doesn't work (since the new objects don't have the vulnid
key). I meant to just push the sample data in your question to the command as-is (without the File N
headers, of course).â ilkkachu
Jul 4 at 20:24
I am sorry, worked! I don't have to parse as you suggested because the jq does that! :) Amazing, thanks!
â Anna
Jul 4 at 20:36
I am sorry, worked! I don't have to parse as you suggested because the jq does that! :) Amazing, thanks!
â Anna
Jul 4 at 20:36
1
1
I spy a useless cat.
â steve
Jul 4 at 22:21
I spy a useless cat.
â steve
Jul 4 at 22:21
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f453468%2fmerging-four-new-line-delimited-json-files-using-jq%23new-answer', 'question_page');
);
Post as a guest
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
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
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