7

I am trying to form an array from a string using Modified Java Script Value step. Here is my code to parse a string and to form a JSON object.

var info = {};
var keywords = 'Adjust course (C-6),Identify underlying factors (C-4),Isolate teacher actions (C-3_)';
if(keywords != null && keywords != ''){
keywords = keywords.replace(/,/g,'","');
keywords = '["'+keywords+'"]';
info.keywords = JSON.parse(keywords);
}

Here in JSON.parse() it throws an error SyntaxError: Missing comma in array literal.

Can anyone please help me parse the array and store in json object.

Thanks in advance!

Arunraj
  • 558
  • 5
  • 21

3 Answers3

1

Try this:

function kwInfo(text)
{
    return JSON.parse('["' + (text || '').split(',').join('","') + '"]');
}

var text = 'Adjust course (C-6),Identify underlying factors (C-4),Isolate teacher actions (C-3_)';
var info = {keywords:kwInfo(text)};

console.log(info);
  • Make sure you're using a (respectable) modern (updated) web browser, like: Chrome, Firefox, Opera, Safari, etc. –  Apr 12 '16 at 06:55
  • this is not regular javascript, and pentaho is not a browser. its the rhino js engine isolated – jacktrade Apr 12 '16 at 10:39
1

Try this one

if(keywords){
  keywords = keywords.split(',');
  info.keywords = keywords;
}
Gugan Abu
  • 546
  • 1
  • 4
  • 17
0

Run kettle in console mode SpoonConsole.bat

var info = {};
var keywords = 'Adjust course (C-6),Identify underlying factors(C-4),Isolate 
teacher actions (C-3_)';

java.lang.System.out.println("Original : " + keywords);

if(keywords != null && keywords != ''){
   keywords = keywords.replace(/,/g,'","');
   java.lang.System.out.println("Regexp applied : " + keywords);
   keywords = '["'+keywords+'"]';
   java.lang.System.out.println(keywords);
   info.keywords = JSON.parse(keywords);
}

Look into console and trace the error in logic

This is only way I found to trace JavaScript step

simar
  • 1,782
  • 3
  • 16
  • 33