weknowthe
weknowthe

Reputation: 21

Json String to map convertor,

I am trying to write a generic code for nested JsonObject to map conversion.

I have a sample JSONObject as

{
  "glossary": {
    "title": "example glossary",
    "GlossDiv": {
      "title": "S",
      "GlossList": {
        "GlossEntry": {
          "ID": "SGML",
          "SortAs": "SGML",
          "GlossTerm": "Standard Generalized \n Markup Language",
          "GlossDef": {
            "para": "A  DocBook.",
            "GlossSeeAlso": [
              "GML",
              "XML"
            ]
          },
          "GlossSee": "markup"
        }
      }
    }
  }
}

I want to convert it into map having key value as

glossary.title = "example glossary",
glossary.GlossDiv.title = "S",
glossary.GlossDiv.GlossList.GlossEntry.ID ="SGML",
glossary.GlossDiv.GlossList.GlossEntry.SortAs ="SGML",
glossary.GlossDiv.GlossList.GlossEntry.GlossTerm="Standard Generalized 
Markup Language",
glosary.GlossDiv.GlossList.GlossEntry.GlossDef.para ="A  DocBook.",
glossary.GlossDiv.GlossList.GlossEntry.GlossDef.GlossSeeAlso_0 = "GML",
glossary.GlossDiv.GlossList.GlossEntry.GlossDef.GlossSeeAlso_1 = "XML",
glossary.GlossDiv.GlossList.GlossEntry.GlossDef.GlossSee = "markup"

Upvotes: 0

Views: 1360

Answers (3)

weknowthe
weknowthe

Reputation: 21

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class JsonToMapConvertor {

    private static HashMap<String, Object> mapReturn = new HashMap<String, Object>();
    public static JsonParser parser = new JsonParser();

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

    String json ="add your Json";

       HashMap<String, Object> map = createHashMapFromJsonString(json,"");        
        for (Map.Entry<String, Object> entry : map.entrySet()) {            
          if(!entry.getValue().toString().contains("{"))  
                System.out.println(entry.getKey()+" : "+entry.getValue());
        }        

   }  

public static HashMap<String, Object> createHashMapFromJsonString(String json,String prefix) {

    JsonObject object = (JsonObject) parser.parse(json);   
    Set<Map.Entry<String, JsonElement>> set = object.entrySet();
    Iterator<Map.Entry<String, JsonElement>> iterator = set.iterator();
    while (iterator.hasNext()) {

        Map.Entry<String, JsonElement> entry = iterator.next(); 
        String key = entry.getKey();

        if(prefix.length()!=0){
            key = prefix + "."+key;
        }

        JsonElement value = entry.getValue();
        if (null != value) {            
            if (!value.isJsonPrimitive()) {
                if (value.isJsonObject()) {
                    mapReturn.put(key,value);
                    mapReturn.put(key, createHashMapFromJsonString(value.toString(),key));
                } else if (value.isJsonArray() && value.toString().contains(":")) {

                    List<HashMap<String, Object>> list = new ArrayList<>();
                    JsonArray array = value.getAsJsonArray();
                    if (null != array) {
                        for (JsonElement element : array) {
                            list.add(createHashMapFromJsonString(value.toString(),key));
                        }                 
                        mapReturn.put(key, list);
                    }
                } else if (value.isJsonArray() && !value.toString().contains(":")) {                    
                    mapReturn.put(key, value.getAsJsonArray());
                }              
            } else {
                mapReturn.put(key, value.getAsString());
            }
        }
    }
    return mapReturn;
    }
}

Upvotes: 0

Oleg Cherednik
Oleg Cherednik

Reputation: 18245

This is method to read Map from json string using Jackson:

public final class JsonUtils {
    public static <T> Map<String, T> readMap(String json) throws Exception {
        if (json == null)
            return null;

        ObjectReader reader = new ObjectMapper().readerFor(Map.class);
        MappingIterator<Map<String, T>> it = reader.readValues(json);

        if (it.hasNextValue()) {
            Map<String, T> res = it.next();
            return res.isEmpty() ? Collections.emptyMap() : res;
        }

        return Collections.emptyMap();
    }
}

This is how to read Map from json using given utilit method:

Map<String, String> map = flatMap(new LinkedHashMap<>(), "", JsonUtils.readMap(json));

And finally, this is how to transfrom Map into required Map (probably this could be done within Jackson engine, with provided custom deserializers or so, but I do not know exactly how, and thats why it is easier to me to implement it manually):

public static Map<String, String> flatMap(Map<String, String> res, String prefix, Map<String, Object> map) {
    for (Map.Entry<String, Object> entry : map.entrySet()) {
        String key = prefix + entry.getKey();
        Object value = entry.getValue();

        if (value instanceof Map)
            flatMap(res, key + '.', (Map<String, Object>)value);
        else
            res.put(key, String.valueOf(value));
    }

    return res;
}

Upvotes: 2

Bhargava Nandibhatla
Bhargava Nandibhatla

Reputation: 254

Jackson JSON is a pretty cool library which does this for you. I wrote a quick example below but you should be able to apply this to your JSONObject.

Let's say you have A.class with property B.class who in turn has a nested property C.class

@JsonPropertyOrder({ "b" })
class A {
    @JsonProperty("b")
    public B b;

    @JsonProperty("b")
    public B getB() {
        return b;
    }

    @JsonProperty("b")
    public void setB(B b) {
        this.b = b;
    }
}

@JsonPropertyOrder({ "c" })
class B {
    @JsonProperty("c")
    public C c;

    @JsonProperty("c")
    public C getC() {
        return c;
    }

    @JsonProperty("c")
    public void setC(C c) {
        this.c = c;
    }
}

@JsonPropertyOrder({ "d" })
class C {
    @JsonProperty("d")
    public String d;

    @JsonProperty("d")
    public String getD() {
       return d;
    }

    @JsonProperty("d")
    public void setD(String d) {
        this.d = d;
    }
}

You can convert a nested JSONObject {"b":{"c":{"d":"test"}}} into A.class like this:

C c = new C();
c.setD("test");

B b = new B();
b.setC(c);

JSONObject obj = new JSONObject();
obj.put("b", b);
String jsonAsString = new Gson().toJson(obj);

A a = mapper.readValue(jsonAsString, A.class);

Similarly you should be able to convert your JSONObject into any type you want. Hope this helps

Upvotes: 1

Related Questions