Couchbase insert array using spark connector

Hi I have List as below :

val myList = List("a","b","c","d') and I want my bucket structure as below :

{
  "data": [
           "a",
           "b",
           "c",
           "d"
         ]
  }

but it insert like this

{
 "data": [
       "a,b,c,d"
    ]
 }

and my spark connector code as below :

myList.map(values=> JsonDocument.create("test", JsonObject.create().put("data", JsonArray.empty.add(values)))

I’m not sure you need the .map(), wouldn’t this do what you need?

val json = JsonDocument.create("test", JsonObject.create().put("data", JsonArray.from(myList)))

I tried the given solution but it return this error

java.lang.IllegalArgumentException: Unsupported type for JsonArray: class [Ljava.lang.String;

Ah yes, sorry that’s my mistake, please try this instead (the JsonDocument API is in Java, so using Java collections makes it all work as you’d expect):

val myList = new util.ArrayList[String]()
myList.addAll(util.Arrays.asList("a", "b", "c"))
val json = JsonDocument.create("test",
  JsonObject.create().put("data", myList))

Or, more concisely:

val json = JsonDocument.create("test",
  JsonObject.create().put("data",
    util.Arrays.asList("a", "b", "c")))
1 Like