JsonPath as a JSON transformer

Viewed 62

I am trying out JsonPath as the JSON transformer by transforming input JSON to the output JSON structure example

Below input, JSON needs to be converted

{
    "BatchId": 1897173,
    "ApprovalCode": "12528809",
    "ResponseCode": 0,
    "ResponseMessage": "PENDING"
}

to

{
  "Batch": {
    "id": 1897173
  },
  "ApprovalCode": "12528809",
  "Response": {
    "ResponseCode": 0,
    "ResponseMessage": "PENDING"
  }
}

I am facing issues when putting the nested key when it is not available like below

DocumentContext dc = JsonPath.using(configuration).parse(json);
dc.put("$.Batch", "id", dc.read("$.BatchId")).jsonString()

When performing this action, the below error is thrown

14:17:49.939 [main] DEBUG com.jayway.jsonpath.internal.path.CompiledPath - Evaluating path: $['BatchId']
14:17:49.944 [main] DEBUG com.jayway.jsonpath.internal.path.CompiledPath - Evaluating path: $['BatchId']
14:17:49.944 [main] DEBUG com.jayway.jsonpath.internal.path.CompiledPath - Evaluating path: $['Batch']
Exception in thread "main" com.jayway.jsonpath.PathNotFoundException 

The goal is to create a new JSON with a new structure and values from the input JSON.

0 Answers
Related