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

Trouble with an object of objects (no arrays) #57

Closed
jblaine opened this issue Dec 26, 2012 · 3 comments
Closed

Trouble with an object of objects (no arrays) #57

jblaine opened this issue Dec 26, 2012 · 3 comments
Labels

Comments

@jblaine
Copy link

jblaine commented Dec 26, 2012

I'm trying to make productive use of jq (cool tool) with input like the following. I am trying to extract the corresponding object's "dns_name" for any object having a specified "state". I failed to figure out any way to do this from the tutorial and manual. Any ideas?

{
  "i-09xxxxxx": {
    "id": "i-09xxxxxx",
    "keyname": "blah",
    "dns_name": "ec2-REDACTED.compute-1.amazonaws.com",
    "created_at": "Sat Aug 18 18:31:48 UTC 2012",
    "state": "running",
    "tags": "{\"node-type\"=>\"bastion\", \"Name\"=>\"bastion\"}",
    "private_dns_name": "ip-REDACTED.ec2.internal"
  },
  "i-0a8bxxxx": {
    "id": "i-0a8bxxxx",
    "keyname": "jblaine",
    "dns_name": null,
    "created_at": "Wed Dec 26 20:32:15 UTC 2012",
    "state": "terminated",
    "tags": "{\"Created-By\"=>\"Puppet\"}",
    "private_dns_name": null
  },
  "i-cedxxxxx": {
    "id": "i-cedxxxx",
    "keyname": "foo",
    "dns_name": null,
    "created_at": "Thu Sep 20 05:36:41 UTC 2012",
    "state": "stopped",
    "tags": "{\"Name\"=>\"graph-inserter-2\"}",
    "private_dns_name": null
  }
}
@stroan
Copy link
Contributor

stroan commented Dec 26, 2012

jq '.[] | select(.state == "running").dns_name'

This should do the trick I think.

@jblaine
Copy link
Author

jblaine commented Dec 26, 2012

Thank you very much, that works.

I get 90% of why that works. The .[] is magic to me.

"If you use the .[foo] syntax, but omit the index entirely, it will return all of the elements of an array. "

But there was no array. :)

@stroan
Copy link
Contributor

stroan commented Dec 26, 2012

Ah, that must be undocumented. [] on an object returns all the values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants