简体   繁体   English

从字典中提取值(在 Node-RED 中)

[英]Extract values from dictionary (in Node-RED)

I'm making a simple HTTP GET request我正在做一个简单的 HTTP GET 请求在此处输入图片说明

and the response, assuming the function just contains return msg;和响应,假设function只包含return msg; , is a dictionary, such as: , 是字典,例如:

{
    "data": [{
        "time": "2020-10-01",
        "value": 998.37
    }],
    "id": "27",
    "tags": []
}

Now I want to extract just the value itself.现在我只想提取值本身。 How can I do that?我怎样才能做到这一点? If I try return msg['data']['value'];如果我尝试return msg['data']['value']; I get a TypeError TypeError: Cannot read property 'value' of undefined .我收到 TypeError TypeError: Cannot read property 'value' of undefined

Edit: I tried Pogrindis approach, but am struggling with the Node-RED flowfiles.编辑:我尝试过 Pogrindis 方法,但我正在努力使用 Node-RED 流文件。

The JSON of my Node-RED interface is pretty simple, the function is a one-liner我的 Node-RED 接口的 JSON 非常简单,函数是单行的

return (msg.payload)['data'][0]['value']; :

[{
    "id": "cda42b56.ad2258",
    "type": "tab",
    "label": "Test",
    "disabled": false,
    "info": ""
}, {
    "id": "95fa7937.aa8e48",
    "type": "debug",
    "z": "cda42b56.ad2258",
    "name": "Logger",
    "active": true,
    "tosidebar": true,
    "console": true,
    "tostatus": false,
    "complete": "payload",
    "targetType": "msg",
    "statusVal": "",
    "statusType": "auto",
    "x": 480,
    "y": 80,
    "wires": []
}, {
    "id": "200acc6f.e2ff54",
    "type": "function",
    "z": "cda42b56.ad2258",
    "name": "",
    "func": "return (msg.payload)['data'][0]['value'];\n",
    "outputs": 1,
    "noerr": 0,
    "initialize": "",
    "finalize": "",
    "x": 300,
    "y": 80,
    "wires": [
        ["95fa7937.aa8e48"]
    ]
}, {
    "id": "831bfe43.3d956",
    "type": "inject",
    "z": "cda42b56.ad2258",
    "name": "",
    "props": [{
        "p": "payload"
    }],
    "repeat": "",
    "crontab": "",
    "once": false,
    "onceDelay": 0.1,
    "topic": "",
    "payload": "{\"data\":[{\"time\":\"2020\",\"value\":997.62}],\"id\":\"xy123\"}",
    "payloadType": "str",
    "x": 120,
    "y": 80,
    "wires": [
        ["200acc6f.e2ff54"]
    ]
}]

data is an array, and value is not the index of said array. data是一个数组,而 value 不是该数组的索引。

You could use an index for data I guess我猜你可以对数据使用索引

const testData = {
    "data": [{
        "time": "2020-10-01",
        "value": 998.37
    }],
    "id": "27",
    "tags": []
};

console.log(testData['data'][0]['value']);

JSFiddle JSFiddle

I say I guess, because with N number of data, you might be better off using some kind of iterator/filter etc but I don't know your use case.我说我猜,因为有 N 个数据,你最好使用某种迭代器/过滤器等,但我不知道你的用例。

The important thing to remember is that a Function node must always return a msg object, not a single value.要记住的重要一点是,Function 节点必须始终返回一个msg对象,而不是单个值。

The error is coming from the debug node (if you mouse over the error in the sidebar it will show which node it came from) because you are returning a raw value and not a msg object.错误来自调试节点(如果您将鼠标悬停在侧栏中的错误上,它将显示它来自哪个节点),因为您返回的是原始值而不是msg对象。

Your function is returning just a number, this will not work.您的函数只返回一个数字,这是行不通的。

The function should read:该函数应为:

msg.payload = msg.payload.data.[0].value;
return msg;

The docs about writing functions nodes can be found here可以在此处找到有关编写函数节点的文档

The flow you have given shows that you are sending a text in the inject node instead of a JSON object.您提供的流程表明您正在注入节点中发送文本而不是 JSON 对象。

Image of injecting node注入节点的图像

This should be changed to the JSON option.这应该更改为 JSON 选项。 Alternatively, you could use the JSON.parse() method in your function node.或者,您可以在函数节点中使用JSON.parse()方法。

Node red nodes communicate through JSON objects, therefore the output of your function also needs to be a JSON object.节点红色节点通过 JSON 对象进行通信,因此您的函数的输出也需要是 JSON 对象。

return {"value" : msg.payload.data[0].value}

Alternatively if your data list is dynamic in length you can use或者,如果您的数据列表的长度是动态的,您可以使用

return {"values" : msg.payload.data.map(object => object.value)}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM