Json transformation using jq
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I want to transform json to new line delimited json.
I was attempting to do it multiple times using jq in bash, but I have not been able to get even close to the final output.
INPUT:
"windows124":
"updated": "2015-01-14",
"attribution": ,
"description": "",
"notes": ,
"alt_names": ,
"sources": ,
"urls": ["google.com", "google.co.uk"],
"common_name": "test",
"uuid": "7259334c-3218-4259-aaab-896d87507f4f"
,
"linux124":
"updated": "",
"attribution": ["Naifdddkoscn"],
"description": "",
"notes": ,
"alt_names": ,
"sources": ,
"urls": ["https://example.com/1.pdf", "https://example.com/1.pdf", "https://example.com/1.pdf"],
"common_name": "121212",
"uuid": "009db412-762d-4256-8df9-eb213be01ffd"
,
"wikipedia123":
"updated": "2018-07-31",
"attribution": ,
"description": "",
"notes": ,
"alt_names": ,
"sources": ,
"urls": ["https://example.com/1.pdf"],
"common_name": "test343",
"uuid": "4d8da0af-cfd7-4990-b211-af0e990vfda0"
WANTED OUTPUT:
"uuid": "7259334c-3218-4259-aaab-896d87507f4f","family": "windows124","updated": "2015-01-14","attribution": ,"description": "","notes": ,"alt_names": ,"sources": ,"urls": ["google.com", "google.co.uk"],"common_name": "test"
"uuid": "009db412-762d-4256-8df9-eb213be01ffd","family": "linux124", "updated": "","attribution": ["Naifdddkoscn"],"description": "","notes": ,"alt_names": ,"sources": ,"urls": ["https://example.com/1.pdf", "https://example.com/1.pdf", "https://example.com/1.pdf"],"common_name": "121212"
"uuid": "4d8da0af-cfd7-4990-b211-af0e990vfda0","family": "wikipedia123", "updated": "2018-07-31","attribution": ,"description": "","notes": ,"alt_names": ,"sources": ,"urls": ["https://example.com/1.pdf"],"common_name": "test343"
What I have so far is:
cat deserialize.json | jq -c '.|to_entries'
"key":"windows124","value":"updated":"2015-01-14","attribution":,"description":"","notes":,"alt_names":,"sources":,"urls":["google.com","google.co.uk"],"common_name":"test","uuid":"7259334c-3218-4259-aaab-896d87507f4f"
"key":"linux124","value":"updated":"","attribution":["Naifdddkoscn"],"description":"","notes":,"alt_names":,"sources":,"urls":["https://example.com/1.pdf","https://example.com/1.pdf","https://example.com/1.pdf"],"common_name":"121212","uuid":"009db412-762d-4256-8df9-eb213be01ffd"
"key":"wikipedia123","value":"updated":"2018-07-31","attribution":,"description":"","notes":,"alt_names":,"sources":,"urls":["https://example.com/1.pdf"],"common_name":"test343","uuid":"4d8da0af-cfd7-4990-b211-af0e990vfda0"
linux json jq
add a comment |
up vote
0
down vote
favorite
I want to transform json to new line delimited json.
I was attempting to do it multiple times using jq in bash, but I have not been able to get even close to the final output.
INPUT:
"windows124":
"updated": "2015-01-14",
"attribution": ,
"description": "",
"notes": ,
"alt_names": ,
"sources": ,
"urls": ["google.com", "google.co.uk"],
"common_name": "test",
"uuid": "7259334c-3218-4259-aaab-896d87507f4f"
,
"linux124":
"updated": "",
"attribution": ["Naifdddkoscn"],
"description": "",
"notes": ,
"alt_names": ,
"sources": ,
"urls": ["https://example.com/1.pdf", "https://example.com/1.pdf", "https://example.com/1.pdf"],
"common_name": "121212",
"uuid": "009db412-762d-4256-8df9-eb213be01ffd"
,
"wikipedia123":
"updated": "2018-07-31",
"attribution": ,
"description": "",
"notes": ,
"alt_names": ,
"sources": ,
"urls": ["https://example.com/1.pdf"],
"common_name": "test343",
"uuid": "4d8da0af-cfd7-4990-b211-af0e990vfda0"
WANTED OUTPUT:
"uuid": "7259334c-3218-4259-aaab-896d87507f4f","family": "windows124","updated": "2015-01-14","attribution": ,"description": "","notes": ,"alt_names": ,"sources": ,"urls": ["google.com", "google.co.uk"],"common_name": "test"
"uuid": "009db412-762d-4256-8df9-eb213be01ffd","family": "linux124", "updated": "","attribution": ["Naifdddkoscn"],"description": "","notes": ,"alt_names": ,"sources": ,"urls": ["https://example.com/1.pdf", "https://example.com/1.pdf", "https://example.com/1.pdf"],"common_name": "121212"
"uuid": "4d8da0af-cfd7-4990-b211-af0e990vfda0","family": "wikipedia123", "updated": "2018-07-31","attribution": ,"description": "","notes": ,"alt_names": ,"sources": ,"urls": ["https://example.com/1.pdf"],"common_name": "test343"
What I have so far is:
cat deserialize.json | jq -c '.|to_entries'
"key":"windows124","value":"updated":"2015-01-14","attribution":,"description":"","notes":,"alt_names":,"sources":,"urls":["google.com","google.co.uk"],"common_name":"test","uuid":"7259334c-3218-4259-aaab-896d87507f4f"
"key":"linux124","value":"updated":"","attribution":["Naifdddkoscn"],"description":"","notes":,"alt_names":,"sources":,"urls":["https://example.com/1.pdf","https://example.com/1.pdf","https://example.com/1.pdf"],"common_name":"121212","uuid":"009db412-762d-4256-8df9-eb213be01ffd"
"key":"wikipedia123","value":"updated":"2018-07-31","attribution":,"description":"","notes":,"alt_names":,"sources":,"urls":["https://example.com/1.pdf"],"common_name":"test343","uuid":"4d8da0af-cfd7-4990-b211-af0e990vfda0"
linux json jq
Got closer with : jq '.|to_entries'
– creed
yesterday
You can add the-c
(--compact-output
) command line option if you don't want the output pretty-printed
– steeldriver
yesterday
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I want to transform json to new line delimited json.
I was attempting to do it multiple times using jq in bash, but I have not been able to get even close to the final output.
INPUT:
"windows124":
"updated": "2015-01-14",
"attribution": ,
"description": "",
"notes": ,
"alt_names": ,
"sources": ,
"urls": ["google.com", "google.co.uk"],
"common_name": "test",
"uuid": "7259334c-3218-4259-aaab-896d87507f4f"
,
"linux124":
"updated": "",
"attribution": ["Naifdddkoscn"],
"description": "",
"notes": ,
"alt_names": ,
"sources": ,
"urls": ["https://example.com/1.pdf", "https://example.com/1.pdf", "https://example.com/1.pdf"],
"common_name": "121212",
"uuid": "009db412-762d-4256-8df9-eb213be01ffd"
,
"wikipedia123":
"updated": "2018-07-31",
"attribution": ,
"description": "",
"notes": ,
"alt_names": ,
"sources": ,
"urls": ["https://example.com/1.pdf"],
"common_name": "test343",
"uuid": "4d8da0af-cfd7-4990-b211-af0e990vfda0"
WANTED OUTPUT:
"uuid": "7259334c-3218-4259-aaab-896d87507f4f","family": "windows124","updated": "2015-01-14","attribution": ,"description": "","notes": ,"alt_names": ,"sources": ,"urls": ["google.com", "google.co.uk"],"common_name": "test"
"uuid": "009db412-762d-4256-8df9-eb213be01ffd","family": "linux124", "updated": "","attribution": ["Naifdddkoscn"],"description": "","notes": ,"alt_names": ,"sources": ,"urls": ["https://example.com/1.pdf", "https://example.com/1.pdf", "https://example.com/1.pdf"],"common_name": "121212"
"uuid": "4d8da0af-cfd7-4990-b211-af0e990vfda0","family": "wikipedia123", "updated": "2018-07-31","attribution": ,"description": "","notes": ,"alt_names": ,"sources": ,"urls": ["https://example.com/1.pdf"],"common_name": "test343"
What I have so far is:
cat deserialize.json | jq -c '.|to_entries'
"key":"windows124","value":"updated":"2015-01-14","attribution":,"description":"","notes":,"alt_names":,"sources":,"urls":["google.com","google.co.uk"],"common_name":"test","uuid":"7259334c-3218-4259-aaab-896d87507f4f"
"key":"linux124","value":"updated":"","attribution":["Naifdddkoscn"],"description":"","notes":,"alt_names":,"sources":,"urls":["https://example.com/1.pdf","https://example.com/1.pdf","https://example.com/1.pdf"],"common_name":"121212","uuid":"009db412-762d-4256-8df9-eb213be01ffd"
"key":"wikipedia123","value":"updated":"2018-07-31","attribution":,"description":"","notes":,"alt_names":,"sources":,"urls":["https://example.com/1.pdf"],"common_name":"test343","uuid":"4d8da0af-cfd7-4990-b211-af0e990vfda0"
linux json jq
I want to transform json to new line delimited json.
I was attempting to do it multiple times using jq in bash, but I have not been able to get even close to the final output.
INPUT:
"windows124":
"updated": "2015-01-14",
"attribution": ,
"description": "",
"notes": ,
"alt_names": ,
"sources": ,
"urls": ["google.com", "google.co.uk"],
"common_name": "test",
"uuid": "7259334c-3218-4259-aaab-896d87507f4f"
,
"linux124":
"updated": "",
"attribution": ["Naifdddkoscn"],
"description": "",
"notes": ,
"alt_names": ,
"sources": ,
"urls": ["https://example.com/1.pdf", "https://example.com/1.pdf", "https://example.com/1.pdf"],
"common_name": "121212",
"uuid": "009db412-762d-4256-8df9-eb213be01ffd"
,
"wikipedia123":
"updated": "2018-07-31",
"attribution": ,
"description": "",
"notes": ,
"alt_names": ,
"sources": ,
"urls": ["https://example.com/1.pdf"],
"common_name": "test343",
"uuid": "4d8da0af-cfd7-4990-b211-af0e990vfda0"
WANTED OUTPUT:
"uuid": "7259334c-3218-4259-aaab-896d87507f4f","family": "windows124","updated": "2015-01-14","attribution": ,"description": "","notes": ,"alt_names": ,"sources": ,"urls": ["google.com", "google.co.uk"],"common_name": "test"
"uuid": "009db412-762d-4256-8df9-eb213be01ffd","family": "linux124", "updated": "","attribution": ["Naifdddkoscn"],"description": "","notes": ,"alt_names": ,"sources": ,"urls": ["https://example.com/1.pdf", "https://example.com/1.pdf", "https://example.com/1.pdf"],"common_name": "121212"
"uuid": "4d8da0af-cfd7-4990-b211-af0e990vfda0","family": "wikipedia123", "updated": "2018-07-31","attribution": ,"description": "","notes": ,"alt_names": ,"sources": ,"urls": ["https://example.com/1.pdf"],"common_name": "test343"
What I have so far is:
cat deserialize.json | jq -c '.|to_entries'
"key":"windows124","value":"updated":"2015-01-14","attribution":,"description":"","notes":,"alt_names":,"sources":,"urls":["google.com","google.co.uk"],"common_name":"test","uuid":"7259334c-3218-4259-aaab-896d87507f4f"
"key":"linux124","value":"updated":"","attribution":["Naifdddkoscn"],"description":"","notes":,"alt_names":,"sources":,"urls":["https://example.com/1.pdf","https://example.com/1.pdf","https://example.com/1.pdf"],"common_name":"121212","uuid":"009db412-762d-4256-8df9-eb213be01ffd"
"key":"wikipedia123","value":"updated":"2018-07-31","attribution":,"description":"","notes":,"alt_names":,"sources":,"urls":["https://example.com/1.pdf"],"common_name":"test343","uuid":"4d8da0af-cfd7-4990-b211-af0e990vfda0"
linux json jq
linux json jq
edited yesterday
Rui F Ribeiro
38.1k1475123
38.1k1475123
asked yesterday
creed
153
153
Got closer with : jq '.|to_entries'
– creed
yesterday
You can add the-c
(--compact-output
) command line option if you don't want the output pretty-printed
– steeldriver
yesterday
add a comment |
Got closer with : jq '.|to_entries'
– creed
yesterday
You can add the-c
(--compact-output
) command line option if you don't want the output pretty-printed
– steeldriver
yesterday
Got closer with : jq '.|to_entries'
– creed
yesterday
Got closer with : jq '.|to_entries'
– creed
yesterday
You can add the
-c
(--compact-output
) command line option if you don't want the output pretty-printed– steeldriver
yesterday
You can add the
-c
(--compact-output
) command line option if you don't want the output pretty-printed– steeldriver
yesterday
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
You can use this jq filter:
<file jq 'to_entries|map(.value + family:(.key))'
As you found out, the to_entries
function allows to get the key name in order add the property family
.
So the filter only creates this family
object and adds it to the content of the value
given by to_entries
function.
The map
function does the add operation to all elements of the value
array.
The final gets rid of the outer array.
Note the output is not order in the way you posted, but the content is same. If you want the keys to be sorted, use the option -S
.
@DOWNVOTER Please explain why!
– oliv
yesterday
Thanks $oliv. Nicely done. Have you solved similar issue before, or how did you figure that out? If you can, please let me know how I could replicate your steps differently than doing brute force on all options.
– creed
yesterday
@creed The way to solve this type of problem is indeed to try out. Read the man pages and go to the jq website to get to know the functions and how to use it. In other words, learn the language.
– oliv
23 hours ago
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
You can use this jq filter:
<file jq 'to_entries|map(.value + family:(.key))'
As you found out, the to_entries
function allows to get the key name in order add the property family
.
So the filter only creates this family
object and adds it to the content of the value
given by to_entries
function.
The map
function does the add operation to all elements of the value
array.
The final gets rid of the outer array.
Note the output is not order in the way you posted, but the content is same. If you want the keys to be sorted, use the option -S
.
@DOWNVOTER Please explain why!
– oliv
yesterday
Thanks $oliv. Nicely done. Have you solved similar issue before, or how did you figure that out? If you can, please let me know how I could replicate your steps differently than doing brute force on all options.
– creed
yesterday
@creed The way to solve this type of problem is indeed to try out. Read the man pages and go to the jq website to get to know the functions and how to use it. In other words, learn the language.
– oliv
23 hours ago
add a comment |
up vote
2
down vote
accepted
You can use this jq filter:
<file jq 'to_entries|map(.value + family:(.key))'
As you found out, the to_entries
function allows to get the key name in order add the property family
.
So the filter only creates this family
object and adds it to the content of the value
given by to_entries
function.
The map
function does the add operation to all elements of the value
array.
The final gets rid of the outer array.
Note the output is not order in the way you posted, but the content is same. If you want the keys to be sorted, use the option -S
.
@DOWNVOTER Please explain why!
– oliv
yesterday
Thanks $oliv. Nicely done. Have you solved similar issue before, or how did you figure that out? If you can, please let me know how I could replicate your steps differently than doing brute force on all options.
– creed
yesterday
@creed The way to solve this type of problem is indeed to try out. Read the man pages and go to the jq website to get to know the functions and how to use it. In other words, learn the language.
– oliv
23 hours ago
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
You can use this jq filter:
<file jq 'to_entries|map(.value + family:(.key))'
As you found out, the to_entries
function allows to get the key name in order add the property family
.
So the filter only creates this family
object and adds it to the content of the value
given by to_entries
function.
The map
function does the add operation to all elements of the value
array.
The final gets rid of the outer array.
Note the output is not order in the way you posted, but the content is same. If you want the keys to be sorted, use the option -S
.
You can use this jq filter:
<file jq 'to_entries|map(.value + family:(.key))'
As you found out, the to_entries
function allows to get the key name in order add the property family
.
So the filter only creates this family
object and adds it to the content of the value
given by to_entries
function.
The map
function does the add operation to all elements of the value
array.
The final gets rid of the outer array.
Note the output is not order in the way you posted, but the content is same. If you want the keys to be sorted, use the option -S
.
edited 22 hours ago
answered yesterday
oliv
1,626311
1,626311
@DOWNVOTER Please explain why!
– oliv
yesterday
Thanks $oliv. Nicely done. Have you solved similar issue before, or how did you figure that out? If you can, please let me know how I could replicate your steps differently than doing brute force on all options.
– creed
yesterday
@creed The way to solve this type of problem is indeed to try out. Read the man pages and go to the jq website to get to know the functions and how to use it. In other words, learn the language.
– oliv
23 hours ago
add a comment |
@DOWNVOTER Please explain why!
– oliv
yesterday
Thanks $oliv. Nicely done. Have you solved similar issue before, or how did you figure that out? If you can, please let me know how I could replicate your steps differently than doing brute force on all options.
– creed
yesterday
@creed The way to solve this type of problem is indeed to try out. Read the man pages and go to the jq website to get to know the functions and how to use it. In other words, learn the language.
– oliv
23 hours ago
@DOWNVOTER Please explain why!
– oliv
yesterday
@DOWNVOTER Please explain why!
– oliv
yesterday
Thanks $oliv. Nicely done. Have you solved similar issue before, or how did you figure that out? If you can, please let me know how I could replicate your steps differently than doing brute force on all options.
– creed
yesterday
Thanks $oliv. Nicely done. Have you solved similar issue before, or how did you figure that out? If you can, please let me know how I could replicate your steps differently than doing brute force on all options.
– creed
yesterday
@creed The way to solve this type of problem is indeed to try out. Read the man pages and go to the jq website to get to know the functions and how to use it. In other words, learn the language.
– oliv
23 hours ago
@creed The way to solve this type of problem is indeed to try out. Read the man pages and go to the jq website to get to know the functions and how to use it. In other words, learn the language.
– oliv
23 hours ago
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%2funix.stackexchange.com%2fquestions%2f481738%2fjson-transformation-using-jq%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
Got closer with : jq '.|to_entries'
– creed
yesterday
You can add the
-c
(--compact-output
) command line option if you don't want the output pretty-printed– steeldriver
yesterday