Kettle PDI - Modified JavaScript - Json function not available

2.1k Views Asked by At

I'm using Kettle PDI 6.0 running on Windows Server 2012. I need to use the Modified Java Script Value to handle on Json object. I try something like this:

var jsondata = JSON.parse(result);

And get that:

"TypeError: Cannot find function parse in object test value test value test value test value test value test value test value test value test value test value. (script#3)"

I already try to looking for a solution on google, but not looks like that. I think that can be something wrong with my installation.

Note: I already try to use the command:

import java.util.*;

But that command is not recognized (Is not marked in bold).

I get:

missing ; before statement (script#2)

Maybe the Java functions not available.

2

There are 2 best solutions below

0
Mauro Ferraz On

I made my own function to resolve the problem. I will post here to help who has the same problem. If anyone want to help to solve the initial problem, I am still interested.

You can paste the code bellow on your "Modified Java Script Value" step after receive the Json response from service or get that on file. Note that you need to change the name of variables that you want to find on Json.

Result field is a Json Value.

    //Script here

    function findInArray(myValue, myArray){
        var myResult='';
        if(myArray.indexOf(myValue) > -1){
            myResult = true;
        } else {
            myResult = false;
        }
        return myResult;
    }


    function getAttributeValue(Atribute, Object)
    {

       start = indexOf(Object,Atribute);

       for (i= start; i < Object.length; i++)
       {
          if (substr(Object,i,1) == ":")
          {
             start_value = i+1;
             break;
          }
       }

       for (i= start_value; i < Object.length; i++)
       {

          end_value = i;

          if (substr(Object,i,1) == ",")
          {         
             break;
          }
       }


       AttributeValue = replace(substr(Object, start_value, end_value-start_value),'"','');   


       if (indexOf(AttributeValue, "null") >= 0)
       {
          AttributeValue = null;      
       } 

       return AttributeValue ;  

    }

    // Recupera Status
    if (findInArray("status",result))
    {

       var status = getAttributeValue("status", result);  

    }
    else
    {
       var status = "";
    }

    // Recupera _ID
    if (findInArray("_id",result))
    {

       var mandrill_id = getAttributeValue("_id", result);  

    }
    else
    {
       var mandrill_id = "";
    }


    // Recupera reject_reason
    if (findInArray("reject_reason",result))
    {

       var reject_reason = replace(getAttributeValue("reject_reason", result),"}","");  

    }
    else
    {
       var reject_reason = "";
    }
0
jacktrade On

yes, the parse json function is not available on the ex4 ecmascript of js rhino engine build in kettle, but you can handle json in kettle using eval.

var resultObj = eval('('+result+')');

//now you can iterate the foo elements of result original json
for(i=0;i< resultObj.length;i++){
  Alert('foo number ' + i ' value = ' + resultObj[i].foo);
}

This is not javascript for the browser so eval is perfectly safe.