0

I have a json string and my requirement is to covert into map, where key will be field of the json. below is my json

{  
   "A":[  
      {  
         "B":[  
            {  
               "C":[  
                  {  
                     "D1":"V1",
                     "D2":"X1",
                     "D3":Y1,
                     "D4":"Z1"
                  },
                  {  
                     "D1":"V2",
                     "D2":"X2",
                     "D3":Y2,
                     "D4":"Z2"
                  }
               ]
            }
         ]
      }
   ]
}

Key should look like "A->B->C->D1" and corresponding value V1,V2. Map signature should look like Map<String,List<String>>. Similar kind of question posted here but my problem is to create key out of json field.Let me know if more information is required. Thanks in advance.

rocky
  • 737
  • 4
  • 11
  • 19
  • I think you have to build your own method to do this. – Incepter Apr 13 '18 at 15:21
  • Here is a simple [link](https://www.mkyong.com/java/jackson-2-convert-java-object-to-from-json/) to map json value to java objects – Incepter Apr 13 '18 at 15:22
  • Thanks @MohamedELAYADI I have basic knowledge but my problem here is to create key.I want to know if there is some library by using this if we can create key from json field.I looking for simplified solution if possible. – rocky Apr 13 '18 at 15:53
  • first, is your structure fix or you are looking for something generic ? do you even know the level to stop on ? – Incepter Apr 13 '18 at 15:58
  • structure is fix – rocky Apr 13 '18 at 16:00
  • well i did somthing that answers your question for the EXACT structure you provided, and to reproduce it was painfull hh – Incepter Apr 13 '18 at 16:29
  • Jackson should be able to marshal this structure. Just make an object structure that matches the JSON. – DwB Apr 13 '18 at 17:11

2 Answers2

0

I did something that answers the exact structure of yours where i changed the value of D3 to be also string:

class Wraper that is the whole object

    public class Wraper {
    public Wraper() {}
    @JsonProperty("A") A[] a;
}

class A

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

Class B

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

Class C

public class C {
    @JsonProperty("D1") String d1;
    @JsonProperty("D2") String d2;
    @JsonProperty("D3") String d3;
    @JsonProperty("D4") String d4;
}

And finally where I tested:

static final String JSON_VAL="{\"A\":[{\"B\":[{\"C\":[{\"D1\":\"V1\",\"D2\":\"X1\",\"D3\":\"Y1\",\"D4\":\"Z1\"},{\"D1\":\"V2\",\"D2\":\"X2\",\"D3\":\"Y2\",\"D4\":\"Z2\"}]}]}]}";


final ObjectMapper mapper = new ObjectMapper();
final Wraper wraper = mapper.readValue(JSON_VAL, Wraper.class);

final Map<String,List<String>> map = new HashMap<>();

Arrays.stream(wraper.a).forEach(a -> {
    Arrays.stream(a.b).forEach(b -> {
        final List<String> d1 = new ArrayList<>();
        final List<String> d2 = new ArrayList<>();
        final List<String> d3 = new ArrayList<>();
        final List<String> d4 = new ArrayList<>();
        Arrays.stream(b.c).forEach(c -> {
            d1.add(c.d1);
            d2.add(c.d2);
            d3.add(c.d3);
            d4.add(c.d4);
        });
        map.put("A->B->C->D1", d1);
        map.put("A->B->C->D2", d2);
        map.put("A->B->C->D3", d3);
        map.put("A->B->C->D4", d4);
    });
});
Incepter
  • 2,711
  • 15
  • 33
0

Try Jackson using the correct Java class definitions (based on the JSON).

Here is some code:

public class topElement
{
    private ElementA[] A;

    public ElementA[] getA()
    {
        return A;
    }

    public void setA(
        final ElementA[] newValue)
    {
        A = newValue;
    }
}

public class ElementA
{
    private ElementB[] B;

    public ElementB[] getB()
    {
        return B;
    }

    public void setB(
        final ElementB[] newValue)
    {
        B = newValue;
    }
}

public class ElementB
{
    private ElementC[] C;

    public ElementC[] getC()
    {
        return C;
    }

    public void setC(
        final ElementC[] newValue)
    {
        C = newValue;
    }
}

public class ElementC
{
    private Map blammyMap;

    public Map getBlammyMap()
    {
        return blammyMap;
    }

    public void setBlammyMap(
        final Map newValue)
    {
        blammyMap = newValue;
    }
}
}
DwB
  • 37,124
  • 11
  • 56
  • 82