2

When I use MongoDB 2.x.x I used (BasicDBList) JSON.parse("[]") to parse the string data to Document array. But the latest MongoDB driver says this is deprecated and the BasicDbObject.parse("") only converts to BasicDBObject.

Here is some code samples I earlier used in 2.x.x Java driver

BasicDbList skuList = (BasicDBList) JSON.parse(skus);

So when I upgraded to 3.6.1, the compiler says this is deprecated. And suggested to use BasicDbObject.parse()

But this only accepts JSON objects structured Strings ...

{ "fruit": "apple"}

... not JSON array formatted Strings.

So, if i have a String like "[\"SKU000001\", \"SKU0000002\", \"SKU0000003\"]" how do I convert to BasicDBList?

2
  • ["SKU000001", "SKU0000002", "SKU0000003"] -> this is a JSON array and must be converted to an ArrayList/DBList. How can each element be treated as document? Commented Apr 13, 2018 at 9:08
  • @RahulRaj Sorry, That was my English issue. I corrected them. My question is actually how to convert string to BasicDbList in mongo java driver 3.6.1. Commented Apr 13, 2018 at 9:24

1 Answer 1

6

JSON.parse() is capable of handling a JSON array, it reads the first character and if it deems the JSON to be an array it handles it accordingly:

case '[':
    value = parseArray(name);
    break;

BasicDBObject.parse() expects a valid JSON document so it will throw an exception when given an orphaned JSON array i.e. a JSON array which is not contained in a JSON document.

BasicDBObject.parse() can handle this ...

{"a": ["SKU000001", "SKU0000002", "SKU0000003"]}

... but it cannot handle this:

["SKU000001", "SKU0000002", "SKU0000003"]

So, there is no direct replacement in the MongoDB v3.x driver for using JSON.parse() to parse a JSON array. Instead, your options are:

  1. Trick BasicDBObject.parse() by presenting the JSON array in a valid JSON document, for example:

    BasicDBObject basicDBObject = BasicDBObject.parse(String.format("{\"a\": %s}",
            "[\"SKU000001\", \"SKU0000002\", \"SKU0000003\"]"));
    
    BasicDBList parsed = (BasicDBList) basicDBObject.get("a");
    assertThat(parsed.size(), is(3));
    assertThat(parsed, containsInAnyOrder("SKU000001", "SKU0000002", "SKU0000003"));
    
  2. Use a JSON parsing library to read the JSON array and then use the deserialised result to create a BasicDBList, for example::

    List<String> values = new ObjectMapper().readValue("[\"SKU000001\", \"SKU0000002\", \"SKU0000003\"]",
            List.class);
    
    BasicDBList parsed = new BasicDBList();
    values.forEach(s -> parsed.add(s));
    assertThat(parsed.size(), is(3));
    assertThat(parsed, containsInAnyOrder("SKU000001", "SKU0000002", "SKU0000003"));
    
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. Actually that's what i though as a work around or just use the JSON.parse() with @suppress annotation. Let's wait and see if anyone got any good workarounds.
Seems for now Mongo don't have any solutions. So I'm sticking with my Implementation. In which I'm converting to object formatted string then convert to BasicDBObject then get the BasicDBList from there.

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.