TripVoltage
TripVoltage

Reputation: 341

Mongo Inserting json doc with nested array elements

I have a json document that has nested array elements. But when I add the document to my mongo db I only writes the last array using the following code:

    mongoColl = mongoDatabase.getCollection("coll"); 
    DBObject dbo = (DBObject)JSON.parse(myJsonString);
    mongoColl.insert(dbo);

    dbObject = mongoColl.findOne();

    String s = JSON.serialize(dbObject);
    JSONObject json = null;
    try 
    {
         json = new JSONObject(s);
         System.out.println(json.toString(4));
    } 
    catch (JSONException e1) 
    {
        e1.printStackTrace();
    }

I puzzled as there is no exception, who do I correctly insert all the array elements in my json document?

Edit I have tried several methods and all output the same result (the last array element in my json document)

Here is my json document: My JSON doc

        // Method 2
        Object jsonObj = s; 
        Object o = com.mongodb.util.JSON.parse(jsonObj.toString());
        DBObject dbObj = (DBObject) o;
        WriteResult result = mongoColl.insert(dbObj);
        System.out.println("Write Result: " + result);

        // Method 3
        JSONArray jsonArray = new JSONArray(s);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("GameofThrones", jsonArray);
        DBObject bson = (DBObject) JSON.parse(jsonObject.toString());
        WriteResult result = mongoColl.insert(bson);
        System.out.println("Write Result: " + result);

Here is the result: Result

Upvotes: 0

Views: 2000

Answers (1)

Blakes Seven
Blakes Seven

Reputation: 50406

The problem here is Your JSON is not valid. The problem here is that you repeat the same "questions" array element over and over in the same document i.e:

{
    "show":[
        {
            "season":[
                {
                    "questions":[
                        { ... } // lots of questions entries
                    ],
                    "questions": [     // It's the same key!
                    ]
                }
            ]
        }
    ]
}

Collapsed editor screenshot so you can see the lines:

Collapsed Structure in Editor

So that's how JSON works. You can only have the key once per object, so all that is happening here is each chunk read and parsed is just replacing the element over and over, until the end.

What you should have:

{
    "show":[
        {
            "season":[
                {
                    "questions":[
                        { ... } // lots of questions entries
                    ]
                },              // <-- End element here
                {               // <-- New element here
                    "questions": [    
                    ]
                }
            ]
        }
    ]
}

So what has happened is whatever process you used to write this list is clearly bugged and did not separate out the objects with the string format it should have done.

Either correct the source that is writing this, or look at a way to process and edit the contents of the text.

Not the fault of the parser ( same thing happens for me with any parser in any language ) but the fault of the data.

Upvotes: 1

Related Questions