Skip to content

Instantly share code, notes, and snippets.

@MarkyMarkMcDonald
Last active May 2, 2017 17:18
Show Gist options
  • Save MarkyMarkMcDonald/594bc7e4213fae8c9c0a011fcfca9027 to your computer and use it in GitHub Desktop.
Save MarkyMarkMcDonald/594bc7e4213fae8c9c0a011fcfca9027 to your computer and use it in GitHub Desktop.
jq filter out children values
echo '{"logs": ["All the cars are really good"],"info": {"cars": [{"name": "another car","type": "big"},{"name": "my car","type": "small"},{"name":"sweet ride", "type":"small"}]}}' | jq '.info.cars |= map(select(.type == "small"))
@MarkyMarkMcDonald
Copy link
Author

MarkyMarkMcDonald commented May 2, 2017

When you have data like:

{
  "logs": [
    "All the cars are really good"
  ],
  "info": {
    "cars": [
      {
        "name": "another car",
        "type": "big"
      },
      {
        "name": "my car",
        "type": "small"
      },
      {
        "name": "sweet ride",
        "type": "small"
      }
    ]
  }
}

And want to filter all the non-small cars without losing your logs:

{
  "logs": [
    "All the cars are really good"
  ],
  "info": {
    "cars": [
      {
        "name": "my car",
        "type": "small"
      },
      {
        "name": "sweet ride",
        "type": "small"
      }
    ]
  }
}

you can use assignment + map + select:

jq '.info.cars |= map(select(.type == "small"))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment