JScript - JSON.parse() not supported

peterchan75

Supremacy Member
Joined
Apr 26, 2003
Messages
6,719
Reaction score
529
Hi,
I trying the parse this JSON using Jscript JSON.parse but this method is not support. Some suggest using eval(). I tried but couldn't get it to work. Any advice is much appreciated.
Code:
var json = [{"date":"2021-06-04T00:00:00.000Z","close":125.89,"high":126.16,"low":123.85,"open":124.07,"volume":75169343,"adjClose":125.89,"adjHigh":126.16,"adjLow":123.85,"adjOpen":124.07,"adjVolume":75169343,"divCash":0.0,"splitFactor":1.0},{"date":"2021-06-07T00:00:00.000Z","close":125.9,"high":126.32,"low":124.8321,"open":126.17,"volume":71057550,"adjClose":125.9,"adjHigh":126.32,"adjLow":124.8321,"adjOpen":126.17,"adjVolume":71057550,"divCash":0.0,"splitFactor":1.0}];
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,301
Hi,
I trying the parse this JSON using Jscript JSON.parse but this method is not support. Some suggest using eval(). I tried but couldn't get it to work. Any advice is much appreciated.
Code:
var json = [{"date":"2021-06-04T00:00:00.000Z","close":125.89,"high":126.16,"low":123.85,"open":124.07,"volume":75169343,"adjClose":125.89,"adjHigh":126.16,"adjLow":123.85,"adjOpen":124.07,"adjVolume":75169343,"divCash":0.0,"splitFactor":1.0},{"date":"2021-06-07T00:00:00.000Z","close":125.9,"high":126.32,"low":124.8321,"open":126.17,"volume":71057550,"adjClose":125.9,"adjHigh":126.32,"adjLow":124.8321,"adjOpen":126.17,"adjVolume":71057550,"divCash":0.0,"splitFactor":1.0}];
Read the code properly. Is it a String, or an Object ?

Why do you need to parse a javascript syntax when it is already parsed by the Javascript engine ? :)

FpfmfrE.png
 

peterchan75

Supremacy Member
Joined
Apr 26, 2003
Messages
6,719
Reaction score
529
It's a string. Rather than using regex, using JSON.parse to put into is straight foward.
var myarray = JSON.parse(json);
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,301
Load this code pen demo in a separate tab, open the console and run to see what I'm doing.

 

peterchan75

Supremacy Member
Joined
Apr 26, 2003
Messages
6,719
Reaction score
529
If it is a string where are ether single or double quotes?
David,

Thank you very much for your time. These are stock data from Tiingo API call that I used to test the JScript code. I run into the problem of JSON.parse not support in WSH JScript. Your question on single or double quotes in the json help fixed the problem. There should be quotes on both ends. I google and found the workaround using html COM object.

Code:
var htmlfile = WSH.CreateObject('htmlfile'), JSON;
htmlfile.write('<meta http-equiv="x-ua-compatible" content="IE=9" />');
htmlfile.close(JSON = htmlfile.parentWindow.JSON);

Thanks again.
 
Important Forum Advisory Note
This forum is moderated by volunteer moderators who will react only to members' feedback on posts. Moderators are not employees or representatives of HWZ Forums. Forum members and moderators are responsible for their own posts. Please refer to our Community Guidelines and Standards and Terms and Conditions for more information.
Top