0

I have a file that contains JSON objects. I am trying to access the file using its URL in my java program to access its contents as follows.

URL url = new URL(Url);
URLConnection request = url.openConnection();
request.connect();
JsonReader jr = new JsonReader(new InputStreamReader((InputStream) request.getContent()));
jr.setLenient(true);
JsonParser jp = new JsonParser();
JsonElement root = jp.parse(jr);

I have to use the jr.setLenient(true); since the data is not a JSON object, but multiple JSON objects. But this approach does not help me to get all the JSON objects in the file. I am trying to get all the JSON objects, loop through each one of them and then save the data into my database.

I have the sample of the type of data I receive at the following URL: Sample File

Is there any way in which I can get all the objects in the file to perform the required operations.

Also, I can manually save the data into a JSON array in a new file manually if nothing works, but there is no comma separation between each JSON object which is making it very difficult for me to parse the data.

But each JSON object is a new line. So is there any way to parse it based on that.

Geo Thomas
  • 1,139
  • 3
  • 26
  • 59
  • Make that text a valid JSON string by enclosing it `[]`. `JsonReader jr = new JsonReader(new InputStreamReader((InputStream) "[" + request.getContent() + "]" ));` Then you get an array of objects and you can iterate over it. – marekful May 15 '18 at 12:48
  • Try this [link](https://stackoverflow.com/questions/15609306/convert-string-to-json-array) – Sharad Gautam May 15 '18 at 12:49
  • @ marekful, I get an error: The operator + is undefined for the argument type(s) InputStream, Object when I try your code. But this is something I am looking for, I need a way to achieve this – Geo Thomas May 15 '18 at 12:53

2 Answers2

1

what @marekful suggest was to fix the JSON string to get a valid one

public static void main(String[] args) throws Exception {
    URL url = new URL("http://d1kv7s9g8y3npv.cloudfront.net/testsite/files/doc-lib/2018/05/15/10/21/48/448/head/test3.txt#");
    URLConnection request = url.openConnection();
    request.connect();
    final Object content = request.getContent();

    BufferedReader br = new BufferedReader(new InputStreamReader((InputStream) content));
    String inputLine;

    List<String> jsonEntries = new ArrayList<>();
    while ((inputLine = br.readLine()) != null) {
        jsonEntries.add(inputLine);
    }
    String jsonString = "["  + jsonEntries.stream().collect(Collectors.joining(",")) +"]";

    JsonParser jp = new JsonParser();
    JsonElement root = jp.parse(jsonString);
    System.out.println(root);
}

}

Vyncent
  • 1,185
  • 7
  • 17
  • if you check the data in the file, there is no comma separation between the JSON objects. In the worst case, I can append [] to the file by opening the file and saving it as a new file manually. But how can I parse the data? – Geo Thomas May 15 '18 at 16:18
  • I've tried the code above with the url you provided and it seems working to me – Vyncent May 15 '18 at 16:42
  • can you try the URL now, I have updated the file. There is a difference in data now. The objects are not comma separated, they are separate lines in the text file – Geo Thomas May 15 '18 at 16:54
  • @ Vyncent, thanks for the update, I solved it with something similar – Geo Thomas May 16 '18 at 07:11
0

I found a solution which worked well for me

URL url = new URL(urlString.trim());
URLConnection request = url.openConnection();
request.connect();
BufferedReader in = new BufferedReader(
        new InputStreamReader(
                request.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) 
{
    jsonObject = new JSONObject(inputLine);
    jsonArray.put(jsonObject);
}
in.close();

I was able to create a JSON array using the above approach and then iterate over that array to save the data as per my requirement

Geo Thomas
  • 1,139
  • 3
  • 26
  • 59