Json transformation using jq

The name of the pictureThe name of the pictureThe name of the pictureClash 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"









share|improve this question























  • 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














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"









share|improve this question























  • 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












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"









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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
















  • 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










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.






share|improve this answer






















  • @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










Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















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

























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.






share|improve this answer






















  • @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














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.






share|improve this answer






















  • @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












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.






share|improve this answer














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.







share|improve this answer














share|improve this answer



share|improve this answer








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
















  • @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

















 

draft saved


draft discarded















































 


draft saved


draft discarded














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





















































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






Popular posts from this blog

How to check contact read email or not when send email to Individual?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?