Private
Private

Reputation: 1771

Traversing a Json Node using Javascript in marklogic?

I want to traverse a JSON Node which is like this (i.e./node.json)

{
    "One": {
        "Name": "One",
        "Country": "US"
    },
    "Two": {
        "State": "kentucky"
    },
    "Three": {
        "Element1": "value1",
        "Element2": "value2",
        "Element3": "value3",
        "Element4": "value4",
        so on ...
    }
}

Updated My Usecase : I tried to run a CORB job with below configurations

Transform.xqy (Where I am keeping all the elements in an array)

var name = fn.tokenize(URI, ";");
const node = cts.doc(name);
var a= node.xpath("/One/*");
var b= node.xpath("/Two/*");
var c= node.xpath("/Three/*");
fn.stringJoin([a, b, c,name], " , ")

When started running the CORB job to create CSV file for 1.3 M documents in my marklogic DB, it is a never ending story.

Upvotes: 0

Views: 269

Answers (1)

updated to use batch from Corb

OK. Based on more info in the ticket: (1) You are not iterating over the uris (2) You are doing a join on a nested array. Seems odd.

My Example doc:

declareUpdate()
let obj = {
    One: {
        Name: "One",
        Country: "US"
    },
    Two: {
        State: "kentucky"
    },
    Three: {
        Element1: "value1",
        Element2: "value2",
        Element3: "value3",
        Element4: "value4"
    }
}

xdmp.documentInsert('/test1.json', obj)
xdmp.documentInsert('/test2.json', obj)

Based on the example, code that I think would work in your module. The uris is to mimic the batched uris coming from Corb:

let uris= '/test1.json;/test2.json'
let rows = uris.split(';')
rows.map(uri => {
  let obj = fn.head(fn.doc(uri)).toObject()
  return   [
              ...Object.keys(obj.One).map(k => obj.One[k]),
              ...Object.keys(obj.Two).map(k => obj.Two[k]),
              ...Object.keys(obj.Three).map(k => obj.Three[k])               
           ].join(',')
}).join('\n')

Resulting in:

One,US,kentucky,value1,value2,value3,value4    
One,US,kentucky,value1,value2,value3,value4

Upvotes: 3

Related Questions