JSON.parse() not working in javascript in pentaho
Asked Answered
A

3

8

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!

Arnone answered 12/4, 2016 at 6:38 Comment(5)
I executed your code in the Chrome dev console and it works fine. Also in JavaScript you can compact the if clause like so: if(keywords){...}Upanddown
You also can use eval function, but be careful!Ruthi
@Arnone Please show what should you want to get after this code, I think that have more correct solution for youRuthi
Can you console.log(keywords) before parsing in your environment and tell what is it?Tades
java.lang.System.out.println(keywords);Steelworks
E
2

Try this one

if(keywords){
  keywords = keywords.split(',');
  info.keywords = keywords;
}
Entree answered 12/4, 2016 at 7:8 Comment(0)
L
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);
Lachlan answered 12/4, 2016 at 6:50 Comment(2)
Make sure you're using a (respectable) modern (updated) web browser, like: Chrome, Firefox, Opera, Safari, etc.Lachlan
this is not regular javascript, and pentaho is not a browser. its the rhino js engine isolatedTimberwork
S
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

Steelworks answered 12/4, 2016 at 13:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.