1

With the following JSON:

{
   "resource_access":{
      "info-bi-test":{
         "roles":[
            "DB7"
         ]
      }
   }
}

How to add another value to an array resource_access."info-bi-test".roles with JMESPath?

I read the documentation of JMESPath and found the merge function, but it only works with objects.

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
  • How do you want to declare which list should be an item added to? Imagine, there might `{"roles1":["DB7"], "roles2":["XP7"]}`. – Vladimir Botka Mar 29 '23 at 14:39

1 Answers1

0

The basic idea to add item(s) to an existing array is to create an array of array(s) and then flatten it.

As an example, a totally static query like

[['a', 'b', 'c'], ['d', 'e']][]

Will give:

[
  "a",
  "b",
  "c",
  "d",
  "e"
]

Now, since you are not giving a desired output, there are multiple cases:

  1. You want to add one hardcoded item to the array resource_access."info-bi-test".roles, and have a simple array as return, with a desired output like
    ["DB7", "DB9"]
    
    Then, this could be achieved with the query:
    [resource_access."info-bi-test".roles, 'DB9'][]
    
  2. You want to add multiple items to the array resource_access."info-bi-test".roles, and have a simple array as return, with a desired output like
    ["DB7", "DB9", "DB11"]
    
    Then, this could be achieved with the query:
    [resource_access."info-bi-test".roles, ['DB9', 'DB11']][]
    
  3. You want to add one item to the array resource_access."info-bi-test".roles form somewhere else in the JSON structure, with an input like
    {
      "resource_access":{
        "info-bi-test":{
          "roles":[
            "DB7"
          ]
        }
      },
      "default_role": "DB9"
    }
    
    and have a simple array as return, with a desired output like
    ["DB7", "DB9"]
    
    Then, this could be achieved with the query:
    [resource_access."info-bi-test".roles, default_role][]
    
  4. You want to add multiple items to the array resource_access."info-bi-test".roles form somewhere else in the JSON structure, with an input like
    {
      "resource_access": {
        "info-bi-test": {
          "roles":[
            "DB7"
          ]
        }
      },
      "default_roles": [
        "DB9",
        "DB11"
      ]
    }
    
    and have a simple array as return, with a desired output like
    ["DB7", "DB9", "DB11"]
    
    Then, this could be achieved with the query:
    [resource_access."info-bi-test".roles, default_roles][]
    
  5. You want to add one hardcoded item to the array resource_access."info-bi-test".roles, but you want the whole object structure returned, with a desired output like
    {
      "resource_access": {
        "info-bi-test": {
          "roles": [
            "DB7",
            "DB9"
          ]
        }
      }
    }
    
    Then, you will have to recreate the object structure in the query:
    {
      resource_access: {
        "info-bi-test": {
          roles: [resource_access."info-bi-test".roles, 'DB9'][]
        }
      }
    }
    
  6. You want to add multiple hardcoded items to the array resource_access."info-bi-test".roles, but you want the whole object structure returned, with a desired output like
    {
      "resource_access":{
        "info-bi-test":{
          "roles":[
            "DB7",
            "DB9",
            "DB11"
          ]
        }
      }
    }
    
    Then, you will have to recreate the object structure in the query:
    {
      resource_access: {
        "info-bi-test": {
          roles: [resource_access."info-bi-test".roles, ['DB9', 'DB11']][]
        }
      }
    }
    
  7. You want to add one item to the array resource_access."info-bi-test".roles form somewhere else in the JSON structure, with an input like
    {
      "resource_access":{
        "info-bi-test":{
          "roles":[
            "DB7"
          ]
        }
      },
      "default_role": "DB9"
    }
    
    but you want the whole object structure returned, with a desired output like
    {
      "resource_access":{
        "info-bi-test":{
          "roles":[
            "DB7",
            "DB9"
          ]
        }
      }
    }
    
    Then, you will have to recreate the object structure in the query:
    {
      resource_access: {
        "info-bi-test": {
          roles: [resource_access."info-bi-test".roles, default_role][]
        }
      }
    }
    
  8. You want to add multiple items to the array resource_access."info-bi-test".roles form somewhere else in the JSON structure, with an input like
    {
      "resource_access":{
        "info-bi-test":{
          "roles":[
            "DB7"
          ]
        }
      },
      "default_roles": [
        "DB9",
        "DB11"
      ]
    }
    
    but you want the whole object structure returned, with a desired output like
    {
      "resource_access":{
        "info-bi-test":{
          "roles":[
            "DB7",
            "DB9",
            "DB11"
          ]
        }
      }
    }
    
    Then, you will have to recreate the object structure in the query:
    {
      resource_access: {
        "info-bi-test": {
          roles: [resource_access."info-bi-test".roles, default_roles][]
        }
      }
    }
    

So, as you can see, they are all flavour of the same principle: nest everything in an array and flatten it.

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83