selecting an array element, based on subelement value -> jq: error: Cannot index array with string #370

Closed
ghost opened this Issue May 22, 2014 · 9 comments

Comments

Projects
None yet
8 participants
@ghost

ghost commented May 22, 2014

my input is

{
  "ACTION": "domain.list",
  "DATA": [
    {
      "TYPE": "slave",
      "AXFR_IPS": "none",
      "DOMAIN": "domain1.com",
      "TTL_SEC": 0,
      "SOA_EMAIL": "",
      "DOMAINID": 11111,
      "DESCRIPTION": "",
      "EXPIRE_SEC": 0,
      "RETRY_SEC": 0,
      "STATUS": 1,
      "LPM_DISPLAYGROUP": "",
      "REFRESH_SEC": 0
    },
    {
      "TYPE": "slave",
      "AXFR_IPS": "none",
      "DOMAIN": "domain2.net",
      "TTL_SEC": 0,
      "SOA_EMAIL": "",
      "DOMAINID": 22222,
      "DESCRIPTION": "",
      "EXPIRE_SEC": 0,
      "RETRY_SEC": 0,
      "STATUS": 1,
      "LPM_DISPLAYGROUP": "",
      "REFRESH_SEC": 0
    },
    {
      "TYPE": "slave",
      "AXFR_IPS": "none",
      "DOMAIN": "domain3",
      "TTL_SEC": 0,
      "SOA_EMAIL": "",
      "DOMAINID": 33333,
      "DESCRIPTION": "",
      "EXPIRE_SEC": 0,
      "RETRY_SEC": 0,
      "STATUS": 1,
      "LPM_DISPLAYGROUP": "",
      "REFRESH_SEC": 0
    },
  ],
  "ERRORARRAY": []
}

I can easily select a specific element in the sequence with

| jq '.DATA | .[2]'

    {
      "TYPE": "slave",
      "AXFR_IPS": "none",
      "DOMAIN": "domain2.net",
      "TTL_SEC": 0,
      "SOA_EMAIL": "",
      "DOMAINID": 22222,
      "DESCRIPTION": "",
      "EXPIRE_SEC": 0,
      "RETRY_SEC": 0,
      "STATUS": 1,
      "LPM_DISPLAYGROUP": "",
      "REFRESH_SEC": 0
    },

or select all specific subelements

| jq '.DATA | .[].DOMAINID'

    "11111"
    "22222"
    "33333"

but can't seem to understand how to pick out ONLY the 'DOMAINID' value, for the element where DOMAIN == 'domain2'. I.e., my desired result is

"22222"

If I try to select 1st

| jq '.DATA | select(.DOMAIN=="domain2")'

I get

jq: error: Cannot index array with string

How do I find/select the desired subelement?

@ghost

This comment has been minimized.

Show comment
Hide comment
@ghost

ghost May 22, 2014

this does the trick

| jq '.DATA[]  | select(.DOMAIN == "domain2") | .DOMAINID'

ghost commented May 22, 2014

this does the trick

| jq '.DATA[]  | select(.DOMAIN == "domain2") | .DOMAINID'

@ghost ghost closed this May 22, 2014

@ghost ghost reopened this May 22, 2014

@ghost ghost closed this May 22, 2014

@gibron

This comment has been minimized.

Show comment
Hide comment
@gibron

gibron Sep 24, 2014

Useful filter use case, thanks.

gibron commented Sep 24, 2014

Useful filter use case, thanks.

@dtolnay dtolnay added the support label Jul 27, 2015

@kovacshuni

This comment has been minimized.

Show comment
Hide comment
@kovacshuni

kovacshuni Jun 22, 2016

used this now, thanks!

used this now, thanks!

@frbrando

This comment has been minimized.

Show comment
Hide comment
@frbrando

frbrando Nov 14, 2017

This is great, thank you for posting!

This is great, thank you for posting!

@ayoubensalem

This comment has been minimized.

Show comment
Hide comment
@ayoubensalem

ayoubensalem Apr 27, 2018

Arigatooo from the future !

ayoubensalem commented Apr 27, 2018

Arigatooo from the future !

@Alexhha

This comment has been minimized.

Show comment
Hide comment
@Alexhha

Alexhha Jun 7, 2018

You can you just

| jq '.DATA[]  | select(.DOMAIN == "domain2").DOMAINID'

Alexhha commented Jun 7, 2018

You can you just

| jq '.DATA[]  | select(.DOMAIN == "domain2").DOMAINID'
@RichardBronosky

This comment has been minimized.

Show comment
Hide comment
@RichardBronosky

RichardBronosky Jun 7, 2018

How would you select any of multiple values for DOMAIN? With python you can do if string in list:

I was hoping for

| jq '.DATA[]  | select(.DOMAIN in ["domain2", "domain3"]).DOMAINID'

RichardBronosky commented Jun 7, 2018

How would you select any of multiple values for DOMAIN? With python you can do if string in list:

I was hoping for

| jq '.DATA[]  | select(.DOMAIN in ["domain2", "domain3"]).DOMAINID'
@pkoppstein

This comment has been minimized.

Show comment
Hide comment
@pkoppstein

pkoppstein Jun 7, 2018

Contributor

@RichardBronosky - The following is one way to achieve what you want and is along the lines of your attempt:

.DATA[] | select(.DOMAIN == ("domain2", "domain3")) | .DOMAINID

If that seems a bit too mysterious, you might prefer something that looks more conventional, such as:

.DATA[] | select(.DOMAIN | IN("domain2","domain3")) | .DOMAINID

If your jq does not have IN/1, you could use this def:

def IN(s): first(select(s == .)) // false;

In future, please ask usage questions at stackoverflow.com using the jq tag: https://stackoverflow.com/questions/tagged/jq

Contributor

pkoppstein commented Jun 7, 2018

@RichardBronosky - The following is one way to achieve what you want and is along the lines of your attempt:

.DATA[] | select(.DOMAIN == ("domain2", "domain3")) | .DOMAINID

If that seems a bit too mysterious, you might prefer something that looks more conventional, such as:

.DATA[] | select(.DOMAIN | IN("domain2","domain3")) | .DOMAINID

If your jq does not have IN/1, you could use this def:

def IN(s): first(select(s == .)) // false;

In future, please ask usage questions at stackoverflow.com using the jq tag: https://stackoverflow.com/questions/tagged/jq

@RichardBronosky

This comment has been minimized.

Show comment
Hide comment
@RichardBronosky

RichardBronosky Jun 7, 2018

@pkoppstein Wow! That was quick. I was in the middle of writing my question on SO when you replied. https://stackoverflow.com/questions/50750688/jq-select-where-attribute-in-list/50750734#50750734

@pkoppstein Wow! That was quick. I was in the middle of writing my question on SO when you replied. https://stackoverflow.com/questions/50750688/jq-select-where-attribute-in-list/50750734#50750734

This issue was closed.

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