I am writing REST API to perform transformation using library like JSONata.
API receive a JSON that has data and map as given below. This is simple example.
{
"map":{
"name":"title",
"info":"description",
"data":{
"text":"blog",
"date":"date"
}
},
"data":{
"title":"title1",
"description":"description1",
"blog":"This is a blog.",
"date":"11/4/2013"
}
}
The Node.JS code is given below.
app.post('/JSONTransform', function (req, res, next) {
const data = req.body.data;
const map = req.body.map;
var expression = jsonata(map);
var result = expression.evaluate(data);
res.send(result);
});
The map variable contain the following
{
"name":"title",
"info":"description",
"data":{
"text":"blog",
"date":"date"
}
}
JSONdata expects value to be JSON Path without quotes as below.
{
"name":title,
"info":description,
"data":{
"text":blog,
"date":date
}
}
Please suggest best option to remove the quotes only in all JSON Values.
I write function to remove the quotes in value by iterating the JSON object.
function jsoniterate(map) {
Object.keys(map).forEach(key => {
console.log(map[key]);
if (typeof (map[key]) == 'object') {
arr1.push('"' + key + '": {');
jsoniterate(map[key]);
arr1.push('}');
}
else {
arr1.push('"' + key + '":' + map[key]);
}
});
}
but the complexity increase when the JSON value has object and so on . and also ',' should be placed between each set of key value pair.
I found some solution using regular expression but that target only for numeric value in the JSON Value. Here, it should be for all JSON values
JSONdata expects value to be JSON Path without quotes as below.
{
"name":title,
"info":description,
"data":{
"text":blog,
"date":date
}
}