Home > front end >  How to append constant array to another array using Jolt spec transformation
How to append constant array to another array using Jolt spec transformation

Time:11-16

I have the following input JSON:

[
  {
    "id": "id1",
    "projectId": "ALL",
    "composable": "true",
    "schema": {
      "properties": {},
      "required": [ "name", "db_access" ]
    }
  },
  {
    "id": "id2",
    "projectId": "ALL",
    "composable": "true",
    "schema": {
      "properties": {},
      "required": [ "test", "remote" ]
    }
  }
]

Desired JSON:

[
  {
    "id": "id1",
    "projectId": "ALL",
    "composable": "true",
    "schema": {
      "properties": {},
      "required": [ "name", "db_access", "account", "region" ]
    }
  },
  {
    "id": "id2",
    "projectId": "ALL",
    "composable": "true",
    "schema": {
      "properties": {},
      "required": [ "test", "remote", "account", "region" ]
    }
  }
]

I wish to append the "account" and "region" Strings to the beginning or end (it doesn't matter) of the "required" array. How do I achieve that using Jolt spec transformation?

CodePudding user response:

You can use such a shift transformation spec

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "[&1].&", // attributes (or objects/arrays other than "schema"
        "schema": {
          "*": "[&2].&1.&", // attributes (or objects/arrays other than "required" 
          "required": {
            "#region|#account": "[&3].&2.&1", // [&3] : represents going three levels up the tree to get values of the indexes in array-wise manner, &2 : replicates "schema", &1 : one level up to grab "required"
            "*": "[&3].&2.&1"
          }
        }
      }
    }
  }
]

the demo on the site enter image description here

  • Related