Manolait
Manolait

Reputation: 352

Transform PDI response to determined structure

I'm new to PDI I'm working recovering information from an API and I need to transform the information that comes to a certain structure and I'm not clear how to do it with the concatenation or with another transformation.

This is my answer to work with:

[
  {
    "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
      }
    }
  }
]

and I would need something of this type to remain, to send it by POST to another 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
  }
}

Thanks for the help.

Edit01

Holla again,

I'm doing it as you told me and I have a problem.

This is my code:

// 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);

The variable data2 is the one that has my JSON is really the same information that tests3, the code if it works but if I step puts puts data by tests3, which I do not understand since data has the same value and would have to work and the response of JsonOutput is {} but I do it with tests3 if it works correctly.

It looks like it is at the time of retrieving the variable but then I show that it has data and data2 and it is the same information that tests3, I do not understand what happens.

can you help me?

Upvotes: 0

Views: 106

Answers (1)

Cristian Curti
Cristian Curti

Reputation: 1054

Right now there isn't a built in step for writing nested JSON in Pentaho, you have to use JavaScript to achieve it, there is a really great post here that i'm using as guide to build my own process.

Upvotes: 2

Related Questions