0

I have a Json in a varible $MYVAR like this :

{
"Name": "somename",
"MyField": "{\"mysubfield1\":\"value1\",\"mysubfield2\":\"value2\"}",
"OtherField" : "Othervalues"
...
}

My constraint - I cannot use the jq parser. How can i parse MyField when it's a stringified Json ? I want to extract each value of the subfields - value1 and value2 and store in variables. Is there a direct approach /plain shell commands?

5
  • 2
    I assume you meant jq, not jp. If you can't use that, I think you're in trouble. bash doesn't have very powerful string processing builtins. Commented Mar 18, 2021 at 8:03
  • And parsing JSON is not trivial, you have to deal with escape sequences and nested elements. You should always use a pre-written parser or you won't handle all the corner cases properly. Commented Mar 18, 2021 at 8:05
  • Does this answer your question? Using jq to parse and display multiple fields in a json serially Commented Mar 18, 2021 at 8:05
  • I'm not sure if I'll be able to install jq. Even if I do, how do I take out the \" to make it a normal parsable json? Commented Mar 18, 2021 at 8:16
  • If you can't use jq, use a language with a JSON parser like perl or python, not shell. Commented Mar 18, 2021 at 8:41

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.