0

POJO class for persistence:

class Target{
private Integer status;
private Integer autoLink;
// other fields

}

Input Json format :

Target
{
"status":"Executed",
"autoLinkIndicator":"YES"
//other fields
}

How do parse through the JSON and change the values of the fields before persisting?

Eg:

if I see "status":"Executed",

I want to change it to "status":1 and "status":"Terminated" -> "status":0

Similarily, for "autoLinkIndicator"

1 for "YES", 0 for "NO"

Currently, Using gson for parsing json into Target class.

Ram_apex
  • 1
  • 1

1 Answers1

0

I hope the below helps - simple parser with out a class and you can edit the value and pass to persistence layer

import java.io.FileReader; 
import java.util.Iterator; 
import java.util.Map; 
import org.json.simple.JSONArray; 
import org.json.simple.JSONObject; 
import org.json.simple.parser.*; 

public class JSONReadExample  
{ 
    public static void main(String[] args) throws Exception  
    { 

        Object obj = new JSONParser().parse(new FileReader(<<<<Ur JSON>>>>));
        // typecasting obj to JSONObject 
        JSONObject jsonObj = (JSONObject) obj; 
        // getting status and autoLinkIndicator
        String status = (String) jo.get("status"); 
        String autoLinkIndicator  = (String) jo.get("autoLinkIndicator");        
        System.out.println(status);; 
        System.out.println(autoLinkIndicator); 

    //Code logic to alter the values and then persist
    }
}
David Buck
  • 3,752
  • 35
  • 31
  • 35
Srikanth Balaji
  • 2,638
  • 4
  • 18
  • 31