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.

3
  • 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. Commented May 15, 2018 at 12:48
  • Try this link Commented May 15, 2018 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 Commented May 15, 2018 at 12:53

2 Answers 2

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);
}

}

Sign up to request clarification or add additional context in comments.

4 Comments

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?
I've tried the code above with the url you provided and it seems working to me
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
@ Vyncent, thanks for the update, I solved it with something similar
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.