I'm new to parsing JSON files so I'm trying to figure out a way to do it. I have an object:
JsonObject summary = Summary.getSummaryAsJson();
The summary looks like:
{
"status": "failed",
"roots": [
{
"name": "Main1",
"status": "passed",
"tests": [
{
"name": "DummyGroup/DummyTest",
"mini_tests": [
{
"name": "dummyMiniTest2",
"status": "PASSED"
},
{
"name": "dummyMiniTest1",
"status": "PASSED"
}
]
},
{
"name": "DummyGroup/BasicTestInSub",
"mini_tests": [
{
"name": "dummyMiniTest2",
"status": "PASSED"
},
{
"name": "dummyMiniTest1",
"status": "PASSED"
}
]
},
{
"name": "DummyGroup2/BasicTest2",
"mini_tests": [
{
"name": "dummyMiniTest2",
"status": "PASSED"
},
{
"name": "dummyMiniTest1",
"status": "PASSED"
}
]
},
{
"name": "DummyGroup2/DummyTest2",
"mini_tests": [
{
"name": "dummyMiniTest2",
"status": "PASSED"
},
{
"name": "dummyMiniTest1",
"status": "PASSED"
}
]
}
]
},
{
"name": "Main3",
"status": "failed",
"tests": [
{
"name": "DummyGroup/DummyTest",
"mini_tests": [
{
"name": "dummyMiniTest2",
"status": "PASSED"
},
{
"name": "dummyMiniTest1",
"status": "PASSED"
}
]
},
{
"name": "DummyGroup/BasicTestInSub",
"mini_tests": [
{
"name": "dummyMiniTest2",
"status": "CHECK_FAILED"
},
{
"name": "dummyMiniTest1",
"status": "CHECK_FAILED"
}
]
},
{
"name": "DummyGroup2/BasicTest2",
"mini_tests": [
{
"name": "dummyMiniTest2",
"status": "CHECK_FAILED"
},
{
"name": "dummyMiniTest1",
"status": "CHECK_FAILED"
}
]
},
{
"name": "DummyGroup2/DummyTest2",
"mini_tests": [
{
"name": "dummyMiniTest2",
"status": "PASSED"
},
{
"name": "dummyMiniTest1",
"status": "PASSED"
}
]
}
]
}
]
}
I would like to extract all failed mini_tests
and insert them into a list. I have the names of the roots so I can loop through them:
Map<Root,List<String>> rootsPerFailed = new HashMap<Root, List<String>>();
for (Root root: roots) {
// iterate through the JSON and check if the current root is equal to root.getName().
// If so, get the failed sub-roots and insert all into rootsPerFailed.
// If no failed sub-roots, insert an empty list or null (not sure which is better).
}
Also, we don't have to iterate through all of the structure because if some root or test has status passed than we don't need to check if it is failed because for sure it didn't. For the object above, the map will look like:
Main1 => []
Main3 => [DummyGroup/BasicTestInSub/dummyMiniTest2,DummyGroup/BasicTestInSub/dummyMiniTest1,DummyGroup2/BasicTest2/dummyMiniTest2,DummyGroup2/BasicTest2/dummyMiniTest1]
I'm really sorry for asking such technical question but I'm not really sure how to do this in Java. In python, it feels easier to parse a JSON file but I'm not sure what to do here. Will be glad for any help.