Turning list of headers and list of values into valid json with jq
I'm working with an endpoint like this:
curl -S www.example.com
[["A", "B", "C", "D"]n
["1","2",null,"4"]]
I'd like to use jq to change this response into proper json rather than a list of headers and a row of data corresponding to the items in the list:
{"A": "1", "B": "2", "C" : null, "D": "4"}
However, what seems like the obvious answer from here
of
curl -S www.example.com | jq '{(.[0]) : .[1] }'
produces
jq: error (at <stdin>:1): Cannot use array (["A...) as object key
And
curl -S www.example.com | jq '{.[0] : .[1] }'
produces
jq: error: syntax error, unexpected '.' (Unix shell quoting issues?) at <top-level>, line 1:
{.[0] : .[1] }
jq: 1 compile error
What's the right syntax for
[[HEADER1,HEADER2,...]n
[DATA1,DATA2]]
responses into json?
Are the unquoted null and the newline the source of my problem?
json jq
add a comment |
I'm working with an endpoint like this:
curl -S www.example.com
[["A", "B", "C", "D"]n
["1","2",null,"4"]]
I'd like to use jq to change this response into proper json rather than a list of headers and a row of data corresponding to the items in the list:
{"A": "1", "B": "2", "C" : null, "D": "4"}
However, what seems like the obvious answer from here
of
curl -S www.example.com | jq '{(.[0]) : .[1] }'
produces
jq: error (at <stdin>:1): Cannot use array (["A...) as object key
And
curl -S www.example.com | jq '{.[0] : .[1] }'
produces
jq: error: syntax error, unexpected '.' (Unix shell quoting issues?) at <top-level>, line 1:
{.[0] : .[1] }
jq: 1 compile error
What's the right syntax for
[[HEADER1,HEADER2,...]n
[DATA1,DATA2]]
responses into json?
Are the unquoted null and the newline the source of my problem?
json jq
add a comment |
I'm working with an endpoint like this:
curl -S www.example.com
[["A", "B", "C", "D"]n
["1","2",null,"4"]]
I'd like to use jq to change this response into proper json rather than a list of headers and a row of data corresponding to the items in the list:
{"A": "1", "B": "2", "C" : null, "D": "4"}
However, what seems like the obvious answer from here
of
curl -S www.example.com | jq '{(.[0]) : .[1] }'
produces
jq: error (at <stdin>:1): Cannot use array (["A...) as object key
And
curl -S www.example.com | jq '{.[0] : .[1] }'
produces
jq: error: syntax error, unexpected '.' (Unix shell quoting issues?) at <top-level>, line 1:
{.[0] : .[1] }
jq: 1 compile error
What's the right syntax for
[[HEADER1,HEADER2,...]n
[DATA1,DATA2]]
responses into json?
Are the unquoted null and the newline the source of my problem?
json jq
I'm working with an endpoint like this:
curl -S www.example.com
[["A", "B", "C", "D"]n
["1","2",null,"4"]]
I'd like to use jq to change this response into proper json rather than a list of headers and a row of data corresponding to the items in the list:
{"A": "1", "B": "2", "C" : null, "D": "4"}
However, what seems like the obvious answer from here
of
curl -S www.example.com | jq '{(.[0]) : .[1] }'
produces
jq: error (at <stdin>:1): Cannot use array (["A...) as object key
And
curl -S www.example.com | jq '{.[0] : .[1] }'
produces
jq: error: syntax error, unexpected '.' (Unix shell quoting issues?) at <top-level>, line 1:
{.[0] : .[1] }
jq: 1 compile error
What's the right syntax for
[[HEADER1,HEADER2,...]n
[DATA1,DATA2]]
responses into json?
Are the unquoted null and the newline the source of my problem?
json jq
json jq
edited Nov 20 '18 at 7:00
peak
30.8k83957
30.8k83957
asked Nov 19 '18 at 21:47
MittenchopsMittenchops
6,4042165137
6,4042165137
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
According to what's posted, the output of the curl command is so weird that it would require special processing. Chances are the output is not as weird as shown, and the sed
command shown as part of the solution below might need tweaking.
In any case, with the input as shown:
sed 's/\"/"/;1s/\n/,/' input.txt | jq '
.[0] as $h
| .[1]
| . as $in
| reduce range(0;length) as $i
({}; .[$h[$i]] = $in[$i])'
produces the desired output.
One-stop shopping
jq -Rs < input.txt '
split("n")
| map(select(length>0))
| ( .[0] | sub("[\\"; "") | sub("\\n"; "") | fromjson) as $h
| .[1]
| sub("\]\]"; "]")
| fromjson
| . as $in
| reduce range(0;length) as $i
({}; .[$h[$i]] = $in[$i])'
Wow, you are very skilled at jq. Thank you!
– Mittenchops
Nov 19 '18 at 23:56
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fstackoverflow.com%2fquestions%2f53383105%2fturning-list-of-headers-and-list-of-values-into-valid-json-with-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
According to what's posted, the output of the curl command is so weird that it would require special processing. Chances are the output is not as weird as shown, and the sed
command shown as part of the solution below might need tweaking.
In any case, with the input as shown:
sed 's/\"/"/;1s/\n/,/' input.txt | jq '
.[0] as $h
| .[1]
| . as $in
| reduce range(0;length) as $i
({}; .[$h[$i]] = $in[$i])'
produces the desired output.
One-stop shopping
jq -Rs < input.txt '
split("n")
| map(select(length>0))
| ( .[0] | sub("[\\"; "") | sub("\\n"; "") | fromjson) as $h
| .[1]
| sub("\]\]"; "]")
| fromjson
| . as $in
| reduce range(0;length) as $i
({}; .[$h[$i]] = $in[$i])'
Wow, you are very skilled at jq. Thank you!
– Mittenchops
Nov 19 '18 at 23:56
add a comment |
According to what's posted, the output of the curl command is so weird that it would require special processing. Chances are the output is not as weird as shown, and the sed
command shown as part of the solution below might need tweaking.
In any case, with the input as shown:
sed 's/\"/"/;1s/\n/,/' input.txt | jq '
.[0] as $h
| .[1]
| . as $in
| reduce range(0;length) as $i
({}; .[$h[$i]] = $in[$i])'
produces the desired output.
One-stop shopping
jq -Rs < input.txt '
split("n")
| map(select(length>0))
| ( .[0] | sub("[\\"; "") | sub("\\n"; "") | fromjson) as $h
| .[1]
| sub("\]\]"; "]")
| fromjson
| . as $in
| reduce range(0;length) as $i
({}; .[$h[$i]] = $in[$i])'
Wow, you are very skilled at jq. Thank you!
– Mittenchops
Nov 19 '18 at 23:56
add a comment |
According to what's posted, the output of the curl command is so weird that it would require special processing. Chances are the output is not as weird as shown, and the sed
command shown as part of the solution below might need tweaking.
In any case, with the input as shown:
sed 's/\"/"/;1s/\n/,/' input.txt | jq '
.[0] as $h
| .[1]
| . as $in
| reduce range(0;length) as $i
({}; .[$h[$i]] = $in[$i])'
produces the desired output.
One-stop shopping
jq -Rs < input.txt '
split("n")
| map(select(length>0))
| ( .[0] | sub("[\\"; "") | sub("\\n"; "") | fromjson) as $h
| .[1]
| sub("\]\]"; "]")
| fromjson
| . as $in
| reduce range(0;length) as $i
({}; .[$h[$i]] = $in[$i])'
According to what's posted, the output of the curl command is so weird that it would require special processing. Chances are the output is not as weird as shown, and the sed
command shown as part of the solution below might need tweaking.
In any case, with the input as shown:
sed 's/\"/"/;1s/\n/,/' input.txt | jq '
.[0] as $h
| .[1]
| . as $in
| reduce range(0;length) as $i
({}; .[$h[$i]] = $in[$i])'
produces the desired output.
One-stop shopping
jq -Rs < input.txt '
split("n")
| map(select(length>0))
| ( .[0] | sub("[\\"; "") | sub("\\n"; "") | fromjson) as $h
| .[1]
| sub("\]\]"; "]")
| fromjson
| . as $in
| reduce range(0;length) as $i
({}; .[$h[$i]] = $in[$i])'
edited Nov 20 '18 at 3:34
answered Nov 19 '18 at 22:22
peakpeak
30.8k83957
30.8k83957
Wow, you are very skilled at jq. Thank you!
– Mittenchops
Nov 19 '18 at 23:56
add a comment |
Wow, you are very skilled at jq. Thank you!
– Mittenchops
Nov 19 '18 at 23:56
Wow, you are very skilled at jq. Thank you!
– Mittenchops
Nov 19 '18 at 23:56
Wow, you are very skilled at jq. Thank you!
– Mittenchops
Nov 19 '18 at 23:56
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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%2fstackoverflow.com%2fquestions%2f53383105%2fturning-list-of-headers-and-list-of-values-into-valid-json-with-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