1

I am reading a JSON object from file.

I am able to read the value but how can I update the code value for my payload

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

Import I am using:-

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.util.Iterator;
import java.io.FileWriter;
import javax.json.JsonValue;
import org.json.simple.JSONArray;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

My Code:-

// read the json file
            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);

But this value is not changing my require data

3 Answers 3

3

try this

   JSONObject jsonObject1 = (JSONObject) jsonObject.get("products");
   JSONObject jsonObject2 = (JSONObject)jsonObject1.get("productsApp15");
   String firstName = (String) jsonObject2.get("code").toString();
Sign up to request clarification or add additional context in comments.

3 Comments

cool .. it's working but how can I write/update on the same location? .. means I want to update the this value for my payload
I got it and I puts it the whole solution as a answer here .. Thanks :) .. vote up
' public void replaceJson() throws JSONException { String json = "{\"products\": {\"productsApp15\": {\"status\": \"active\",\"attribute_set\": \"Apparel\", \"name\": \"productsApp16\", \"product_type\": \"product\", \"code\": \"productsApp16\" } }}"; JSONObject jsonObject = new JSONObject(json); JSONObject jsonObject1 = (JSONObject) jsonObject.get("products"); JSONObject jsonObject2 = (JSONObject) jsonObject1.get("productsApp15"); jsonObject2.put("code", "try"); System.out.println(jsonObject.toString()); }'
2

Below code is working for me:-

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: " + firstName2);

Thanks to Rama Krishan

2 Comments

if it is working then accept it as answer @Shubham Jain
Yes I will .. I can't answer my own answer before 2 days .. Thanks Rama for your help
1

public void replaceJson() throws JSONException { String json = "{\"products\": {\"productsApp15\": {\"status\": \"active\",\"attribute_set\": \"Apparel\", \"name\": \"productsApp16\", \"product_type\": \"product\", \"code\": \"productsApp16\" } }}";

   JSONObject jsonObject = new JSONObject(json);
   JSONObject jsonObject1 = (JSONObject) jsonObject.get("products");
   JSONObject jsonObject2 = (JSONObject)    jsonObject1.get("productsApp15");
         jsonObject2.put("code", "try");
          System.out.println(jsonObject.toString()); 
}'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.