0

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?

1 Answer 1

1

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 http://jolt-demo.appspot.com/ is

enter image description here

Sign up to request clarification or add additional context in comments.

5 Comments

Hey, thanks! That works as a charm. I updated my question a bit - the input/output json is now a list. Could you please update your answer to accommodate that?
I'm not sure this works as expected. I'm getting: { "id" : [ "id1", "id2" ], "projectId" : [ "ALL", "ALL" ], "composable" : [ "true", "true" ], "schema" : { "properties" : [ { }, { } ], "required" : [ "region", "account", "name", "db_access", "region", "account", "test", "remote" ] } }
It's important to see the exact picture for the Jolt. I've just edited based on the latest case @mdzh
This is better but there's still problem with the spec - the schema part is missing now. 'required' and 'properties' are directly under top level
excuse me I missed then @mdzh

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.