Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to change json value containing hyphens in Java?

Here is my json file

    {
  "file-list":[
    "ab3bdc2c3d-f2fg63-42c3-abasd53-e78c66afc77d"
  ],
  "metadata":{
    "X-Object-Meta-Favourite":"true"
  }
}

So i want to change "X-Object-Meta-Favourite":"true" ‘s value to false.

First i read

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

public static JSONObject favourite() throws IOException {

    String body = new String(Files.readAllBytes(Paths.get("src/test/resources/config/environments/FileSystem/Favourite.json")));
        return new JSONObject(body);
    
    }

And it returns JSONObject. So i use this body int his method:

public void fovurite(String action) throws IOException {

    action = "false";

    JSONObject body = readFiles.favourite();
    body.put(body.getJSONObject("metadata").getString("X-Object-Meta-Favourite"),action);

But added true = "false" value. I couldnt change X-Object-Meta-Favourite ‘ value.

I also tried body.put("metadata['X-Object-Meta-Favourite']",action) but this time added metadata['X-Object-Meta-Favourite'] = "false"

In short how can i change this value ? Thank you for your advice

>Solution :

You are reading the value of the X-Object-Meta-Favourite and putting action in a field of said value, so basically you are putting "false" inside a field named "true", what you want to do is to get the nested object, modify it and put it in a field named metadata.

so it wold be something like:

JSONObject body = readFiles.favourite();
JSONObject nested = body.getJSONObject("metadata");
nested.put("X-Object-Meta-Favourite", action);
body.put("metadata", nested);

I have not tested the code but I hope you understand what I mean.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading