0

I'm converting the JSON file into Map Object, and below is the output

[
    INV: [[ 
            TestCaseID:MKP, 
            TestData: [
                [
                    Controltype:text, 
                    Label:Title, 
                    Inputvalues:solid state device
                ],
                [
                    Controltype:search, 
                    Label:Creater, 
                    Inputvalues:Sabra-Anne Truesdale
                ]
            ]
    ]]
]

code

Map jsonMap = new LinkedHashMap()
jsonMap = converJsonToMapObject(fileName)
println jsonMap

From this, how can I retrieve the inner map [TestData]? I'm searching the Test case id if that matches on the map then I need to retrieve the test data.

m.antkowicz
  • 13,268
  • 18
  • 37
Prabu
  • 3,550
  • 9
  • 44
  • 85
  • 1
    Just an aside: the `new LinkedHashMap()` in your first line is totally unnecessary because it's lost when you then reassign the variable on the second line. – k314159 Sep 10 '20 at 08:16

1 Answers1

0

You should be able to retrieve a value from the map using the get method with the key - https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#get-java.lang.Object-

So:

public static Map findTestCase(List<Map> l, String testCaseID) {
    for (Map i : l) {
        if (testCaseID.equals(i.get("TestCaseID"))) {
            return i;
        }
    }
    return null;
}

List testCases = jsonMap.get("INV");
Map testCase = findTestCase(testCases, "MKP");
if (testCase != null) {
    testCase.get("TestData");
}

This is assuming that the parsing code that you're calling interprets JSON arrays as List objects.

Imagining that the JSON actually looks like this:

{
  "INV": [
    {
      "TestCaseID": "MKP",
      "TestData": [
        {
          "Controltype": "text",
          "Label": "Title",
          "Inputvalues": "solid state device"
        },
        {
          "Controltype": "search",
          "Label": "Creater",
          "Inputvalues": "Sabra-Anne Truesdale"
        }
      ]
    }
  ]
}
Tom
  • 43,583
  • 4
  • 41
  • 61
  • 1
    This won't work because the map only has "INV" as the one and only key. And it seems the value is a list of maps, and somewhere in that list is the map that the OP wants to get. – k314159 Sep 10 '20 at 08:18
  • This answer is still poor quality since it does not meet the logic presented by OP `I'm searching the Test case id if that matches on the map then I need to retrieve the test data` and just return always **first** test case no matter what ID it does have – m.antkowicz Sep 10 '20 at 08:34