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?

BugHunter
  • 61
  • 1
  • 7
  • 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. – Barmar Mar 18 '21 at 08: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. – Barmar Mar 18 '21 at 08:05
  • Does this answer your question? [Using jq to parse and display multiple fields in a json serially](https://stackoverflow.com/questions/28164849/using-jq-to-parse-and-display-multiple-fields-in-a-json-serially) – F. Hauri - Give Up GitHub Mar 18 '21 at 08: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? – BugHunter Mar 18 '21 at 08:16
  • If you can't use `jq`, use a language with a JSON parser like `perl` or `python`, not shell. – Shawn Mar 18 '21 at 08:41

0 Answers0