Details
-
Type: Monitor
-
Status: Closed
-
Priority: Major
-
Resolution: Done
-
Affects Version/s: None
-
Fix Version/s: 2021
-
Component/s: FIWARE-TECH-HELP
-
Labels:
-
HD-Chapter:Data
-
HD-Enabler:Orion
Description
Created question in FIWARE Q/A platform on 05-12-2017 at 15:12
Please, ANSWER this question AT https://stackoverflow.com/questions/47655751/generate-array-by-traversing-json-javascript-nodejs
Question:
Generate array by traversing JSON JavaScript NodeJS
Description:
I am working with fiware and when making a subscriptions this information is being sent to a method that defined in my rest API, and I would like to save that information in my database.
The information that comes to me from the ContextBroker is:
{
"subscriptionId": "5a268a598dc68904bbc7b3cf",
"originator": "localhost",
"contextResponses": [
{
"contextElement": {
"type": "Temperature",
"isPattern": "false",
"id": "S_Temp001",
"attributes": [
{
"name": "Tem_int",
"type": "float",
"value": 22,
"metadatas": [
]
},
]
},
"statusCode":
}
]
}
The method that I have defined in my API is this:
// insertBD
function postinsertbdarduino1 (req, res) {
var inver =
{ fiwareServicePath: String, entityId: String, entityType: String, attrName: String, attrType: String, attrValue: String, attrMd: String };
var params = req.body;
// check if the pass && name etc arrive and encrypt
// Assign values to the user object
//inver.entityId = params.entityId;
inver.fiwareServicePath = params.fiwareServicePath;
inver.entityId = params.id;
inver.entityType = params.type;
inver.attrName = params.name;
inver.attrType = params.attrType;
inver.attrValue = params.attrValue;
inver.attrMd = params.attrMd;
pool.getConnection (function (err, connection) {
if (err)
console.log ("connection tests");
// if we want to add other conditions in the where would it be to put the fields and? and then to what is it equivalent
// 'SELECT * FROM all WHERE id =? AND username =? ', [Req.params.id, user.username], function (err, rows)
connection.query ('INSERT INTO inver SET?', [inver], function (err, rows) {
if (err)
else
});
// so that it can be reused by the pool, it is important to "release" it
connection.release ();
});
}
For now I'm just passing the id, the type and the name.
But he's saving my record in the empty database.
How can i fix this?
Thanks greetings.
EDIT1
I just launched the JSON I changed in my API method by the subscriptionId value inver.entityId = params.subscriptionId; and if the data arrives, but I need to recover the data " type ":" Temperature " " id ":" S_Temp001 ",
{
"name": "Tem_int",
"type": "float",
"value": 22,
"metadatas": [
]
},
{ "name": "Tem_out", "type": "Integer", "value": 22 }
How can I get to that part of the JSON?
Thank you.
EDIT2
Good,
I modify the question.
How can I run this JSON in NODEJS JavaScript:
{
"subscriptionId": "5a268a598dc68904bbc7b3cf",
"originator": "localhost",
"contextResponses": [
{
"contextElement": {
"type": "Temperature",
"isPattern": "false",
"id": "S_Temp001",
"attributes": [
{
"name": "Tem_int",
"type": "float",
"value": 2,
"metadatas": [
]
},
]
},
"statusCode":
}
]
}
Greetings and thank you.
EDIT03
Hello,
I need to generate the following format for my array.
var tmp = [['S_Temp001', 'Temperature', 'Tem_int', 'float', 2, 'accuracy',
'Float', 2], ['S_Temp001', 'Temperature', 'Tem_out', 'Integer', 1, 'accuracy',
'Float', 2]];
var tmp = [];
for (var i = 0; i < json["contextResponses"].length; i++)
EDIT04 solved
Hi,
I have already found the solution, I add it in case it could be worth 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++)
}
}
a greeting
Activity
- All
- Comments
- History
- Activity
- Transitions
2018-01-12 19:48|CREATED monitor | # answers= 0, accepted answer= False