I am using JSONata for performing JSON to JSON transformation.
For some unique reasons, I want to merge two JSONata expressions :
As an example :
Parent Expression:
var script = `
{
"data":
{
"name" : data.payload.Name.(FirstName & ' ' & LastName),
"alias": data.payload.Name.(Salutation & ' ' & FirstName),
"active": data.payload.Status = 'New' ? true : false,
"signature": "Have good day ," & data.payload.Name.FirstName & "!"
}
}
`;
Also I have few simple assignment kind of JSONata expression like :
Expression 1 :
{
"source" : source
}
Expression 2 :
{
"data": {
"email" : data.payload.Email
}
}
I would like to add above two expressions to expressions defined using script
.
So after adding these two expressions, I should be able to get :
var script = `
{
"source": source,
"data":
{
"name" : data.payload.Name.(FirstName & ' ' & LastName),
"alias": data.payload.Name.(Salutation & ' ' & FirstName),
"active": data.payload.Status = 'New' ? true : false,
"signature": "Have good day ," & data.payload.Name.FirstName & "!",
"email": data.payload.Email
}
}
`;
How do I do using javascript/JSONata ?
Background and constraints :
Child Expressions (expression 1 and 2 in the example) (that is supposed to be added into Parent expression) will always be simple assignment like
"a" : x.y.z or "b" : x
.Child Expressions may already be present in parent expression. In that case, it replaces assignment.
Also I want to delete some json paths from parent expression (ofcouse , if it is present) like If delete path
data.Email
.
What I have done ? :
- I tried to convert JSONata script to JSON by putting values under double quotes and encoding value using escape() function.
Once I have JSON, I look for path mentioned in child expression (like data.Email)
- If path exists : replace its value
- If path does not exist : create path and assign value
- If path is supposed to be deleted : simply delete it.
Once I have done processing above JSON, I convert it to JSONata script by removing quotes using bunch of regex and then applying unescape() method for decoding.
The problem with this approach is :
- It is not reliable (regex matching and replacement is not fullproof)
- I am not sure whether every JSONata (which does not declare any functions) can be converted to valid JSON always.