将 PDI 响应转换为确定的结构
Transform PDI response to determined structure
我是 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
}
}
感谢您的帮助。
Edit01
再次呼啦,
我正在按照你告诉我的去做,但我遇到了问题。
这是我的代码:
// 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 来做。
好像是在取变量的时候,但是后来我显示里面有data和data2,和tests3的信息是一样的,我不明白发生了什么。
你能帮帮我吗?
目前在 Pentaho 中没有用于编写嵌套 JSON 的内置步骤,您必须使用 JavaScript 来实现它,有一个非常棒的 post 我将其用作构建我自己的流程的指南。
我是 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
}
}
感谢您的帮助。
Edit01
再次呼啦,
我正在按照你告诉我的去做,但我遇到了问题。
这是我的代码:
// 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 来做。
好像是在取变量的时候,但是后来我显示里面有data和data2,和tests3的信息是一样的,我不明白发生了什么。
你能帮帮我吗?
目前在 Pentaho 中没有用于编写嵌套 JSON 的内置步骤,您必须使用 JavaScript 来实现它,有一个非常棒的 post