Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Selection use case #610

Closed
vito-c opened this issue Oct 31, 2014 · 9 comments
Closed

Selection use case #610

vito-c opened this issue Oct 31, 2014 · 9 comments

Comments

@vito-c
Copy link

@vito-c vito-c commented Oct 31, 2014

Here is a quick tip for people trying to select objects. Let's say I want to select all the objects that have the "d" key below

object:
{
  "id": 123,
  "a": {
    "b": {
      "c": {
        "d": 10
      }
    },
    "e": {
      "d": 5
    }
  },
  "r": {
    "d": {
      "foo": 42
    }
  },
  "d": 9
}

the correct jq filter would be jq ' ..|select(has("d"))?'
and if you wanted to find only the object that has the "foo" key equal to 42 you'd do jq ' ..|select(has("d"))?|select(.d.foo =42)'

{
  "d": {
    "foo": 42
  }
}

cli reference:
echo '{ "id": 123,"a": {"b": {"c": {"d": 10}},"e": {"d": 5}} , "r":{"d":{"foo":42}}, "d":9}'

@vito-c
Copy link
Author

@vito-c vito-c commented Oct 31, 2014

@nicowilliams could you tag this as an example or use case? I tried using https://jqplay.org/ to create the example but was unable to get it to save my json.

@vito-c
Copy link
Author

@vito-c vito-c commented Oct 31, 2014

I'll also create an issue later with some more tickets to be added to the tag. Thanks!

@jacquestardie
Copy link

@jacquestardie jacquestardie commented Nov 4, 2014

@vito-c, I'm trying to a query similar to what you've described here over on stackoverflow.

I tried running the example I provided with jq ' .. | select('MODE = "D")', but am getting all sorts ofCannot index number with string` errors.

@wtlangford
Copy link
Collaborator

@wtlangford wtlangford commented Nov 4, 2014

Try .features[] | select(.properties.MODE == "D")

On Tue, Nov 4, 2014, 12:44 jacques notifications@github.com wrote:

@vito-c https://github.com/vito-c, I'm trying to a query similar to
what you've described here over on stackoverflow
https://stackoverflow.com/questions/26741075/filtering-geojson-with-jq.

I tried running the example I provided with jq ' .. | select('MODE =
"D")', but am getting all sorts ofCannot index number with string` errors.


Reply to this email directly or view it on GitHub
#610 (comment).

@jacquestardie
Copy link

@jacquestardie jacquestardie commented Nov 4, 2014

Thanks @wtlangford! That's giving me this:

{
  "type": "Feature",
  "properties": {
    "MODE": "D"
  },
  "geometry": {
    "type": "Point",
    "coordinates": [
      -69.23651039600372,
      45.46053888199693
    ]
  }
}

Much closer, the only remaining issue is that it's stripping out the "type": "FeatureCollection", key, which I need to use this as GeoJSON.

@wtlangford
Copy link
Collaborator

@wtlangford wtlangford commented Nov 4, 2014

Then in that case...
.features |= [.[] | select(.properties.MODE == "D")] does what you want.

@jacquestardie
Copy link

@jacquestardie jacquestardie commented Nov 4, 2014

❤️

@vito-c
Copy link
Author

@vito-c vito-c commented Nov 5, 2014

jq ' .. | select( has("properties") )? | select( .properties.MODE == "D")'
The ? tells jq to ignore errors. The .. is to recurse into the object

jq '.features[] | select(.properties.MODE == "D")'
Will get you the results you are after without recursion just to note the differences in the methods

.type as $type | .features[] | { type: $type, features: [select(.properties.MODE == "D") ] } | select(.features != [])
This will also work but is more verbose but has no recursion. @wtlangford solutions are better for this use case. I just wanted to point out what the usage of the ? and .. operators

@jacquestardie
Copy link

@jacquestardie jacquestardie commented Nov 5, 2014

Thanks everyone, appreciate the help.

@dtolnay dtolnay closed this Jul 25, 2015
@dtolnay dtolnay added support and removed query labels Jul 25, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants
You can’t perform that action at this time.