3

I have a big JSON file (> 1Gb) which includes an array of objects:

[
   {
      "Property1":"value",
      "Property2":{
         "subProperty1":"value",
         "subProperty2":[
            "value",
            "value"
         ]
      },
      "Property3":"value"
   },
   {
      "Property1":"value",
      "Property2":{
         "subProperty1":"value",
         "subProperty2":[
            "value",
            "value"
         ]
      },
      "Property3":"value"
   }
]

Currently, I parse this JSON using Gson but it doesn't work, I have following error: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

In order to parse this JSON, I did following:

reader = new BufferedReader(new FileReader(jsonFile));
Gson gson = new GsonBuilder().create();
Type typeArray = new TypeToken<List<String>>(){}.getType();
List<String> topics = gson.fromJson(reader, typeArray);

I want to parse this JSON array as String Array. In other words, I want a Java list of string instead of a Java list of objects. Like that :

topics[0] = "{\"Property1\":\"value\",\"Property2\":{\"subProperty1\":\"value\",\"subProperty2\":[\"value\",\"value\"]},\"Property3\":\"value\"}";
topics[1] = "{\"Property1\":\"value\",\"Property2\":{\"subProperty1\":\"value\",\"subProperty2\":[\"value\",\"value\"]},\"Property3\":\"value\"}";

Thank you :)

6
  • I'm not sure it works that way. What's the problem with parsing the entire file and reformatting the individual objects back to a json string? Is it a memory problem? Commented May 15, 2017 at 8:48
  • @Thomas I don't think this is a memory problem. I think this is a parsing problem. For my test, I have a little JSON (~10Mb) Commented May 15, 2017 at 8:54
  • Can you check the charset encoding of the JSON file? It could be a problem with an UTF byte-order-mark, for instance. Please paste in a minimal example JSON with which this can be reproduced. Commented May 15, 2017 at 8:59
  • See also Why does Gson fromJson throw a JsonSyntaxException: Expected some type but was some other type? Commented May 15, 2017 at 9:03
  • @MickMnemonic The charset encoding of my JSON file seems fine. It works well with json-simple API but I need Gson in order to parse huge Json File. Here is a raw extract of my JSON file and here, a formatted extract. This an array of only 2 complex objects, but finally, It needs work on the same array but with around 500,000 complex objects (~1Gb) Commented May 15, 2017 at 9:10

1 Answer 1

3

Something like this should work:

public List<String> convertToStringArray(File file) throws IOException {
    List<String> result = new ArrayList<>();
    String data = FileUtils.readFileToString(file, "UTF-8");
    JsonArray entries = (new JsonParser()).parse(data).getAsJsonArray();
    for (JsonElement obj : entries)
        result.add(obj.toString());
    return result;
}

I used file reader from apache.commons.io, but you could replace that with native Java reader... Also, if you need that topics[0] = in each string you could add that with:

result.add(String.format("topics[%s] = %s", result.size(), obj.toString()));

These are used imports from gson:

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
Sign up to request clarification or add additional context in comments.

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.