jq parsing json out of json values
up vote
1
down vote
favorite
I have something like the following
echo "{"t":"set foo='{\"mode\":1}'"}"|jq .
{
"t": "set foo='{"mode":1}'"
}
and I'd like the output to look something like
{
"t": "set foo='{"mode":1}'",
"mode": 1
}
Right now I am making several execs to make this happen and would like to see if its possible into one jq call.
bash shell-script jq
add a comment |
up vote
1
down vote
favorite
I have something like the following
echo "{"t":"set foo='{\"mode\":1}'"}"|jq .
{
"t": "set foo='{"mode":1}'"
}
and I'd like the output to look something like
{
"t": "set foo='{"mode":1}'",
"mode": 1
}
Right now I am making several execs to make this happen and would like to see if its possible into one jq call.
bash shell-script jq
It's unclear why you can't just add the"mode": 1
bit to ourecho
.
– Kusalananda
Dec 7 at 20:11
1
It sounds more like testing challenge, but it shouldn't be treated as applicable and practical example. I can present a solution, but the point is that you need to manage your input to not get into such fragile pipes
– RomanPerekhrest
Dec 7 at 20:14
This is an example of a real world example. In this case there is no control over the input, for it is a complex pipe.
– winmutt
Dec 7 at 21:06
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have something like the following
echo "{"t":"set foo='{\"mode\":1}'"}"|jq .
{
"t": "set foo='{"mode":1}'"
}
and I'd like the output to look something like
{
"t": "set foo='{"mode":1}'",
"mode": 1
}
Right now I am making several execs to make this happen and would like to see if its possible into one jq call.
bash shell-script jq
I have something like the following
echo "{"t":"set foo='{\"mode\":1}'"}"|jq .
{
"t": "set foo='{"mode":1}'"
}
and I'd like the output to look something like
{
"t": "set foo='{"mode":1}'",
"mode": 1
}
Right now I am making several execs to make this happen and would like to see if its possible into one jq call.
bash shell-script jq
bash shell-script jq
asked Dec 7 at 19:57
winmutt
1061
1061
It's unclear why you can't just add the"mode": 1
bit to ourecho
.
– Kusalananda
Dec 7 at 20:11
1
It sounds more like testing challenge, but it shouldn't be treated as applicable and practical example. I can present a solution, but the point is that you need to manage your input to not get into such fragile pipes
– RomanPerekhrest
Dec 7 at 20:14
This is an example of a real world example. In this case there is no control over the input, for it is a complex pipe.
– winmutt
Dec 7 at 21:06
add a comment |
It's unclear why you can't just add the"mode": 1
bit to ourecho
.
– Kusalananda
Dec 7 at 20:11
1
It sounds more like testing challenge, but it shouldn't be treated as applicable and practical example. I can present a solution, but the point is that you need to manage your input to not get into such fragile pipes
– RomanPerekhrest
Dec 7 at 20:14
This is an example of a real world example. In this case there is no control over the input, for it is a complex pipe.
– winmutt
Dec 7 at 21:06
It's unclear why you can't just add the
"mode": 1
bit to our echo
.– Kusalananda
Dec 7 at 20:11
It's unclear why you can't just add the
"mode": 1
bit to our echo
.– Kusalananda
Dec 7 at 20:11
1
1
It sounds more like testing challenge, but it shouldn't be treated as applicable and practical example. I can present a solution, but the point is that you need to manage your input to not get into such fragile pipes
– RomanPerekhrest
Dec 7 at 20:14
It sounds more like testing challenge, but it shouldn't be treated as applicable and practical example. I can present a solution, but the point is that you need to manage your input to not get into such fragile pipes
– RomanPerekhrest
Dec 7 at 20:14
This is an example of a real world example. In this case there is no control over the input, for it is a complex pipe.
– winmutt
Dec 7 at 21:06
This is an example of a real world example. In this case there is no control over the input, for it is a complex pipe.
– winmutt
Dec 7 at 21:06
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
For this exact input,
jq '. + (.t[9:-1] | fromjson)'
will do what you want. It extracts characters {"mode":1}
from the string (starting at 9, omitting the last one) so that it leaves off the single quotes on both sides, then parses it as JSON into an object with fromjson
, and finally merges that object ({"mode": 1}
) with the original input (.
) using +
.
You will need to adjust the indices to match your real data. If you need to find where the opening '
is, (.t|index("u0027")+1)
will work as a replacement for 9
; if you need to parse it out more thoroughly, ask a fresh question.
This didn't work out of the back but I'll fiddle around some.
– winmutt
Dec 7 at 21:20
The comment you posted pre-edit seemed to show it working and the output is verbatim what's in the question; if it's not what you wanted, you can edit the question to say what you actually wanted
– Michael Homer
Dec 7 at 21:29
You are correct, I misread the output. This ended up working fine along with some other tweaks. Thanks again!
– winmutt
Dec 7 at 21:53
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%2f486658%2fjq-parsing-json-out-of-json-values%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
1
down vote
For this exact input,
jq '. + (.t[9:-1] | fromjson)'
will do what you want. It extracts characters {"mode":1}
from the string (starting at 9, omitting the last one) so that it leaves off the single quotes on both sides, then parses it as JSON into an object with fromjson
, and finally merges that object ({"mode": 1}
) with the original input (.
) using +
.
You will need to adjust the indices to match your real data. If you need to find where the opening '
is, (.t|index("u0027")+1)
will work as a replacement for 9
; if you need to parse it out more thoroughly, ask a fresh question.
This didn't work out of the back but I'll fiddle around some.
– winmutt
Dec 7 at 21:20
The comment you posted pre-edit seemed to show it working and the output is verbatim what's in the question; if it's not what you wanted, you can edit the question to say what you actually wanted
– Michael Homer
Dec 7 at 21:29
You are correct, I misread the output. This ended up working fine along with some other tweaks. Thanks again!
– winmutt
Dec 7 at 21:53
add a comment |
up vote
1
down vote
For this exact input,
jq '. + (.t[9:-1] | fromjson)'
will do what you want. It extracts characters {"mode":1}
from the string (starting at 9, omitting the last one) so that it leaves off the single quotes on both sides, then parses it as JSON into an object with fromjson
, and finally merges that object ({"mode": 1}
) with the original input (.
) using +
.
You will need to adjust the indices to match your real data. If you need to find where the opening '
is, (.t|index("u0027")+1)
will work as a replacement for 9
; if you need to parse it out more thoroughly, ask a fresh question.
This didn't work out of the back but I'll fiddle around some.
– winmutt
Dec 7 at 21:20
The comment you posted pre-edit seemed to show it working and the output is verbatim what's in the question; if it's not what you wanted, you can edit the question to say what you actually wanted
– Michael Homer
Dec 7 at 21:29
You are correct, I misread the output. This ended up working fine along with some other tweaks. Thanks again!
– winmutt
Dec 7 at 21:53
add a comment |
up vote
1
down vote
up vote
1
down vote
For this exact input,
jq '. + (.t[9:-1] | fromjson)'
will do what you want. It extracts characters {"mode":1}
from the string (starting at 9, omitting the last one) so that it leaves off the single quotes on both sides, then parses it as JSON into an object with fromjson
, and finally merges that object ({"mode": 1}
) with the original input (.
) using +
.
You will need to adjust the indices to match your real data. If you need to find where the opening '
is, (.t|index("u0027")+1)
will work as a replacement for 9
; if you need to parse it out more thoroughly, ask a fresh question.
For this exact input,
jq '. + (.t[9:-1] | fromjson)'
will do what you want. It extracts characters {"mode":1}
from the string (starting at 9, omitting the last one) so that it leaves off the single quotes on both sides, then parses it as JSON into an object with fromjson
, and finally merges that object ({"mode": 1}
) with the original input (.
) using +
.
You will need to adjust the indices to match your real data. If you need to find where the opening '
is, (.t|index("u0027")+1)
will work as a replacement for 9
; if you need to parse it out more thoroughly, ask a fresh question.
answered Dec 7 at 20:28
Michael Homer
45.5k7121160
45.5k7121160
This didn't work out of the back but I'll fiddle around some.
– winmutt
Dec 7 at 21:20
The comment you posted pre-edit seemed to show it working and the output is verbatim what's in the question; if it's not what you wanted, you can edit the question to say what you actually wanted
– Michael Homer
Dec 7 at 21:29
You are correct, I misread the output. This ended up working fine along with some other tweaks. Thanks again!
– winmutt
Dec 7 at 21:53
add a comment |
This didn't work out of the back but I'll fiddle around some.
– winmutt
Dec 7 at 21:20
The comment you posted pre-edit seemed to show it working and the output is verbatim what's in the question; if it's not what you wanted, you can edit the question to say what you actually wanted
– Michael Homer
Dec 7 at 21:29
You are correct, I misread the output. This ended up working fine along with some other tweaks. Thanks again!
– winmutt
Dec 7 at 21:53
This didn't work out of the back but I'll fiddle around some.
– winmutt
Dec 7 at 21:20
This didn't work out of the back but I'll fiddle around some.
– winmutt
Dec 7 at 21:20
The comment you posted pre-edit seemed to show it working and the output is verbatim what's in the question; if it's not what you wanted, you can edit the question to say what you actually wanted
– Michael Homer
Dec 7 at 21:29
The comment you posted pre-edit seemed to show it working and the output is verbatim what's in the question; if it's not what you wanted, you can edit the question to say what you actually wanted
– Michael Homer
Dec 7 at 21:29
You are correct, I misread the output. This ended up working fine along with some other tweaks. Thanks again!
– winmutt
Dec 7 at 21:53
You are correct, I misread the output. This ended up working fine along with some other tweaks. Thanks again!
– winmutt
Dec 7 at 21:53
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f486658%2fjq-parsing-json-out-of-json-values%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
It's unclear why you can't just add the
"mode": 1
bit to ourecho
.– Kusalananda
Dec 7 at 20:11
1
It sounds more like testing challenge, but it shouldn't be treated as applicable and practical example. I can present a solution, but the point is that you need to manage your input to not get into such fragile pipes
– RomanPerekhrest
Dec 7 at 20:14
This is an example of a real world example. In this case there is no control over the input, for it is a complex pipe.
– winmutt
Dec 7 at 21:06