0

I have a json file that is not in the format like I expect it to be. When I convert this json file into a csv file using CDL, it is not in a proper format as I am converting the JSONObject to JSONArray.

I want to change the format of the JSON file so that I don't have to change the JSONObject to JSONArray.

Existing File format -

{"CampaignID":1,"Name":"Halloween Deals","Status":"Sent"}
{"CampaignID":2,"Name":"New Year Deals Campaign 2","Status":"Sent"}

How I want it to be

{"Campaigns":[{"CampaignID":1,"Name":"Halloween Deals","Status":"Sent"},
              {"CampaignID":2,"Name":"New Year Deals Campaign 2","Status":"Sent"}]}

I am extracting a json file CamapignList.json which has a list of campaigns. The file contains the REST API link for each campaign details. So, I am looping through the url for the number of campaigns in CampaignList.json file and writing the details in another file CamapignDetail.json which is not in the correct format.

JSONObject jObj = new JSONObject(result);
JSONArray items = jObj.getJSONArray("Items");
for (int i=0; i<items.length(); i++)
{
    JSONObject cObj = items.getJSONObject(i);
    String campaign_url = "rest.xyz.com/v1/Campaigns/…;
    System.out.println(camp_url);
    HttpGet reqst = new HttpGet(camp_url);
    HttpResponse res= client.execute(req);
    BufferedReader rd = new BufferedReader (new InputStreamReader(resp.getEntity().getContent()));
    PrintWriter wr = new PrintWriter(new FileWriter(camp_file, true));
    wr.println(rd.readLine());
}
vzamanillo
  • 9,905
  • 1
  • 36
  • 56
  • Possible duplicate http://stackoverflow.com/questions/11111737/how-to-properly-format-json-string-in-java – Bacteria Jun 05 '15 at 14:04
  • 1
    i don't think first example is valid JSON – Armand Jun 05 '15 at 14:06
  • Yes, the existing json file is in the wrong format. It is extracted from the REST API links given in another json file for each campaign. Is there any way to correct it ? I already checked the post you suggested but that does not help in my case. –  Jun 05 '15 at 14:09
  • What's the specific issue? It looks like you just need to wrap it in an array and stick it onto a `Campaigns` key in a new object. – Dave Newton Jun 05 '15 at 14:13
  • I am extracting a json file CamapignList.json which has a list of campaigns. The file contains the REST API link for each campaign details. So, I am looping through the url for the number of campaigns in CampaignList.json file and writing the details in another file CamapignDetail.json which is not in the correct format. –  Jun 05 '15 at 14:20
  • JSONObject jObj = new JSONObject(result); JSONArray items = jObj.getJSONArray("Items"); for (int i=0;i –  Jun 05 '15 at 14:28
  • Please, do not post code in the comments, it is very hard to read it. – vzamanillo Jun 05 '15 at 15:04

1 Answers1

0

I used a tool in this web site: I upload the JSON file then I click the tool button then it corrects the file and I download it again:

https://jsoneditoronline.org/