0

We are trying to transform a JsonPath to a different jsonpath as below:

Request:
{
 "name" : "Required",
 "address[0]" :  "Required",
 "address[0].city" : "Optional",
 "address[0].country" : "Required"
 "address[1]" :  "Required",
 "address[1].city" : "Required",
 "address[1].country" : "Required",
}

Expected Transformed Response
{
"customerName" : "Required",
"homeAddress" : "Required",
"homeAddress.city" : "Optional",
"homeAddress.country" : "Required",
"officeAddress" : "Required",
"officeAddress.city" : "Required",
"officeAddress.country" : "Required",
}

As mentioned above, the intention is to map address[0] -> homeAddress, and also from an array parent(address[0]) to an object parent(homeAddress), and address[1] to officeAddress.

Is there any way to do this transformation from a jsonpath to another jsonpath based on some conditions?

We have tried JOLT already.

wallisers
  • 388
  • 3
  • 16
Arun
  • 1
  • 1
  • Your question is about jsonpath, but there is not a single jsonpath expression in your question. What is the jsonpath expression you require help with? – tom redfern Nov 11 '19 at 13:19
  • So here the jsonpath will be for example $.address[0].city which needs to be tranformed to Jsonpath $.homeaddress.city. These elements represents the jsonpaths of another json and indicates whether the elements or objects in that path is required or optional – Arun Nov 11 '19 at 16:02
  • OK, so why do you need to do this? What is your use case? It is not clear to me what you are trying to achieve. – tom redfern Nov 11 '19 at 16:25
  • The intention is to use the rules in the first format(Request) and apply it to a JSON which is based on the second format(expected response). for example, consider the rule as $.address[1].city : "Required" which when applied to a JSON like below { "homeAddress" : { "city" : "CITY"}} should indicate that $homeaddress.city is mandatory. To do that validation, i need to transform the JSONPath first and then evaluate the JSON. – Arun Nov 12 '19 at 04:10
  • So what tool do you want to use to do the transformation? Just looking around there are dozens. JOLT is an option - is that the option you are hoping to use? – Jerry Jeremiah Sep 06 '22 at 04:43

1 Answers1

0

Using the JOLT tester at http://jolt-demo.appspot.com/

If you paste this request into the "JSON Input" field:

{
 "name" : "Required",
 "address[0]" :  "Required",
 "address[0].city" : "Optional",
 "address[0].country" : "Required",
 "address[1]" :  "Required",
 "address[1].city" : "Required",
 "address[1].country" : "Required"
}

And you paste this into the "JSON Spec" field:

[
  {
    "operation": "shift",
    "spec": {
      "name": "customerName",
      "address\\[0\\]": "homeAddress",
      "address\\[0\\]\\.city": "homeAddress\\.city",
      "address\\[0\\]\\.country": "homeAddress\\.country",
      "address\\[1\\]": "officeAddress",
      "address\\[1\\]\\.city": "officeAddress\\.city",
      "address\\[1\\]\\.country": "officeAddress\\.country"
    }
  }
]

Then the transform produces:

{
  "customerName" : "Required",
  "homeAddress" : "Required",
  "homeAddress.city" : "Optional",
  "homeAddress.country" : "Required",
  "officeAddress" : "Required",
  "officeAddress.city" : "Required",
  "officeAddress.country" : "Required"
}

Note: The keys of the spec are JSONPath expressions so anything that is treated specially by JSONPath needs to be escaped with backslashes: [, ], .

Jerry Jeremiah
  • 9,045
  • 2
  • 23
  • 32