0

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

4
  • It's not a good idea to use the JSON.parse or JSON.serialize methods - I know they look like they're exactly what you need, but the default MongoDB Java driver doesn't have full official support for JSON. You're better off using a library that's designed for this, such as MongoJack. Commented Aug 5, 2015 at 8:28
  • It may be possible to get your code to work correctly, but if you post the output of running this code, that would help us Commented Aug 5, 2015 at 8:28
  • @Trisha Thanks but looking at MongoJack seems to be for POJO's and I'm just using a regular json string document in Java. Maybe there are others you know of that would be more suitable to use in this instance? Commented Aug 5, 2015 at 9:19
  • @Trisha - Think this may just be what I need instead jsonschema2pojo.org Commented Aug 5, 2015 at 9:41

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Many thanks that was totally it. It was I who hashed the json document together for some server testing and json validator passed it so I assumed the problem was something else! Well spotted and great answer too. What editor is that your using BTW?

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.