3

I want to parse json file "c:/employeesRecord.json" using org.json.simple library. Below is sample json data, each record is sperated by next line in the file.

{"Employees ":["A ;B ;C ;D ;E ;F"],"JobTitle":"Software Engineer"}
{"Employees ":["A ;B ;C ;D ;E ;F"],"JobTitle":"Software Engineer"}
{"Employees ":["A ;B ;C ;D ;E ;F"],"JobTitle":"Software Engineer"}
...

How to parse such json file without root element using org.json.simple.

halfer
  • 19,824
  • 17
  • 99
  • 186
Bahadar Ali
  • 173
  • 1
  • 3
  • 14

3 Answers3

3

You can "tweak" the input from the file and change it into a valid json format

String json = <your json from file>
json = "[" + json + "]";
json = json.replace("\n",",");
// parse your json, now it should be a valid.
Marcx
  • 6,806
  • 5
  • 46
  • 69
2

Since the full file is not a valid JSON object you will need to parse it line-by-line, ie. read first line, parse it using your parser, save the result and then repeat for the next line.

AleSod
  • 422
  • 3
  • 6
1

Parse it line by line after spliting by \n.

String[] lines = json.split("\n");
List<JsonObject> objects = new ArrayList<>(lines.length); // depending on the JSON library you are using

for(String line : lines) {
    objects.add(parseJson(line));
}
SOFe
  • 7,867
  • 4
  • 33
  • 61