parse JSON data using jq and convert into csv
I am downloading api json data and converting into my final result as a csv file.
The sample data is in the below form:
{
"content": [{
"Title": "abc",
"brand": "xyz",
"information": {
"c1": "101",
"c2": "11111",
"c3": "a,b,c,d,e:abc."
}
},
{
"Title": "RX100",
"brand": "Yamaha",
"information": {
"c1": "102",
"c2": "22222",
"c3": "a."
}
},
{
"Title": "victor",
"brand": "TVS",
"information": {
"c1": "103",
"c2": "33333",
"c3": "a,b,c"
}
},
{
"Title": "R15",
"brand": "Yamaha",
"information": {
"c1": "104",
"c2": "44444",
"c3": "a,b"
}
}
]
}
I have successfully downloaded and converted into multiple csv on the basis of number of Title.
Data looks like in the below form after converting in multiple csv file.
Headers-> c1,c2,c3
csv1--> 101,11111,a,b,c,d,e:abc.
csv2--> 102,22222,a.
csv3--> 103,33333,a,b,c.
csv4--> 104,44444,a,b.
but I want the above data in below form.
Headers-> c1,c2,c3,c4,c5,c6,c7
csv1--> 101,11111,a,b,c,d,e:abc.
csv2--> 102,22222,a.
csv3--> 103,33333,a,b,c.
csv4--> 104,44444,a,b.
Is it possible to divide the c3 into different number of columns on the basis of "," present in my json file using json. The column c3 will get divided on the basis of number of elements are present and will be the maximum value of c3 data.
shell-script jq
add a comment |
I am downloading api json data and converting into my final result as a csv file.
The sample data is in the below form:
{
"content": [{
"Title": "abc",
"brand": "xyz",
"information": {
"c1": "101",
"c2": "11111",
"c3": "a,b,c,d,e:abc."
}
},
{
"Title": "RX100",
"brand": "Yamaha",
"information": {
"c1": "102",
"c2": "22222",
"c3": "a."
}
},
{
"Title": "victor",
"brand": "TVS",
"information": {
"c1": "103",
"c2": "33333",
"c3": "a,b,c"
}
},
{
"Title": "R15",
"brand": "Yamaha",
"information": {
"c1": "104",
"c2": "44444",
"c3": "a,b"
}
}
]
}
I have successfully downloaded and converted into multiple csv on the basis of number of Title.
Data looks like in the below form after converting in multiple csv file.
Headers-> c1,c2,c3
csv1--> 101,11111,a,b,c,d,e:abc.
csv2--> 102,22222,a.
csv3--> 103,33333,a,b,c.
csv4--> 104,44444,a,b.
but I want the above data in below form.
Headers-> c1,c2,c3,c4,c5,c6,c7
csv1--> 101,11111,a,b,c,d,e:abc.
csv2--> 102,22222,a.
csv3--> 103,33333,a,b,c.
csv4--> 104,44444,a,b.
Is it possible to divide the c3 into different number of columns on the basis of "," present in my json file using json. The column c3 will get divided on the basis of number of elements are present and will be the maximum value of c3 data.
shell-script jq
add a comment |
I am downloading api json data and converting into my final result as a csv file.
The sample data is in the below form:
{
"content": [{
"Title": "abc",
"brand": "xyz",
"information": {
"c1": "101",
"c2": "11111",
"c3": "a,b,c,d,e:abc."
}
},
{
"Title": "RX100",
"brand": "Yamaha",
"information": {
"c1": "102",
"c2": "22222",
"c3": "a."
}
},
{
"Title": "victor",
"brand": "TVS",
"information": {
"c1": "103",
"c2": "33333",
"c3": "a,b,c"
}
},
{
"Title": "R15",
"brand": "Yamaha",
"information": {
"c1": "104",
"c2": "44444",
"c3": "a,b"
}
}
]
}
I have successfully downloaded and converted into multiple csv on the basis of number of Title.
Data looks like in the below form after converting in multiple csv file.
Headers-> c1,c2,c3
csv1--> 101,11111,a,b,c,d,e:abc.
csv2--> 102,22222,a.
csv3--> 103,33333,a,b,c.
csv4--> 104,44444,a,b.
but I want the above data in below form.
Headers-> c1,c2,c3,c4,c5,c6,c7
csv1--> 101,11111,a,b,c,d,e:abc.
csv2--> 102,22222,a.
csv3--> 103,33333,a,b,c.
csv4--> 104,44444,a,b.
Is it possible to divide the c3 into different number of columns on the basis of "," present in my json file using json. The column c3 will get divided on the basis of number of elements are present and will be the maximum value of c3 data.
shell-script jq
I am downloading api json data and converting into my final result as a csv file.
The sample data is in the below form:
{
"content": [{
"Title": "abc",
"brand": "xyz",
"information": {
"c1": "101",
"c2": "11111",
"c3": "a,b,c,d,e:abc."
}
},
{
"Title": "RX100",
"brand": "Yamaha",
"information": {
"c1": "102",
"c2": "22222",
"c3": "a."
}
},
{
"Title": "victor",
"brand": "TVS",
"information": {
"c1": "103",
"c2": "33333",
"c3": "a,b,c"
}
},
{
"Title": "R15",
"brand": "Yamaha",
"information": {
"c1": "104",
"c2": "44444",
"c3": "a,b"
}
}
]
}
I have successfully downloaded and converted into multiple csv on the basis of number of Title.
Data looks like in the below form after converting in multiple csv file.
Headers-> c1,c2,c3
csv1--> 101,11111,a,b,c,d,e:abc.
csv2--> 102,22222,a.
csv3--> 103,33333,a,b,c.
csv4--> 104,44444,a,b.
but I want the above data in below form.
Headers-> c1,c2,c3,c4,c5,c6,c7
csv1--> 101,11111,a,b,c,d,e:abc.
csv2--> 102,22222,a.
csv3--> 103,33333,a,b,c.
csv4--> 104,44444,a,b.
Is it possible to divide the c3 into different number of columns on the basis of "," present in my json file using json. The column c3 will get divided on the basis of number of elements are present and will be the maximum value of c3 data.
shell-script jq
shell-script jq
asked 1 hour ago
samsam
386
386
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
For this exact data,
jq -r '.content.information | [.c1, .c2, .c3] | join(",")' < sample.json
will work - just inserting commas between the three columns replicates the effect of a joined-upCSV file. For more complex real data, particularly with numbers, this will work better:
jq -r '.content.information | [.c1, .c2, .c3|split(",")|.] | @csv' < sample.json
This one is all fairly standard up to .c3|split(",")|.
, which
- Extracts column 3's value
- Splits that value into an array on commas
- Flattens that array out
@csv
then converts the array into CSV format. This produces the following output for your sample file:
"101","11111","a","b","c","d","e:abc."
"102","22222","a."
"103","33333","a","b","c"
"104","44444","a","b"
If you don't want quotes, the most reliable thing is to use @tsv
and just replace the tabs:
jq -r '.content.information|[.c1, .c2, .c3|split(",")|.] | @tsv|gsub("t"; ",")' < sample.json
If all your values are strings you could also use join(",")
again.
If you want the literal header line c1,...c7
created as well, this will do it:
jq -r '[.content.information|[.c1, .c2, .c3|split(",")|.]] | (([range([. | length] | max)|"c" + (.+1|tostring)]|join(",")), (. | join(",")))' < sample.json
There's three parts: the first creates the arrays of columns as before, and then the other two parts use that as input:
([range([. | length] | max)|"c" + (.+1|tostring)]|join(","))
generates the header line: it finds the maximum length of any row by generating an array of row lengths, creates a range 0..maximum and maps over it to create an array of "c1".."c7", then joins them all together with commas.
(. | join(","))
is analogous to our second half from earlier, and the same things about@csv
apply.
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
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%2f507615%2fparse-json-data-using-jq-and-convert-into-csv%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
For this exact data,
jq -r '.content.information | [.c1, .c2, .c3] | join(",")' < sample.json
will work - just inserting commas between the three columns replicates the effect of a joined-upCSV file. For more complex real data, particularly with numbers, this will work better:
jq -r '.content.information | [.c1, .c2, .c3|split(",")|.] | @csv' < sample.json
This one is all fairly standard up to .c3|split(",")|.
, which
- Extracts column 3's value
- Splits that value into an array on commas
- Flattens that array out
@csv
then converts the array into CSV format. This produces the following output for your sample file:
"101","11111","a","b","c","d","e:abc."
"102","22222","a."
"103","33333","a","b","c"
"104","44444","a","b"
If you don't want quotes, the most reliable thing is to use @tsv
and just replace the tabs:
jq -r '.content.information|[.c1, .c2, .c3|split(",")|.] | @tsv|gsub("t"; ",")' < sample.json
If all your values are strings you could also use join(",")
again.
If you want the literal header line c1,...c7
created as well, this will do it:
jq -r '[.content.information|[.c1, .c2, .c3|split(",")|.]] | (([range([. | length] | max)|"c" + (.+1|tostring)]|join(",")), (. | join(",")))' < sample.json
There's three parts: the first creates the arrays of columns as before, and then the other two parts use that as input:
([range([. | length] | max)|"c" + (.+1|tostring)]|join(","))
generates the header line: it finds the maximum length of any row by generating an array of row lengths, creates a range 0..maximum and maps over it to create an array of "c1".."c7", then joins them all together with commas.
(. | join(","))
is analogous to our second half from earlier, and the same things about@csv
apply.
add a comment |
For this exact data,
jq -r '.content.information | [.c1, .c2, .c3] | join(",")' < sample.json
will work - just inserting commas between the three columns replicates the effect of a joined-upCSV file. For more complex real data, particularly with numbers, this will work better:
jq -r '.content.information | [.c1, .c2, .c3|split(",")|.] | @csv' < sample.json
This one is all fairly standard up to .c3|split(",")|.
, which
- Extracts column 3's value
- Splits that value into an array on commas
- Flattens that array out
@csv
then converts the array into CSV format. This produces the following output for your sample file:
"101","11111","a","b","c","d","e:abc."
"102","22222","a."
"103","33333","a","b","c"
"104","44444","a","b"
If you don't want quotes, the most reliable thing is to use @tsv
and just replace the tabs:
jq -r '.content.information|[.c1, .c2, .c3|split(",")|.] | @tsv|gsub("t"; ",")' < sample.json
If all your values are strings you could also use join(",")
again.
If you want the literal header line c1,...c7
created as well, this will do it:
jq -r '[.content.information|[.c1, .c2, .c3|split(",")|.]] | (([range([. | length] | max)|"c" + (.+1|tostring)]|join(",")), (. | join(",")))' < sample.json
There's three parts: the first creates the arrays of columns as before, and then the other two parts use that as input:
([range([. | length] | max)|"c" + (.+1|tostring)]|join(","))
generates the header line: it finds the maximum length of any row by generating an array of row lengths, creates a range 0..maximum and maps over it to create an array of "c1".."c7", then joins them all together with commas.
(. | join(","))
is analogous to our second half from earlier, and the same things about@csv
apply.
add a comment |
For this exact data,
jq -r '.content.information | [.c1, .c2, .c3] | join(",")' < sample.json
will work - just inserting commas between the three columns replicates the effect of a joined-upCSV file. For more complex real data, particularly with numbers, this will work better:
jq -r '.content.information | [.c1, .c2, .c3|split(",")|.] | @csv' < sample.json
This one is all fairly standard up to .c3|split(",")|.
, which
- Extracts column 3's value
- Splits that value into an array on commas
- Flattens that array out
@csv
then converts the array into CSV format. This produces the following output for your sample file:
"101","11111","a","b","c","d","e:abc."
"102","22222","a."
"103","33333","a","b","c"
"104","44444","a","b"
If you don't want quotes, the most reliable thing is to use @tsv
and just replace the tabs:
jq -r '.content.information|[.c1, .c2, .c3|split(",")|.] | @tsv|gsub("t"; ",")' < sample.json
If all your values are strings you could also use join(",")
again.
If you want the literal header line c1,...c7
created as well, this will do it:
jq -r '[.content.information|[.c1, .c2, .c3|split(",")|.]] | (([range([. | length] | max)|"c" + (.+1|tostring)]|join(",")), (. | join(",")))' < sample.json
There's three parts: the first creates the arrays of columns as before, and then the other two parts use that as input:
([range([. | length] | max)|"c" + (.+1|tostring)]|join(","))
generates the header line: it finds the maximum length of any row by generating an array of row lengths, creates a range 0..maximum and maps over it to create an array of "c1".."c7", then joins them all together with commas.
(. | join(","))
is analogous to our second half from earlier, and the same things about@csv
apply.
For this exact data,
jq -r '.content.information | [.c1, .c2, .c3] | join(",")' < sample.json
will work - just inserting commas between the three columns replicates the effect of a joined-upCSV file. For more complex real data, particularly with numbers, this will work better:
jq -r '.content.information | [.c1, .c2, .c3|split(",")|.] | @csv' < sample.json
This one is all fairly standard up to .c3|split(",")|.
, which
- Extracts column 3's value
- Splits that value into an array on commas
- Flattens that array out
@csv
then converts the array into CSV format. This produces the following output for your sample file:
"101","11111","a","b","c","d","e:abc."
"102","22222","a."
"103","33333","a","b","c"
"104","44444","a","b"
If you don't want quotes, the most reliable thing is to use @tsv
and just replace the tabs:
jq -r '.content.information|[.c1, .c2, .c3|split(",")|.] | @tsv|gsub("t"; ",")' < sample.json
If all your values are strings you could also use join(",")
again.
If you want the literal header line c1,...c7
created as well, this will do it:
jq -r '[.content.information|[.c1, .c2, .c3|split(",")|.]] | (([range([. | length] | max)|"c" + (.+1|tostring)]|join(",")), (. | join(",")))' < sample.json
There's three parts: the first creates the arrays of columns as before, and then the other two parts use that as input:
([range([. | length] | max)|"c" + (.+1|tostring)]|join(","))
generates the header line: it finds the maximum length of any row by generating an array of row lengths, creates a range 0..maximum and maps over it to create an array of "c1".."c7", then joins them all together with commas.
(. | join(","))
is analogous to our second half from earlier, and the same things about@csv
apply.
edited 38 mins ago
answered 45 mins ago
Michael HomerMichael Homer
49.9k8137175
49.9k8137175
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f507615%2fparse-json-data-using-jq-and-convert-into-csv%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