how to insert node in JSON doc in Marklogic using xdmp:node-insert - nosql

I want to insert node in a JSON document like this:
{
"fileName": "l",
"user_id": "test",
"requestId": "1232",
"feedbackType": "cpd_mah"
}
Running this code:
let $old := $doc/filename
return
xdmp:node-insert-after($old, object-node{"isSaved": ""})
but this is throwing an error XDMP-CHILDUNNAMED.

I agree with Mads, that using node-insert-child makes more sense with JSON. You can use node-insert-after though. Your attempt is throwing an XDMP-CHILDUNNAMED error because you are passing in an unnamed object node (which is basically an anonymous wrapper for its properties), rather than the named property you'd like to insert. Mads code is giving away how you can make it work:
let $old := doc("/test.json")/filename
return
xdmp:node-insert-after($old, object-node{"isSaved": ""}/isSaved)
Note: if you run it multiple times, it will replace rather than insert, since properties should be unique.
HTH!

JSON properties don't have the concept of a sibling, the way that XML elements do.
A JSON object is basically a map with a set of properties. So, rather than inserting an object as a child node after one of the properties, you want to insert a child node (notice the XPath selecting isSaved from the constructed object) of the JSON document object-node()
let $old := doc("/test.json")/object-node()
return
xdmp:node-insert-child($old, object-node{"isSaved": ""}/isSaved)
A few examples of working with JSON in XQuery from the MarkLogic documentation:
https://docs.marklogic.com/guide/app-dev/json#id_60123

Related

How to extract the value from a json object in Azure Data Factory

I have my ADF pipeline, Where my final output from set variable activity is something like this {name:test, value:1234},
The input coming to this variable is
{
"variableName": "test",
"value": "test:1234"
}
The expression provided in Set variable Item column is #item().ColumnName. And the ColumnName in my JSon file is something like this "ColumnName":"test:1234"
How can I change it so that I get only 1234. I am only interested in the value coming here.
It looks like you need to split the value by colon which you can do using Azure Data Factory (ADF) expressions and functions: the split function, which splits a string into an array and the last function to get the last item from the array. This works quite neatly in this case:
#last(split(variables('varWorking'), ':'))
Sample results:
Change the variable name to suit your case. You can also use string methods like lastIndexOf to locate the colon, and grab the rest of the string from there. A sample expression would be something like this:
#substring(variables('varWorking'),add(indexof(variables('varWorking'), ':'),1),4)
It's a bit more complicated but may work for you, depending on the requirement.
It seems like you are using it inside of an iterator since you got item but however, I tried with a simple json lookup value
#last(split(activity('Lookup').output.value[0].ColumnName,':'))

Delete specific value from firebase database using swift

Firebase Database
I tried using this bit of code but it doesn't seem to work. I take the name the user selects and store it in nameList.
Lets say I store Blake Wodruff in nameList[0].
How do I remove only that name?
var nameList = [String](repeating: "", count:100)
func remove() {
print(nameList[countAddNames])
let usernameRef = Database.database().reference().child("Candidate 1").child("alton").child(nameList[countAddNames]);
usernameRef.removeValue();
}
To write to or delete a node, you must specify its entire path. So to delete node 0 from your JSON, you'd do:
let usernameRef = Database.database().reference().child("Candidate 1").child("alton").child("0");
usernameRef.removeValue();
Or a bit shorter:
let usernameRef = Database.database().reference().child("Candidate 1/alton/0");
usernameRef.removeValue();
If you only know the name of the user you want to remove, you'll need to first look up its index/full path before you can remove it. If you have the data in your application already, you can do it in that code. Otherwise you may have to use a database query (specifically .queryOrderedByValue and .queryEqualToValue) to determine where the value exists in the database.
Also see: Delete a specific child node in Firebase swift
Once you remove a value from your JSON structure, Firebase may no longer recognize it as an array. For this reason it is highly recommended to not use arrays for the structure that you have. In fact, I'd model your data as a set, which in JSON would look like:
"alton": {
"Jake Jugg": true,
"Blake Wodruff": true,
"Alissa Sanchez": true
}
This would automatically:
Prevent duplicates, as each name can by definition only appear once.
Make removing a candidate by their name as easy as Database.database().reference().child("Candidate 1/alton/Jake Jugg").removeValue()
For more on this, also see my answer to Firebase query if child of child contains a value

How do I assign array of Objects as value to an Attribute in Apache NiFi

In Apache NiFi, My Mongo DB is returning Array of Objects as shown below
[
{ fname:john,
city:Nyc
},
{ lname:doe,
city:Nj
}
]
I am trying to assign the entire array of Objects to an Attrbiute using EvaluateJsonPath Processor,
my EvaluateJsonPath configuration is as shown below,but I am getting "EvaluateJsonPath unable to return a scalar" error.
I tried $.* in fullname value, tried changing return type to Scalar and auto detect. still I am getting the same error.
How do I assign the entire array of objects to an attribute in Apache NiFi
In case,if any one is interested
Changing Return type to Json and Fullname value to $.* resolved the issue.

Data factory lookup (dot) in the item() name

I am having lookup wherein salesforce query is there. I am using elements (item()) in subsequent activities. Till now i had item().name or item().email but now i have item().NVMStatsSF__Related_Lead__r.FirstName which has (dot) in the field name.
How should i parse it through body tag so that it reads it correctly?
So I have the following data in item()
{
"NVMStatsSF__Related_Lead__c": "00QE000egrtgrAK",
"NVMStatsSF__Agent__r.Name": "ABC",
"NVMStatsSF__Related_Lead__r.Email": "geggegg#gmail.com",
"NVMStatsSF__Related_Lead__r.FirstName": "ABC",
"NVMStatsSF__Related_Lead__r.OwnerId": "0025434535IIAW"
}
now when i use item().NVMStatsSF__Agent__r.Name it will not parse because of (dot) after NVMStatsSF__Agent__r. And it is giving me the following error.
'item().NVMStatsSF__Related_Lead__r.Email' cannot be evaluated because property 'NVMStatsSF__Related_Lead__r' doesn't exist, available properties are 'NVMStatsSF__Related_Lead__c, NVMStatsSF__Agent__r.Name, NVMStatsSF__Related_Lead__r.Email, NVMStatsSF__Related_Lead__r.FirstName, NVMStatsSF__Related_Lead__r.OwnerId'.",
"failureType": "UserError",
"target": "WebActivityToAddPerson"
this is because ADF uses '.' for object reading.
Could you find a way to rename the field name which contains '.'?
Seems like you need a built-in function to get the value of an object according to the key. Like getValue(item(), 'key.nestkey'). But unfortunately, seems there isn't such a function. You may need handle your key first.
Finally, it worked. I was being silly.
Instead of taking the value from the child table with the help of (dot) operator I just used subquery. Silly see.
And it worked.

Parse JSON in Drools WHEN block

I am trying to automate the rule creation using Rule Template.
I am using ResultSetGenerator which will get the result set from Database and create the rule.
One of the column(Grade) in the DB table is of type JSON with following value:
{
"grade":"A",
"subjects":["Math","Geography"]
}
I want the use the above JSON in drools WHEN block in the following way.
rule "TEST"
WHEN
s:String(this==#{Grade})
THEN
//DO SOMETHING
How can I parse the Json of the Grade column to get the value for "grade" attribute from it and use it in WHEN block.
Please help.
Thanks
Try assigning the JSON to a Map object instead.
The "subjects" element of the Map object should be able to be assigned to a List object as well.
We have a rulesengine that passes raw JSON as facts, and we parse through the JSON by using Map() in the WHEN section.