Insert a new array item at the end of JSON array using MarkLogic Patch Builder Insert a new array item at the end of JSON array using MarkLogic Patch Builder json json

Insert a new array item at the end of JSON array using MarkLogic Patch Builder


Under the hood the Node.js Client API uses the MarkLogic REST API and if you hit up its documentation you'll find a section on limitations working with JSON documents.

In this section it is explained that it's not possible to insert a property immediately after an "anonymous" root property. (This would be a JSON document where there's no named root property).

Your best option at the moment would be to retrieve the document, do the update in your JavaScript code and reinsert the document. This could be achieved in the following way:

const uri = '/cities.json';db.documents.read(uri).result().then(response => {  const document = response[0].content;  document.push({ city: 'Denver', pop: '682,545 (2015)' });  return db.documents.write({ uri, content: document }).result();}).then(response => console.info(response)).catch(error => console.error(error));