0

I have a file with multiple lines like the following string. I need to extract the values of id and obj.

{"Class":"ZONE","id":"DEV100.ZN301","name":"3109 E BTM","zgroup":"EAST FLOOR 3","prog":"","members":[{"obj":"DEV300.WC3"},{"obj":"DEV300.WC4"},{"obj":"DEV300.WC7"},{"obj":"DEV300.WC10"}]}

I am using the following command to obtain the output:

[user@server ~]$ cat file.txt | grep "\"Class\":\"ZONE\"" | while IFS="," read -r a b c d e f ; do echo "$b$f";done | grep ZN301

Output:

"id":"DEV100.ZN301""members":[{"obj":"DEV300.WC3"},{"obj":"DEV300.WC4"},{"obj":"DEV300.WC7"},{"obj":"DEV300.WC10"}]}

My aim is to get the following Output:

DEV100.ZN301 : DEV300.WC3 , DEV300.WC4 , DEV300.WC7 , DEV300.WC10

Please help me out. Thanks!

2
  • 1
    The sane way to do this is to use jq. Trying to parse JSON with tools that don't understand its syntax is innately error-prone. Commented May 31, 2018 at 0:00
  • Keep in mind that if the program generating your files suddenly starts, say, sorting the keys, to make the lines instead look like {"Class":"ZONE","id":"DEV100.ZN301","members":[{"obj":"DEV300.WC3"},{"obj":"DEV300.WC4"},{"obj":"DEV300.WC7"},{"obj":"DEV300.WC10"}],"name":"3109 E BTM","prog":"","zgroup":"EAST FLOOR 3"}, that would have exactly the same semantics for any compliant JSON parser, but would break your code that depends on ordering utterly. And that kind of thing can happen just with library version changes and no code updates to the tool itself at all. Commented May 31, 2018 at 0:02

1 Answer 1

1
jq -r 'select(.Class == "ZONE") | (.id + " : " + ([.members[] | .obj] | join(" , ")))'

...or, leaning on a Python interpreter:

parse_json() {
  python -c '
import sys, json
for line in sys.stdin:
  line = line.strip()    # ignore trailing newlines
  if not line: continue  # skip blank lines
  doc = json.loads(line)
  if doc.get("Class") != "ZONE":
    continue
  line_id = doc.get("id")
  objs = [m.get("obj") for m in doc.get("members", [])]
  sys.stdout.write("%s : %s\n" % (line_id, " , ".join(objs)))
'
}

parse_json 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the Answer Charles, but I cannot install custom packages on the machines and i have 1000s of such machines. So I am trying to get a shell command that will help me out.

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.