I have a custom record that is scripted (by a cons...
# suitescript
b
I have a custom record that is scripted (by a consultant) and I keep getting a "Syntax Error: Expected end of stream" error. I can't see any obvious missing commas or unterminated strings so I don't know what could be causing this. It's only happening when the array it's trying to parse has more than one object. Working example is [{"prop1":"abc", "prop2":100, "prop3":"", "prop4":0}]. Not working example is [{"prop1":"abc", "prop2":100, "prop3":"", "prop4":0}, {"prop1":"def", "prop2":150, "prop3":"", "prop4":0}, {"prop1":"ghi", "prop2":50, "prop3":"", "prop4":0}] Any thoughts about why it's unhappy with the second example? I'm new-ish to JSON
s
are you sure this is a JSON issue? if so, and this is happening as you try and parse the data above, ensure there's no unexpected trailing whitespace or other invisible characters after the trailing ]
b
No, I'm not sure this is a JSON issue to be honest! I only thought it was because the consultant is storing the data that is being passed in the array in a custom record that gives the status of the scheduled script that is then saving it to the transaction record. So the status field is where this error is getting logged and it's showing the array that I mentioned earlier.
s
I think pasting more of the code here would be helpful
b
@stalbert var stGrantDetails = arrResult[i].getValue('custrecord_param_grantdetails'); var arrGrantDetails = stGrantDetails.replace('[', '').replace(']', '').replace(',{', '|{').split('|'); arrResult is getting set in a separate script that is returning an array of nlobjSearchResult objects Is this enough code? Sorry - I'm new to using slack and also new to SuiteScript. Thanks for your help!
j
Could it be this bug? https://bugzilla.mozilla.org/show_bug.cgi?id=513549 NS has not updated their javascript engine for a long time. Try calling jsonstring.trim() before passing it to JSON.parse()
Copy code
var jsonstring = 'your string';
JSON.parse(jsonstring.trim());
s
it reads like your custrecord_param_grantdetails field is holding some random pipe-delimited sort of thing? Even so, I'd recommend you try jkabot's suggestion given the specific error message you're seeing.
b
@jkabot @stalbert I tried this and it didn't work but it lead me to at least pinpointing what is happening! I just am not sure how to fix it. I have this array, stGrantDetails, that then gets some replacing/splitting methods called on it, but it isn't doing it globally. stGrantDetails = [{"lineid":"2","grant1":"871","amount1":14.36,"grant2":"","amount2":0,"grant3":"","amount3":0},{"lineid":"29","grant1":"871","amount1":143.61,"grant2":"","amount2":0,"grant3":"","amount3":0},{"lineid":"30","grant1":"871","amount1":-143.61,"grant2":"","amount2":0,"grant3":"","amount3":0}] var arrGrantDetails = stGrantDetails.replace('[', '').replace(']', '').replace(',{', '|{').split('|'); There is then a for loop that is supposed to print out the length to the console and then call JSON.parse() on each array element. it should be printing out length = 3 but it's getting the first one correct and then thinking the last two are considered one because it's missing the '|{'. All of this to ask: how would you properly do a global replace to replace ',{' with '|{'?
j
Copy code
.replace(/,{/g,'|{')
b
@jarens thanks - I actually had to add a couple of escape characters and got it to work with replace(/\,\{/g, '|{').
🎉 1
s
Correct me if I'm wrong, but does
custrecord_param_grantdetails
just hold a stringified JSON array of objects? Depending upon what you are trying to extract, there may be better ways to split it.
✔️ 1
if the solution above works for you, though, then no need to look further
☝️ 1
j
@scottvonduhn, I was gonna mention that the code definitely needs a refactor but let's not forget that all code is some degree of awful so what works is what is initially best. lol 🤣
☝️ 1
👎 1