1

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.

Jonas K.
  • 21
  • 6
  • Can you iterate over the keys in the JSONObject and see if that offers any clues? (https://stackoverflow.com/questions/9151619/how-to-iterate-over-a-jsonobject) – Michael Berry Jun 27 '18 at 09:53
  • Well, iterating over the keys as explained in the first answer in your link only prints "_id". Im working on giving some formatted code to that but it doesn't seem to work... – Jonas K. Jun 27 '18 at 11:11
  • Using this: https://stackoverflow.com/a/32412500/9977047 , I do get all the keys and values. But one key is missing, this time it's "Feedback". Although the value of the "Feedback" key is printed. Like this: "Ort : Elsterwerda" "Wetter-Keyword : Anderes" ": Test Gelb" – Jonas K. Jun 27 '18 at 11:27
  • At a guess, it might be the carriage return character in the feedback key that's throwing it off. Is there any way you can remove that and check if you still get the same behaviour? – Michael Berry Jun 27 '18 at 11:31
  • The "\r" seems to be automatically inserted into the document when getting it out of the DB. That could be the problem. – Jonas K. Jun 27 '18 at 11:45
  • It's certainly suspect. I'd start with removing that and see if it works - if not we can start exploring other possibilities. – Michael Berry Jun 27 '18 at 11:47
  • Reading the keys and values works when the "\r" isn't in there. So the curious thing is, that the "/r" is automatically generated at the last key-value pair, when I import a .csv into my MongoDB (but only as soon as I get the documents in my program). That makes sense, since in a .csv there are line brakes at the end to indicate a new dataset. If I import a JSON file to my MongoDB, the "\r" is not generated. So iterating the keys and importing a JSON file to my MongoDB does produce the desired results. However, the problem described first in my question remains unaffected. – Jonas K. Jun 27 '18 at 12:29

1 Answers1

1

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.

Jonas K.
  • 21
  • 6