[ https://jira.fiware.org/browse/HELP-13272?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Jose Manuel Cantera updated HELP-13272:
---------------------------------------
Status: In Progress (was: Open)
> [fiware-stackoverflow] Generate array by traversing JSON JavaScript NodeJS
> --------------------------------------------------------------------------
>
> Key: HELP-13272
> URL: https://jira.fiware.org/browse/HELP-13272
> Project: Help-Desk
> Issue Type: Monitor
> Components: FIWARE-TECH-HELP
> Reporter: Backlog Manager
> Assignee: Jose Manuel Cantera
> Labels: fiware, fiware-orion, javascript, node.js
>
> Created question in FIWARE Q/A platform on 05-12-2017 at 15:12
> {color: red}Please, ANSWER this question AT{color} 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": [
> {
> "name": "accuracy",
> "type": "Float",
> "value": 44}
> ]
> },
> {
> "name": "Tem_out",
> "type": "Integer",
> "value": 22
> }
> ]
> },
> "statusCode": {
> "code": "200",
> "reasonPhrase": "OK"
> }
> }
> ]
> }
> 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 ('Error requesting a connection:' + err);
> return;
> }
> 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)
> {
> throw err;
> console.log ('Error when connecting' + error);
> }
> else
> {
> res.status (200) .json (rows);
> }
> });
> // 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": "accuracy",
> "type": "Float",
> "value": 44}
> ]
> },
> {
> "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": [
> {
> "name": "accuracy",
> "type": "Float",
> "value": 2}
> ]
> },
> {
> "name": "Tem_out",
> "type": "Integer",
> "value": 1
> }
> ]
> },
> "statusCode": {
> "code": "200",
> "reasonPhrase": "OK"
> }
> }
> ]
> }
> 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++) {
> tmp.push([json["contextResponses"][i].contextElement.id,
> json["contextResponses"][i].contextElement.type,
> json["contextResponses"][i].contextElement.attributes[0].name,
> json["contextResponses"][i].contextElement.attributes[0].type,
> json["contextResponses"][i].contextElement.attributes[0].value,
> 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]);
> console.log("tmp --> ",tmp);
> }
> 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++) {
> 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
--
This message was sent by Atlassian JIRA
(v6.4.1#64016)
You can get more information about our cookies and privacy policies clicking on the following links: Privacy policy Cookies policy