0

When I curl to my URL, I get a JSON in the following format:

{"busyexecutors":0,
 "computers":[{"displayName":"Master","actions":[{},{},{}]},
              {"displayName":"137.0.01","actions":[{},{},{}]}]}

I want to extract only displayName where it's not equal to Master. So the output should be "137.0.0.1".

Let me know whether I can achieve this without external utilities like jq.

4
  • 2
    "I don't want to use extra utilities" - why? Portability? And how broad is your "like" - is Ruby okay (as it's installed on most current Unix-likes)? Because doing this in pure shell is somewhat fragile. Commented Oct 21, 2015 at 1:09
  • I agree with Amadan. Either you are using jq, or you are using something like python and ruby. Pure shell is simply not capable of handling this. Commented Oct 21, 2015 at 1:28
  • Thanks Amadan & skyline..wanted to know whether its possible with just shell. i don't understand ..what do you mean by fragile. Commented Oct 21, 2015 at 5:35
  • Shell utilities are typically line-oriented, whereas structured file formats very often exhibit variability in whitespace, including line breaks; so creating a parser for a structured file format using line-oriented tools tends to produce code which has a number of corner cases where results are invalid and incorrect, or the program simply aborts. This is a FAQ; see e.g. blog.codinghorror.com/parsing-html-the-cthulhu-way (the topic is HTML and regex but it extends readily to JSON and cut | head | tac | rev). Commented Nov 10, 2015 at 7:44

2 Answers 2

2

There are few good reasons not to use a proper JSON parser when working with JSON.

jq -r '.computers | .[].displayName | select(.!="Master")' <<EOF
{"busyexecutors":0,
 "computers":[{"displayName":"Master","actions":[{},{},{}]},
          {"displayName":"137.0.01","actions":[{},{},{}]}]}
EOF
Sign up to request clarification or add additional context in comments.

Comments

1

As has been noted, this is a bad idea. However, if you insist to do it:

#!/usr/bin/awk -f
{
  split($0, z, /"/)
  for (y in z)
    if (z[y] == "displayName")
      if (z[y+2] != "Master")
        print z[y+2]
}

1 Comment

Strictly speaking, awk is also a utility... :P

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.