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 ma in array literal.
Can anyone please help me parse the array and store in json object.
Thanks in advance!
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 ma in array literal.
Can anyone please help me parse the array and store in json object.
Thanks in advance!
Share Improve this question asked Apr 12, 2016 at 6:38 ArunrajArunraj 5685 silver badges22 bronze badges 5- I executed your code in the Chrome dev console and it works fine. Also in JavaScript you can pact the if clause like so: if(keywords){...} – fikkatra Commented Apr 12, 2016 at 6:45
-
1
You also can use
eval
function, but be careful! – savelichalex Commented Apr 12, 2016 at 6:46 - @Arunraj Please show what should you want to get after this code, I think that have more correct solution for you – savelichalex Commented Apr 12, 2016 at 6:50
-
Can you
console.log(keywords)
before parsing in your environment and tell what is it? – Rajaprabhu Aravindasamy Commented Apr 12, 2016 at 7:01 - java.lang.System.out.println(keywords); – simar Commented Apr 12, 2016 at 13:20
3 Answers
Reset to default 2Try this one
if(keywords){
keywords = keywords.split(',');
info.keywords = keywords;
}
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);
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