Good,
I modify the question.
How can I go through this JSON in NODEJS JavaScript:
{
"subscriptionId" : "5a268a598dc68904bbc7b3cf",
"originator" : "localhost",
"contextResponses" : [
{
"contextElement" : {
"type" : "Temperatura",
"isPattern" : "false",
"id" : "S_Temp001",
"attributes" : [
{
"name" : "Tem_int",
"type" : "float",
"value" : 2,
"metadatas" : [
{
"name" : "accuracy",
"type" : "Float",
"value": 2 }
]
},
{
"name" : "Tem_out",
"type" : "Integer",
"value" : 1
}
]
},
"statusCode" : {
"code" : "200",
"reasonPhrase" : "OK"
}
}
]
}
Greetings and thank you.
EDIT01
Right now the answer I have is:
[ [ 'S_Temp001', 'Temperatura', 'Tem_int', 'float', 2, 'accuracy', 'Float',2 ] ]
I am saving it in DB in such a way (it works correctly):
var sql = "INSERT INTO inver (fiwareServicePath, entityId, entityType, attrName, attrType, attrValue) VALUES ?";
connection.query(sql, [tmp], function(err, rows) {
if(err)
{
throw err;
console.log('Error al Conectar' + error);
}
else
{
res.status(200).json(rows);
}
});
Now my question: I'm only saving what belongs to attributes[0]
I need to generate another array separated by , and inserted inside [] in the same tmp array, that is to say something like this:
var tmp= [['S_Temp001', 'Temperatura', 'Tem_int', 'float', 2, 'accuracy',
'Float',2],['S_Temp001', 'Temperatura', 'Tem_out', 'Integer', 1, 'accuracy',
'Float',2]];
For the metadata I have added this in the code you have provided:
json["contextResponses"][i].contextElement.attributes[0].metadatas[0].name,
json["contextResponses"][i].contextElement.attributes[0].metadatas[0].type,
json["contextResponses"][i].contextElement.attributes[0].metadatas[0].value]);
I need it like this to insert this list in my database and each array inside in a new row, so the example that I have given you would look like:
I suppose that I have to generate a new for within this for (var i = 0; i < json["contextResponses"].length; i++)
so that it goes through the attributes and adds them as a new array with the structure that I have mentioned before.
var tmp= [['S_Temp001', 'Temperatura', 'Tem_int', 'float', 2, 'accuracy',
'Float',2],['S_Temp001', 'Temperatura', 'Tem_out', 'Integer', 1, 'accuracy',
'Float',2]];
I do not know if I explained well.
Thank you.
EDIT02 Solution
Good,
I have already found the solution, I add it in case it can be useful to someone.
var tmp2 = [];
for (var i = 0; i < params.contextResponses.length; i++) {
for (var j = 0; j < params.contextResponses[i].contextElement.attributes.length; j++) {
for (var k = 0; k < params.contextResponses[i].contextElement.attributes[j].metadatas.length; k++) {
console.log("nuevo log antes de petar: " + params["contextResponses"][i].contextElement.attributes[j].metadatas[k].value);
tmp2.push([params.contextResponses[i].contextElement.id,
params.contextResponses[i].contextElement.type,
params.contextResponses[i].contextElement.attributes[j].name,
params.contextResponses[i].contextElement.attributes[j].type,
params.contextResponses[i].contextElement.attributes[j].value,
params.contextResponses[i].contextElement.attributes[j].metadatas[k].name,
params.contextResponses[i].contextElement.attributes[j].metadatas[k].type,
params.contextResponses[i].contextElement.attributes[j].metadatas[k].value]);
console.log("tmp2 --> ",tmp2);
}
}
}
A greeting and thanks for the help.
Modify the JSON thinking more or less about what you need to see it, and at the end add a for in which the DATA remains in the format that you need (you can make yourself a way of how to do it in case I don't know exactly what you need). Even so, let me know if it is not what you need and if it is possible to give me a little more detail to give a more accurate answer.
I hope if you still hadn't been able to solve it, I hope this helps you or if you have an easier way to do what I asked for, share :)
PS: It's only to go through the complete object, if you need more things, let me know.
PS: Sorry for the delay, I hadn't had much time and an update left me in a mess on my note xD.
If you only require certain json values, you could use destructuring, here is an example of what I mean:
More on the subject => https://developer.mozilla.org/es/docs/Web/JavaScript/Reference/Operator/Destructuring_assignment