【JavaScript】MQTTX-脚本-自定义函数-payload json格式化

/**
* @description: default script
* @param {any} value - Payload
* @param {string} msgType - Message type, value is 'received' or 'publish'
* @param {number} index - Index of the message, valid only when script is used in the publish message and timed message is enabled
* @return {any} - Payload after script processing
*/
function handlePayload(value, msgType, index) {
    
    
  const jsonObj = JSON.parse(value)
  return JSON.stringify(jsonObj, null, 2);
}
execute(handlePayload)

输入

{
    
    "token":"123","timestamp":"2023-10-24T13:33:35.746+0800","body":[{
    
    "name":"UA","val":"0.0"},{
    
    "name":"UB","val":"0.0"}]}

输出

{
    
    
  "token": "123",
  "timestamp": "2023-10-24T13:33:35.746+0800",
  "body": [
    {
    
    
      "name": "UA",
      "val": "0.0"
    },
    {
    
    
      "name": "UB",
      "val": "0.0"
    }
  ]
}

猜你喜欢

转载自blog.csdn.net/qq_43577613/article/details/134028085
今日推荐