0

I have a json string as the following:

{"hasErrors":"true", "exceptions":"..........", "hasRight":"false"}

I need the result from hasRight and hasErrors

so that I can do the following comparison operation:

if ["$RESULT" == "true"]; then
exit 0
elif ["$ERR" == "true"]; then
    exit 400
else
    exit 404
fi

Note that the position of the hasError and hasRight keys may not remain the same. e.g

{"exceptions":"..........", "hasRight":"false", "hasErrors":"true"}
  • 2
    If this is JSON, you should be asking how to parse JSON in bash. – Charles Duffy Jun 20 '16 at 15:38
  • 1
    BTW, `["$RESULT" == "true"]` will never work. Spaces are important. Also, `[` (aka `test`) is specified with `=`, not `==`, as the string comparison operator, so using `==` is relying on an extension which any given shell may or may not provide. – Charles Duffy Jun 20 '16 at 15:39
  • Also, `400` and `404` are not valid exit status values: Exit status is a single byte, so these numbers are too large to be passed in that way. – Charles Duffy Jun 20 '16 at 15:40
  • 1
    http://stackoverflow.com/q/1955505/1030675 – choroba Jun 20 '16 at 15:40
  • See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html re: the POSIX specification for `test`, for which `[` is a synonym. Just as you can't run `test"$RESULT" = true` without a space between the `test` and the `$RESULT`, you also can't run `["$RESULT" = true]` with no spaces. – Charles Duffy Jun 20 '16 at 15:42
  • btw, re: variable names, all-caps names are used for variables that have impact on, or are set by, components of the shell or operating system; it's the set of variables with at least one lower-case character in their names reserved for application use. See the fourth paragraph of http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap08.html, keeping in mind that shell and environment variables share a namespace. – Charles Duffy Jun 20 '16 at 15:49
  • you roasted me bruh –  Jun 20 '16 at 15:51

1 Answers1

1
result=$(jq -r '.hasErrors' <<<"$json_string")
if [[ $result = true ]]; then
  ...etc...
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • why the double `[[ ]]` ? and isn't it `==`? –  Jun 20 '16 at 15:41
  • @Price, the POSIX-compliant string comparison operator is `=`, not `==`. See the link to the POSIX spec for `test` I gave you in a comment on the question. – Charles Duffy Jun 20 '16 at 15:43
  • @Price, ...and `[[` is a bash and ksh builtin that's an improved alternative to `test`. Among other things, this alternative disables string-splitting and glob expansion (so you don't need to quote on the left-hand side), adds more syntax (like regex and glob-pattern tests), and explicitly allows `==` as a synonym (though, being as that synonym will lead you into compatibility bugs if using it in places where only POSIX constructs are allowed, I suggest avoiding it regardless). – Charles Duffy Jun 20 '16 at 15:44
  • ...see BashFAQ #31, at http://mywiki.wooledge.org/BashFAQ/031, for an extended discussion of `[` vs `[[`. – Charles Duffy Jun 20 '16 at 15:46
  • it didn't work. `jq: command not found` –  Jun 20 '16 at 15:50
  • Obviously, if you don't have it already, `jq` is a thing that you need to install. Alternately, if you can't install new software, you can use the Python json module -- we have many answers on similar questions already in the knowledge base showing you how to do this. – Charles Duffy Jun 20 '16 at 15:52
  • isn't it possible without any kind of extra library? without python and without this jq? –  Jun 20 '16 at 15:59
  • Someone has written an actual JSON parser in native bash, but that's still a library -- it's enough code you wouldn't want to copy-and-paste it around. Real parsers are necessarily complex, and anything else you do will be an error-prone hack. – Charles Duffy Jun 20 '16 at 16:06