Home > OS >  How to get multiple list from one response in App Studio using jq and map? (Active Campaign)
How to get multiple list from one response in App Studio using jq and map? (Active Campaign)

Time:02-08

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 })"
          }
        ]
      }
    }
  ]
}

CodePudding user response:

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

  •  Tags:  
  • Related