如何将数据进行解包重新发布

{
“did”: “FH7080738470”,
“utime”: “1970/01/28 19:59:54”,
“content”: [
{
“pid”: “000000000015”,
“type”: “0”,
“addr”: “ZYG15”,
“addrv”: “3434.889893”,
“ctime”: “1970/01/28 19:59:54”
},
{
“pid”: “000000000015”,
“type”: “0”,
“addr”: “ZWG15”,
“addrv”: “906.789978”,
“ctime”: “1970/01/28 19:59:54”
},
{
“pid”: “000000000017”,
“type”: “0”,
“addr”: “ZYG17”,
“addrv”: “4432.649902”,
“ctime”: “1970/01/28 19:59:54”
},
{
“pid”: “000000000017”,
“type”: “0”,
“addr”: “ZWG17”,
“addrv”: “1261.209961”,
“ctime”: “1970/01/28 19:59:54”
},
{
“pid”: “000000000007”,
“type”: “0”,
“addr”: “ZYG07”,
“addrv”: “10434.040039”,
“ctime”: “1970/01/28 19:59:54”
},
{
“pid”: “000000000007”,
“type”: “0”,
“addr”: “ZWG07”,
“addrv”: “2236.760010”,
“ctime”: “1970/01/28 19:59:54”
},
{
“pid”: “000000000008”,
“type”: “0”,
“addr”: “ZYG08”,
“addrv”: “3347.409912”,
“ctime”: “1970/01/28 19:59:54”
},
{
“pid”: “000000000008”,
“type”: “0”,
“addr”: “ZWG08”,
“addrv”: “965.380005”,
“ctime”: “1970/01/28 19:59:54”
},
{
“pid”: “000000000095”,
“type”: “0”,
“addr”: “ZYG95”,
“addrv”: “24062.380859”,
“ctime”: “1970/01/28 19:59:54”
},
{
“pid”: “000000000095”,
“type”: “0”,
“addr”: “ZWG95”,
“addrv”: “330.660004”,
“ctime”: “1970/01/28 19:59:54”
}
]
}

现网关发布消息如上面所示,一个pid为一个设备,每个网关所带设备数不确定,希望将数据进行解包重新发布,解包为以下格式:
{
“pid”: “000000000095”,
“type”: “0”,
“addr”: “ZWG95”,
“addrv”: “330.660004”,
“ctime”: “1970/01/28 19:59:54”
}
请问EMQX是否可以完成此任务呢,需要如何操作?

使用 FOREACH 语句可以对一个数组执行多次动作:

FOREACH
  payload.content as elem
DO
  elem as val
FROM "t/#"

你的 “republish” 动作里面,配置 ${val} 作为 payload 模板即可。

详见:


达到效果,我检查一下有没遗漏,十分感谢!


能否做到将这个唯一的did加入到解包的每一条中呢

你可以阅读一下上面发你的文档里,关于 FOREACH 语句的用法。你需要在 DO 里构造你的每一条消息的字段,加上 did 字段并塞进 val 这个 Map 类型中:

FOREACH
  payload.content as elem
DO
  elem as val,
  payload.did as val.did
FROM "t/#"

明白了,感谢!