0

I have a JSON like this,

{
  "entry": [
    {
      "resource": {
        "id": "car-1",
        "type": "vehicle",
        "color": "red",
        "owner": {
          "ref": "Person/person-1"
        }
      }
    },
    {
      "resource": {
        "id": "car-2",
        "type": "vehicle",
        "color": "blue",
        "owner": {
          "ref": "Person/person-2"
        }
      }
    },
    {
      "resource": {
        "id": "person-1",
        "type": "Person",
        "name": "john"
      }
    },
    {
      "resource": {
        "id": "person-2",
        "type": "Person",
        "name": "wick"
      }
    }
  ]
}

and want to transform it to something like this.

[
  {
    "id": "car-1",
    "type": "vehicle",
    "color": "red",
    "ownername": "john"
  },
  {
    "id": "car-2",
    "type": "vehicle",
    "color": "blue",
    "ownername": "wick"
  }
]

The < resource/owner/ref > -> < type/id > : For each car the owner name to be added into the JSON.

I tried various combinations but unable to do it.

Any help would be greatly appreciated.

bloodyKnuckles
  • 11,551
  • 3
  • 29
  • 37
Prakash P
  • 100
  • 1
  • 7

3 Answers3

0

It isn't meant do do that. It is meant for structural format changes of Json, not data dependent ones.

I.e. I get Json out of a backend system, it is always in format "A", and I need to always return it in format "B", regardless of what the data actually is.

Milo S
  • 4,466
  • 1
  • 19
  • 22
0

You can combine under common nodes by using @(1,id). and @(1,owner.ref). identifiers, after getting rid of the pieces upto / for the value of owner.ref such as

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "*": {
          "*": {
            "ow*": {
              "ref_": "=split('/', @(1,ref))",
              "ref": "=lastElement(@(1,ref_))"
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "*": {
            "@": "@(1,id).&",
            "*": "@(1,owner.ref).&"
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "pers*": {
        "@id": "[#2].id",
        "@type": "[#2].type",
        "@color": "[#2].color",
        "@resource.name": "[#2].name"
      }
    }
  }
]

the demo on the site http://jolt-demo.appspot.com/ is :

enter image description here

Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55
0

You may consider another library Josson and use function group() to complete the task.

https://github.com/octomix/josson

entry.resource
.group(if([type='vehicle'], owner.ref.removeStart('Person/'), id),
       if([type='vehicle'], field(owner:), map(ownername:name)))@
.elements.mergeObjects()

Deserialization

Josson josson = Josson.fromJsonString(
    "{" +
    "  \"entry\": [" +
    "    {" +
    "      \"resource\": {" +
    "        \"id\": \"car-1\"," +
    "        \"type\": \"vehicle\"," +
    "        \"color\": \"red\"," +
    "        \"owner\": {" +
    "          \"ref\": \"Person/person-1\"" +
    "        }" +
    "      }" +
    "    }," +
    "    {" +
    "      \"resource\": {" +
    "        \"id\": \"car-2\"," +
    "        \"type\": \"vehicle\"," +
    "        \"color\": \"blue\"," +
    "        \"owner\": {" +
    "          \"ref\": \"Person/person-2\"" +
    "        }" +
    "      }" +
    "    }," +
    "    {" +
    "      \"resource\": {" +
    "        \"id\": \"person-1\"," +
    "        \"type\": \"Person\"," +
    "        \"name\": \"john\"" +
    "      }" +
    "    }," +
    "    {" +
    "      \"resource\": {" +
    "        \"id\": \"person-2\"," +
    "        \"type\": \"Person\"," +
    "        \"name\": \"wick\"" +
    "      }" +
    "    }" +
    "  ]" +
    "}");

Transformation

JsonNode node = josson.getNode(
    "entry.resource" +
    ".group(if([type='vehicle'], owner.ref.removeStart('Person/'), id)," +
    "       if([type='vehicle'], field(owner:), map(ownername:name)))@" +
    ".elements.mergeObjects()");
System.out.println(node.toPrettyString());

Output

[ {
  "id" : "car-1",
  "type" : "vehicle",
  "color" : "red",
  "ownername" : "john"
}, {
  "id" : "car-2",
  "type" : "vehicle",
  "color" : "blue",
  "ownername" : "wick"
} ]
Raymond Choi
  • 1,065
  • 2
  • 7
  • 8