0

I'm very new working java with json. Couldn't find relevant solutions. That's why posting this question. I have json with property name as value in it. How to convert this property name as attribute and restructured in array. Any help would be much appreciated.

Following is my json file:

INPUT JSON:

{
    "payload": {
        "giata": {
            "435185": [
                "2019-11-27T14:34:32.368197Z"
            ]
        },
        "dod1": {
            "A0947863": [
                "2019-11-27T14:34:32.368197Z"
            ]
        },
        "gr": {
            "A0947863": [
                "2019-11-27T14:34:32.368197Z",
                "2021-04-27T06:13:12.841968Z",
                "2021-04-27T06:13:21.640612Z",
                "2021-04-27T06:13:28.439001Z",
                "2021-04-27T06:13:43.487103Z",
                "2021-04-27T06:13:49.770269Z"
            ]
        }
    }
}

Expected JSON :

{
    "payload": {
        "sources": [
            {
                "source": "giata",
                "code": "435185",
                "version": [
                    "2019-11-27T14:34:32.368197Z"
                ]
            },
            {
                "source": "dod1",
                "code": "A0947863",
                "version": [
                    "2019-11-27T14:34:32.368197Z"
                ]
            },
            {
                "source": "gr",
                "code": "A0947863",
                "version": [
                    "2019-11-27T14:34:32.368197Z",
                    "2021-04-27T06:13:12.841968Z",
                    "2021-04-27T06:13:21.640612Z",
                    "2021-04-27T06:13:28.439001Z",
                    "2021-04-27T06:13:43.487103Z",
                    "2021-04-27T06:13:49.770269Z"
                ]
            }
        ]
    }
}

Code:

package com.tui.structure_json;
import java.util.Map;
import java.util.Set;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class App 
{
    public static void main( String[] args )
    {
        String yourJson = "{\r\n" + 
                "   \"payload\": {\r\n" + 
                "       \"giata\": {\r\n" + 
                "           \"435185\": [\r\n" + 
                "               \"2019-11-27T14:34:32.368197Z\"\r\n" + 
                "           ]\r\n" + 
                "       },\r\n" + 
                "       \"dod1\": {\r\n" + 
                "           \"A0947863\": [\r\n" + 
                "               \"2019-11-27T14:34:32.368197Z\"\r\n" + 
                "           ]\r\n" + 
                "       },\r\n" + 
                "       \"gr\": {\r\n" + 
                "           \"A0947863\": [\r\n" + 
                "               \"2019-11-27T14:34:32.368197Z\",\r\n" + 
                "               \"2021-04-27T06:13:12.841968Z\",\r\n" + 
                "               \"2021-04-27T06:13:21.640612Z\",\r\n" + 
                "               \"2021-04-27T06:13:28.439001Z\",\r\n" + 
                "               \"2021-04-27T06:13:43.487103Z\",\r\n" + 
                "               \"2021-04-27T06:13:49.770269Z\"\r\n" + 
                "           ]\r\n" + 
                "       }\r\n" + 
                "   }\r\n" + 
                "}";
        JsonElement element = JsonParser.parseString(yourJson);
        JsonObject obj = element.getAsJsonObject(); //since you know it's a JsonObject
        Set<Map.Entry<String, JsonElement>> entries = obj.entrySet();//will return members of your object
        for (Map.Entry<String, JsonElement> entry: entries) {
            System.out.println(entry.getValue());
        }
    }
}
marjun
  • 696
  • 5
  • 17
  • 30
  • I would recommend parsing it, mapping it, then serialising it again. – Henry Twist May 03 '21 at 11:46
  • @HenryTwist Thanks for response. Dynamic parsing can be done through json path? Any pseudo code or references will be helpful? – marjun May 03 '21 at 12:19
  • 1
    I've actually just noticed that your expected JSON isn't actually valid JSON, the array isn't an array? So maybe update that to what you actually want and then I'll take another look. – Henry Twist May 03 '21 at 12:27
  • 1
    @HenryTwist I have updated the input and expected json. Please have a look. – marjun May 03 '21 at 12:40
  • 1
    I would take a look at this answer which includes steps that parse the JSON keys, which is what you need here: https://stackoverflow.com/a/31094365/10082297 – Henry Twist May 03 '21 at 14:09
  • mapping I feel some what challenging. – marjun May 03 '21 at 17:28

1 Answers1

0

You may try library Josson to do the transformation.

https://github.com/octomix/josson

Deserialization

Josson josson = Josson.fromJsonString(
    "{" +
    "    \"payload\": {" +
    "        \"giata\": {" +
    "            \"435185\": [" +
    "                \"2019-11-27T14:34:32.368197Z\"" +
    "            ]" +
    "        }," +
    "        \"dod1\": {" +
    "            \"A0947863\": [" +
    "                \"2019-11-27T14:34:32.368197Z\"" +
    "            ]" +
    "        }," +
    "        \"gr\": {" +
    "            \"A0947863\": [" +
    "                \"2019-11-27T14:34:32.368197Z\"," +
    "                \"2021-04-27T06:13:12.841968Z\"," +
    "                \"2021-04-27T06:13:21.640612Z\"," +
    "                \"2021-04-27T06:13:28.439001Z\"," +
    "                \"2021-04-27T06:13:43.487103Z\"," +
    "                \"2021-04-27T06:13:49.770269Z\"" +
    "            ]" +
    "        }" +
    "    }" +
    "}");

Transformation

JsonNode node = josson.getNode(
    "map(payload" +
    "  .map(sources: entries()" +
    "    .map(source: key," +
    "         code: value.keys().[0]," +
    "         version: value.toArray().[0]" +
    "    )" +
    "  )" +
    ")");
System.out.println(node.toPrettyString());

Output

{
  "payload" : {
    "sources" : [ {
      "source" : "giata",
      "code" : "435185",
      "version" : [ "2019-11-27T14:34:32.368197Z" ]
    }, {
      "source" : "dod1",
      "code" : "A0947863",
      "version" : [ "2019-11-27T14:34:32.368197Z" ]
    }, {
      "source" : "gr",
      "code" : "A0947863",
      "version" : [ "2019-11-27T14:34:32.368197Z", "2021-04-27T06:13:12.841968Z", "2021-04-27T06:13:21.640612Z", "2021-04-27T06:13:28.439001Z", "2021-04-27T06:13:43.487103Z", "2021-04-27T06:13:49.770269Z" ]
    } ]
  }
}
Raymond Choi
  • 1,065
  • 2
  • 7
  • 8