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

Comments

Projects
None yet
5 participants
@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

This comment has been minimized.

Show comment
Hide comment
@vito-c

vito-c 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 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

This comment has been minimized.

Show comment
Hide comment
@vito-c

vito-c Oct 31, 2014

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

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

This comment has been minimized.

Show comment
Hide comment
@jacquestardie

jacquestardie 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.

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

This comment has been minimized.

Show comment
Hide comment
@wtlangford

wtlangford Nov 4, 2014

Collaborator

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).

Collaborator

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

This comment has been minimized.

Show comment
Hide comment
@jacquestardie

jacquestardie 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.

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

This comment has been minimized.

Show comment
Hide comment
@wtlangford

wtlangford Nov 4, 2014

Collaborator

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

Collaborator

wtlangford commented Nov 4, 2014

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

@jacquestardie

This comment has been minimized.

Show comment
Hide comment
@jacquestardie

jacquestardie commented Nov 4, 2014

❤️

@vito-c

This comment has been minimized.

Show comment
Hide comment
@vito-c

vito-c 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

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

This comment has been minimized.

Show comment
Hide comment
@jacquestardie

jacquestardie Nov 5, 2014

Thanks everyone, appreciate the help.

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