将 PDI 响应转换为确定的结构



我是PDI的新手,我正在努力从API恢复信息,我需要转换到某种结构的信息,我不清楚如何使用串联或其他转换来做到这一点。

这是我对以下工作的回答:

[
  {
    "457": {
      "1": {
        "value": "4.1",
        "timestamp": 1534159593
      },
      "2": {
        "value": "52.2",
        "timestamp": 1534159593
      },
      "3": {
        "value": "23.0",
        "timestamp": 1534159593
      },
      "4": {
        "value": "250.0",
        "timestamp": 1534159593
      }
    }
  }
]

我需要保留这种类型的东西,通过 POST 将其发送到另一个 API

{
  "id": "457",
  "type": "greenhouse",
  "1": {
    "value": 4.1,
    "type": "Float",
    "timestamp": 1534159593
  },
  "2": {
    "value": 52.2,
    "type": "Integer",
    "timestamp": 1534159593
  },
  "3": {
    "value": 23.0,
    "type": "Integer",
    "timestamp": 1534159593
  },
  "4": {
    "value": 250.0,
    "type": "Integer",
    "timestamp": 1534159593
  }
}

感谢您的帮助。

编辑01

霍拉又,

我正在按照你告诉我的去做,我有一个问题。

这是我的代码:

// Script here
var data = data2;
var tests = data;

var tests3 = {"457": {"2": {"value": "54.0", "timestamp": 1534246741}, "3": {"value": "22.2", "timestamp": 1534246741}, " 4 ": {" value ":" 260.0 "," timestamp ": 1534246741}," 21 ": {" value ":" 890.0 "," timestamp ": 1534246741}," 1 ": {" value ":" 4.13 "," timestamp ": 1534246741}," 17 ": {" value ":" 194.04687499999997 "," timestamp ": 1534246741}," 5 ": {" value ":" 35.417 "," timestamp ": 1534246741}," 6 ": {" value ":" 26.299999999999997 "," timestamp ": 1534246741}," 8 ": {" value ":" 4.7 "," timestamp ": 1534246741}," 15 ": {" value ":" 0.78 "," timestamp ": 1534246741}," 10 ": {" value ":" 24.94 "," timestamp ": 1534246741}," 22 ": {" value ":" 0.0 "," timestamp ": 1534246741}," 23 ": {" value ":" 0.0 "," timestamp ": 1534246741}," 24 ": {" value ":" 0.0 "," timestamp ": 1534246741}," 26 ": {" value ":" 0.0 "," timestamp ": 1534246741}," 653 ": {" value ":" 0.0 "," timestamp ": 1534246741}," 657 ": {" value ":" - 98.0 "," timestamp ": 1518420299}, "43": {"value": "11.892947103200001", "timestamp": 1534246741}, "42": {"value": "403.61749999999995", "timestamp": 1534246741}}};
var key = Object.keys (data) [0];
var finalobj = {};
for (var and in data [key]) {
    finalobj [e] = {
        type: "float"
        , value: parseFloat (data [key] [e] .value)
        , metadata: {
            timestamp: {
                value: parseInt (data [key] [e] .timestamp)
                , type: "Integer"
            }
        }
    };
}
 var JsonOutput = JSON.stringify (finalobj);

变量 data2 是具有我的 JSON 的变量,它实际上与 tests3 的信息相同,如果代码有效,但如果我通过 tests3 逐步放置数据,我不明白,因为数据具有相同的值并且必须工作并且 JsonOutput 的响应是 {},但如果它正常工作,我会使用 tests3 来做。

看起来是在检索变量时,但后来我显示它有数据和 data2,并且它与 tests3 的信息相同,我不明白会发生什么。

你可以帮我吗?

现在没有一个内置的步骤来编写Pentaho中的嵌套JSON,你必须使用JavaScript来实现它,这里有一个非常棒的帖子,我用它来作为构建我自己的流程的指南。

相关内容

  • 没有找到相关文章

最新更新