简体   繁体   中英

How to override the 2nd Object JSON with 1st JSON object

I have a situation where I need to override 2nd JSON object value to 1st.

JSON original :-

{
    "products": {
        "productsApp15": {
            "status": "active",
            "attribute_set": "Apparel",
            "name": "productsApp16",
            "product_type": "product",
            "code": "productsApp16"
        }
    }
}

My 1st object :-

{
    "productsApp15": {
        "attribute_set": "Apparel",
        "status": "active",
        "name": "productsApp16",
        "product_type": "product",
        "code": "productsApp16"
    }
}

My 2nd object :-

{
    "attribute_set": "Apparel",
    "status": "active",
    "name": "productsApp16",
    "product_type": "product",
    "code": "try"
}

If you see the value of key -> code is updated here. I want this change in my real or 1st JSON object so I can pass it to my Payload

My Code:-

    FileReader reader = new FileReader(filePath);

    JSONParser jsonParser = new JSONParser();
    JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);

    JSONObject jsonObject1 = (JSONObject) jsonObject.get("products");
    JSONObject jsonObject2 = (JSONObject)jsonObject1.get("productsApp15");
    String firstName = (String) jsonObject2.get("code").toString();

    System.out.println("The first name is: " + firstName);

    jsonObject2.remove("code");
    jsonObject2.put("code", "try");

    JSONObject jsonObject3 = (JSONObject)jsonObject1.get("productsApp15");
    String firstName2 = (String) jsonObject2.get("code").toString();
    System.out.println("The first name is: " + jsonObject3);

    JSONObject combined = new JSONObject();
    combined.put("Object1", jsonObject1);
    combined.put("Object2", jsonObject3);
    String firstName3 = (String) jsonObject2.get("code").toString();
    System.out.println("The first name is: " + combined);

My Main objective :- I am reading a file which contain my JSON, As you can see my object is again inside an another object .

I want to update the value and then want to pass it to payload.

But how to get the original JSON structure with updated value?

Is it possible?

By Constructing the Java classes against the structure that is required in JSON, this can be achieved.

Messing up with JSON with JSON readers and parser is risky.

public class Products{
    private List<Product> productsList;
}

public class Product{
    private Map<String, ProProps> map = new HashMap<>();
    class ProProps{
       private String code;
       private String name;
       ...
    }
}

This gives you to map the objects and values.

Get the value of product that you want by key and replace the required props that you want.

Though the conversion of Java - Json takes place, there can be many extensions that can happen when you develop.

You can use this example to convert and do the manipulations required.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM