Eric Kolotyluk Eric Kolotyluk - 3 months ago 17
JSON Question

How do I insert more that one triple in a Marklogic JSON document?

I am trying to insert some triples, via Javascript (query console), into a JSON document of the form

declareUpdate();
xdmp.documentInsert('/aem/5/content/demo-spark/en_GB/automation_article.json',
{
"triple" : {
"subject" : "https://content.ea.com/aem/5/content/demo-spark/en_GB/automation_article2.json",
"predicate" : "https://content.ea.com/iri/author",
"object" : "jasonmoore"
},
"triple" : {
"subject" : "https://content.ea.com/aem/5/content/demo-spark/en_GB/automation_article2.json",
"predicate" : "https://content.ea.com/iri/id",
"object" : "automation_article2"
},
"triple" : {
"subject" : "https://content.ea.com/aem/5/content/demo-spark/en_GB/automation_article2.json",
"predicate" : "https://content.ea.com/iri/dateCreated",
"object" : "2015-08-14 09:38:10 GMT-7:00"
},
"content" : {
. . .
}
});


However, when I look in the newly created document, only the last triple is there, the other two are missing.

What do I need to do to get the first two triples in the same document?

Answer

A JSON object stores key-value pairs. The keys are unique.

var obj = {
  a : 'This is a property, but it will be overwritten',
  a : 'Im really the value of a property'
};

console.log(obj);

That's the same to say :

var obj = {
  a : 'This is a property, but it will be overwritten'
};

obj['a'] = 'Im really the value of a property';

console.log(obj);

Now you can think what's happening: Everytime you try to insert in the key "triple" is overwritting what it contains, and the value that finally stores is the last one.

var myDbObject = {};
var obj = {
  "triple" : {
    "subject" : "https://content.ea.com/aem/5/content/demo-spark/en_GB/automation_article2.json",
    "predicate" : "https://content.ea.com/iri/author",
    "object" : "jasonmoore"
  },
  "triple" : {
    "subject" : "https://content.ea.com/aem/5/content/demo-spark/en_GB/automation_article2.json",
    "predicate" : "https://content.ea.com/iri/id",
    "object" : "automation_article2"
  },
  "triple" : {
    "subject" : "https://content.ea.com/aem/5/content/demo-spark/en_GB/automation_article2.json",
    "predicate" : "https://content.ea.com/iri/dateCreated",
    "object" : "2015-08-14 09:38:10 GMT-7:00"
  }
};

Object.keys(obj).forEach(key=>{
  myDbObject[key] = obj[key];
});

console.log(myDbObject);