Hi @Ashish_Mishra,
I am sure you could perform what you need in Java via the couchbase elastic connector, however I am focused on Eventing so my answer will be specifically for Eventing type solutions, I will leave a Java coding solution to others like @david.nault - but without trying anything out, I think you could follow the example here https://mkyong.com/java/jackson-2-convert-java-object-to-from-json/
From the Eventing perspective you could write a simple function like
function OnUpdate(doc, meta) {
// skip non-interesting docs
if (!doc.type && doc.type !== "page") return;
doc.items = JSON.stringify(doc.items);
// This is a write to an binding in settings could be
// the source bucket/keyspace or another bucket/keyspace
tgt_bkt[meta.id] = doc;
}
If your bucket binding alias (in the Eventing function’s settings) is the same as the Eventing source bucket it will convert you documents to something like:
{
"id": "12131",
"type": "page",
"items": "[{\"id\":\"12131\",\"props\":\"sdads\"},{\"id\":\"2323\",\"props\":\"sdads\"}]"
}
If your bucket binding alias (in the Eventing function’s settings) is to another the bucket you would get a duplicate item but in the way you want and your Kafka connector could listen to that bucket (or keyspace in 7.X).
Alternatively is space is not a concern you could do the following and just enrich the source documents in place
function OnUpdate(doc, meta) {
// skip non-interesting docs
if (!doc.type && doc.type !== "page") return;
doc.items_as_string = JSON.stringify(doc.items);
// This is a write to an binding in settings could be
// the source bucket/keyspace or another bucket/keyspace
tgt_bkt[meta.id] = doc;
}
This second function will store your array in both forms a true JSON array and a stringified array.
{
"id": "12131",
"type": "page",
"items": [
{
"id": "12131",
"props": "sdads"
},
{
"id": "2323",
"props": "sdads"
}
],
"items_as_string": "[{\"id\":\"12131\",\"props\":\"sdads\"},{\"id\":\"2323\",\"props\":\"sdads\"}]"
}
Best
Jon Strabala
Principal Product Manager - Server