0

I intend to create a JSON Array with the following structure. The metadata tag is going to constant in all the entries. I am stumped.

[{
        "metadata": {
            "Value": "String"
        },
        "name": "String",
        "id": "String"
    },
    {
        "metadata": {
            "Value": "String"
        },
        "name": "String",
        "id": "String"
    }
]
1
  • What exactly you want to do/ Commented Jun 14, 2017 at 5:17

2 Answers 2

1
public class yourJsonObject {

private Map<String, String> metadata;

private String name;

private string id;

public yourJsonObject() {

}

public Map<String, String> getMetadata(){
return metadata;
}

public void setMetadata(Map<String, String> metadata){
this.metadata = metadata;
}

public String getName(){
return name;
}

public void setName(String name) {
this.name = name;
}

public String getId(){
return id;
}

public void setId(String id){
this.id = id;
}

}

Then somewhere else you can just do this:

ObjectMapper mapper = new ObjectMapper(); // create once, reuse
yourJsonObject example = new yourJsonObject(); // have your POJO you want to save
mapper.writeValue(new File("result.json"), example);

To read you can just use:

ObjectMapper mapper = new ObjectMapper(); // create once, reuse
yourJsonObject value = mapper.readValue(new File("data.json"), yourJsonObject .class);

Both snippets are taken from my linked wiki article from jackson themselves.

Jackson should automatically be able to parse this POJO to an equivalent JSON if configured correctly. Note: Jackson has to be globally registered and has to know about it. Please read the wiki of what you use to know about it... Jackson in 5 Minutes

Else you could just manually build the JSON like Neeraj said.

Sign up to request clarification or add additional context in comments.

4 Comments

Sounds like a wonderful solution. I had this in mind. I was going to make a POJO and then serialize it into the requisite structure. I was stumped with the makings of that structure, though.
Well then, there you go. This is the best way of doing it in for my preference. There is also someway to integrate jackson so much that you don't have to use the ObjectMapper everywhere but I don't know how right now. But that is also only usefull for big software with a lot of POJO's and REST communication. Hope you achieve what you need now :)
However, I am still wondering how to insert a Key:Value pair inside the Value for metadata.
You can prefill a temp map and pass that to setMetadata or you can add a function to add one pair and call that as much as needed.
0
JSONArray array = new JSONArray(); // Create JSONArray Object

JSONObject jsonObject = new JSONObject(); // Your JSONObject which gets added into array
jsonObject.put("metadata",new MetaDataCustomClass("SomeRandomStringValue"));
jsonObject.put("name", "Neeraj");
jsonObject.put("id", "123");

array.add(jsonObject); // Here you push the jsonObject into Array.

Note: MetaDataCustomClass is just a custom Class having a Value instance variable of type String.

Class MetaDataCustomClass {

   private String value;

   public MetaDataCustomClass(String value){
       this.value = value;
   }

}

1 Comment

Are you sure the metadata structure will be maintained accordingly? Because metadata is to contain a Key:Value pair inside that.

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.