Jonas K.
Jonas K.

Reputation: 21

Cannot read the first line of a JSON file in Java

I am trying to read some data from a JSON file that I generated from a MongoDB document. But when trying to read the first entry in the document, i get an exception:

org.json.JSONException: JSONObject["Uhrzeit"] not found.

This only happens with the first entry, reading other entrys does not cause an exception. Using jsonObject.getString("") on any entry that is not the first returns the values as expected.

    //Initiate Mongodb and declare the database and collection
    MongoClient mongoClient = new MongoClient(new MongoClientURI("mongodb://localhost:27017"));
    MongoDatabase feedbackDb = mongoClient.getDatabase("local");
    MongoCollection<Document> feedback = feedbackDb.getCollection("RückmeldungenShort");

    //gets all documents in a collection. "new Document()" is the filter, that returns all Documents
    FindIterable<Document> documents = feedback.find(new Document());

    //Iterates over all documents and converts them to JSONObjects for further use
    for(Document doc : documents) {

        JSONObject jsonObject = new JSONObject(doc.toJson());

        System.out.print(jsonObject.toString());
        System.out.print(jsonObject.getString("Uhrzeit"));

    }

Printing jsonObject.toString() produces the JSON String for testing purposes (in one line):

{  
   "Ort":"Elsterwerda",
   "Wetter-Keyword":"Anderes",
   "Feedback\r":"Test Gelb\r",
   "Betrag":"Gelb",
   "Datum":"18.05.2018",
   "Abweichung":"",
   "Typ":"Vorhersage",
   "_id":{  
      "$oid":"5b33453b75ef3c23f80fc416"
   },
   "Uhrzeit":"05:00"
}

Note, that the order in which the entries appear is mixed up and the first one appearing in the database was "Uhrzeit".

This is how it looks like:

inside the MongoDB.

The JSON file is valid according to https://jsonformatter.curiousconcept.com/ .

The "Uhrzeit" is even recognized within the JSONObject while in debug mode:

debug mode.

I assumed it might have something to do with the entries themselves, so I switched "Datum" and "Ort" to the first place in the document but that produced the same results.

There are lots of others that have posted on this error message, but it seems to me like they all had slightly different problems.

Upvotes: 1

Views: 580

Answers (1)

Jonas K.
Jonas K.

Reputation: 21

I imported a .csv with my data into MongoDB and read the documents from there. Somewhere in the process of reading the data, "\r"s were automatically generated where the line breaks were in my .csv (aka. at the end of each dataset). In this case at the key value pair "Feedback" (as seen in the last picture).

When checking my output again with another JSON validator, I noticed that there was an "invisible" symbol in my JSON file that caused the key not to be found. Now this symbol is located in front of the first key (after the MongoDB-id) when importing a .csv document to my DB. I imported a correct version of the .csv into my MongoDB and exported it again and the symbol reappeared.

The problem was that my .csv was in "Windows" format. Converting it to "Unix" format will get rid of the generated "\r"s. The "invisible" symbol was the UTF-8-BOM code that is added at the beginning of a document. You can reformat your .csv to be just UTF-8 and get rid of it that way.

Upvotes: 1

Related Questions