I wrote a fairly simple scraper in Python that scrapes a JSON string from a server, and outputs it as a table/saves it as a CSV. I am now trying to port my script over to Java but am finding the learning curve a lot steeper.
I have gotten as far as extract my string, but am having trouble recreating the part of my original python script that flattens one of the nested parts of the JSON string.
The element in my JSON string that is nested looks like this:
"workItemProperties":[{"Key":"System.Id","Value":12345},{"Key":"System.Title","Value":"Title text here"},{"Key":"System.IterationPath","Value":"Some\Path\Here"},{"Key":"System.ChangedDate","Value":"\/Date(1555424893503)\/"},{"Key":"System.ChangedBy","Value":"Fred Smith <Fred\Smith>"},{"Key":"Microsoft.VSTS.TCM.AutomationStatus","Value":"Not Automated"}]
In Python the following code would flatten this:
for i in JsonResults['testPoints']:
for item in i['workItemProperties']:
key = item['Key']
i[key] = item['Value']
del i['workItemProperties']
So that instead of my csv having a single column who's contents is just the nested workItemProperties string, instead System.Id, System.Title, System.IterationPath etc would all be in their own columns.
I am fairly new to coding and struggling to recreate the above in Java, I assume I need loop through the JSONObjects within my JSONArray, but can't seem to get this to work.
Here is the main portion of my code (not including my many failed flattening/un-nesting attempts).
try {
document = Jsoup.connect("http://url").userAgent("Opera").proxy("1.2.3.4", 1234).header("Authorization", "Basic " + base64login).timeout(10 * 1000).validateTLSCertificates(false).get();
Elements jsCode = document.getElementsByClass("__allTestPointsOfSelectedSuite");
String JsonResults = jsCode.html();
output = new JSONObject(JsonResults);
docs = output.getJSONArray("testPoints");
File file=new File("JSONextract.csv");
String csv = CDL.toString(docs);
FileUtils.writeStringToFile(file, csv)
System.out.println(csv);
} catch (IOException e) {
e.printStackTrace();
}
I am currently using the org.json library to read the JSON string.