0

I have following JSON which I get using API. The json has 2 list, 1- main_roles and 2- cc_roles. Using following code I can get 1 list. But I want to get both list in dropdown(display, value).

How can I get both list 1- main_roles and 2- cc_roles in 1 jq using map?

JSON

{
    "invoice": {
        "invoice_id": "b494a46396e47726873708f71a71a228ba2782fa",
        "main_roles": [
            {
                "name": "Agent"
            },
            {
                "name": "Customer"
            }
        ],
        "cc_roles": [
            {
                "name": "Manager"
            }
        ]        
    }
}

JSON Code

"select": {
  "label": "Invoice",
  "form_fields": [
    {
      "label": "Select Role",
      "id": "selectRole",
      "type": "dropdown",
      "required": true,
      "options": {
        "!pipe": [
          {
            "!http": {
              "method": "GET",
              "path": "https://api.test.com/getroles"
            }
          },
          {
            "!jq": ".invoice.main_roles | map({ display: .name , value: .name })"
          }
        ]
      }
    }
  ]
}
NJ Bhanushali
  • 901
  • 1
  • 12
  • 21

1 Answers1

1

If you want to get both lists as separate arrays, simply call them one after another, separated by a comma ,:

.invoice | .main_roles, .cc_roles | map({ display: .name , value: .name })
[
  {
    "display": "Agent",
    "value": "Agent"
  },
  {
    "display": "Customer",
    "value": "Customer"
  }
]
[
  {
    "display": "Manager",
    "value": "Manager"
  }
]

Demo

If you want them combined into one array, add them together using +:

.invoice | .main_roles + .cc_roles | map({ display: .name , value: .name })
[
  {
    "display": "Agent",
    "value": "Agent"
  },
  {
    "display": "Customer",
    "value": "Customer"
  },
  {
    "display": "Manager",
    "value": "Manager"
  }
]

Demo

pmf
  • 24,478
  • 2
  • 22
  • 31
  • This is really useful. Can you please suggest me how can I combine both in one array if the field name is different for both list. i.e if the first list `main_roles`, field is `name` and second list `cc_roles `, field is `fullname` Demo: https://jqplay.org/s/EEpR63qidl – NJ Bhanushali Feb 08 '22 at 06:15
  • 1
    If you always have just one item in the object (whatever it be named, `name`, `fullname` etc), you can omit writing it out and just use `{ display: .[] , value: .[] }` ([Demo](https://jqplay.org/s/Z4BGbY5Zm0)). In any other case you'd need to generate the object first (separately for both inputs), and add them together afterwards ([Demo](https://jqplay.org/s/fMrAqdlBAm)). – pmf Feb 08 '22 at 06:19
  • 1
    This `[.invoice | .main_roles, .cc_roles | .[][] | {display:., value:.}]` is a kind of minimal solution ([Demo](https://jqplay.org/s/4uQUJ5SZkr)) – pmf Feb 08 '22 at 06:35
  • I have added another question with same subject. Tried hard but didn't get the expected result. Can you please review and help me? https://stackoverflow.com/questions/71103776/how-to-get-key-value-pairs-of-the-objects-from-complex-json-using-jq-and-map-a – NJ Bhanushali Feb 13 '22 at 18:40