0

I'm accessing an API that returns JSON with nested objects like this:

{
  "9273": {
    "status": "ok",
    "tag": "group-8",
    "name": "London"
  },
  "4029": {
    "status": "unknown",
    "tag": "group-12",
    "name": "Tokyo"
  },
  "6322": {
    "status": "ok",
    "tag": "group-12",
    "name": "Singapore"
  },
  "1038": {
    "status": "unknown",
    "tag": "group-19",
    "name": "Melbourne"
  },
  "2938": {
    "status": "ok",
    "tag": "group-12",
    "name": "New York"
  }
}

I'm trying to parse the JSON using jq, regex, sed etc. in a Bash script, filter it on status (ok) and tag (highest group with an 'ok'), and flatten the matching objects to get a multi-line sorted string value of their name along with a static prefix (e.g. City).

Desired output below:

City: New York
City: Singapore

I'll appreciate any help in working this out.

1 Answer 1

3

With your data, and using the -r command-line option, the following program produces the output shown below:

[.[]]
| map(select(.status == "ok"))
| (max_by( .tag | sub("group-";"") | tonumber) | .tag) as $mx
| .[]
| select(.tag==$mx)
| "City: \(.name)"

Output:

City: Singapore
City: New York

If you want the cities listed in alphabetical order, then you can add the appropriate sort_by filter to the jq pipeline.

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

5 Comments

Thanks, looks good. Where does the sort_by go in the chain?
Never mind. Figured out the sorting: [.[]] | map(select(.status == "ok")) | (max_by( .tag | sub("group-";"") | tonumber) | .tag) as $mx | [.[]] | map(select(.tag==$mx)) | sort_by(.name) | .[] | "City: \(.name)"
By the way, I made a shorter alternative using group_by based on your answer: [.[]] | map(select(.status == "ok")) | group_by(.tag) | max_by( .[].tag | sub("group-";"") | tonumber) | sort_by(.name)? | .[] | "City: \(.name)". Do you see any problem with it as such?
Yes, there are some efficiency issues. Also, the semantics of max_by(STREAM) is a bit obscure.
Thanks for your time and review. So you reckon group_by is inefficient in this case?

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.