DEV Community

Pravin kumar
Pravin kumar

Posted on

Need to transform Json data

I am writing a project where I need to transform Json from one format to another. I am new to json. Any lead will be appreciated.
Input Data:

{
  "sorts": [
    {
      "name": "firstName",
      "sortOrder": "Ascending"
    }
  ],
  "page": "1",
  "pageSize": "10",
  "filters": [
    {
      "name": "studentIds",
      "type": "InList",
      "value": "(S001,S002,S004)"
    },
    {
      "name": "assignmentStatus",
      "type": "InList",
      "value": "(Completed, In Progress)"
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode

Target Data:

{
    "orderBy": "firstName",
    "sortOrder": "Ascending",
    "page": "1",
    "pageSize": "10",
    "studentIds": [
        "S001",
        "S002",
        "S003"
    ],
    "assignmentStatus": [
        "Completed",
        "In Progress"
    ]
}
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
raymondchoigit profile image
Raymond Choi

If you are programming Java, my open source project can do the job.

github.com/octomix/josson

Josson josson = Josson.fromJsonString(
        "{" +
        "  \"sorts\": [" +
        "    {" +
        "      \"name\": \"firstName\"," +
        "      \"sortOrder\": \"Ascending\"" +
        "    }" +
        "  ]," +
        "  \"page\": \"1\"," +
        "  \"pageSize\": \"10\"," +
        "  \"filters\": [" +
        "    {" +
        "      \"name\": \"studentIds\"," +
        "      \"type\": \"InList\"," +
        "      \"value\": \"(S001,S002,S004)\"" +
        "    }," +
        "    {" +
        "      \"name\": \"assignmentStatus\"," +
        "      \"type\": \"InList\"," +
        "      \"value\": \"(Completed, In Progress)\"" +
        "    }" +
        "  ]" +
        "}");

System.out.println(
        josson.getNode(
            "map(" +
            " orderBy:sorts[0].name," +
            " sortOrder:sorts[0].sortOrder," +
            " page," +
            " pageSize," +
            " studentIds:filters[name='studentIds'].value.substr(1,-1).split(',')," +
            " assignmentStatus:filters[name='assignmentStatus'].value.substr(1,-1).split(',').trim()" +
            ")")
        .toPrettyString());
Enter fullscreen mode Exit fullscreen mode

Output:

{
  "orderBy" : "firstName",
  "sortOrder" : "Ascending",
  "page" : "1",
  "pageSize" : "10",
  "studentIds" : [ "S001", "S002", "S004" ],
  "assignmentStatus" : [ "Completed", "In Progress" ]
}
Enter fullscreen mode Exit fullscreen mode