-1

Someone have any examples of how to treat return Json (url) using Modified Java Script Value? Follow data the I need from url json:

{
"result": {
"data": [
{
"name": "page1",
"period": "dia",
"values": [
{
"value": 4,
"end_time": "2016-03-25"
},
{
"value": 2,
"end_time": "2016-03-26"
},
{
"value": 0,
"end_time": "2016-03-27"
}
],

},
{
"name": "page2",
"period": "dia",
"values": [
{
"value": 22,
"end_time": "2016-03-25"
},
{
"value": 30,
"end_time": "2016-03-26"
},
{
"value": 88,
"end_time": "2016-03-27"
}
],

} 
I need create rows this data. Regards, Santana, Marcos

2 Answers2

0

Thanks for help, You right, not need js, only json. But I have problem. Getting catch the names, but value and dates do not. I'm using: $ .. Date []. Name $ .. Date []. Values (the problem is here, it loads the three values (value, end_time) and need only one. I tried $ .value it gives error and then shows that it has more than one value.

I got it, I used a step select value, split fields and Row Normaliser.

Thanks so much! Santana, Marcos

-1

Depending on the data you want in the rows this could be achieve better and faster with JSON Input. For instance if you write $..value and $..end_time in the Fields it creates the rows.

If you want to do it in JavaScript (but here is no reason why this cannot be done in JSON Input) here is a sample script to get you started:

var obj = JSON.parse(json);
datas = obj.result.data;
for (var i = 0; i < datas.length; i++) {
  var data = datas[i];
  for (var ii = 0; ii < data.values.length; ii++) {
    var value = data.values[ii];
    var row = createRowCopy(getOutputRowMeta().size());
    var idx = getInputRowMeta().size();
    row[idx++] = value.value;
    row[idx++] = value.end_time;
    putRow(row);
  }
}
trans_Status = SKIP_TRANSFORMATION;

Remember to add the two values you add to the row to the output fields of the step. value - string and end_time - string.

bolav
  • 6,938
  • 2
  • 18
  • 42