problem in parsing json in javascript and access value by key - javascript

I'm receiving some error response in ajax error function;
$.ajax(Request).error(function (Response) {
var content = JSON.stringify(Response.responseText);
var obj = JSON.parse(content);
console.log("Response.responseText:",Response.responseText);
console.log("Response.responseText.Message:", Response.responseText.Message);
console.log("Response.responseText[Message]:", Response.responseText["Message"]);
console.log("content:", content);
console.log("content.Message:", content.Message);
console.log("content[Message]:", content["Message"]);
console.log("obj:", obj);
console.log("obj.message:", obj.Message);
console.log("obj[Message]:", obj["Message"]);
});
Here are outputs of console.log :
I need to access Message but none of them create access to its value. How can I access Message?
Main Question
why all of them are undefined?
How can I access Message?
Solution
var parsed= JSON.parse(startpaymentResponse.responseText);
console.log("parsed:", parsed);
console.log("parsed.Message:", parsed.Message);
console.log("parsed[Message]:", parsed["Message"]);
Meanwhile when I copy Response.responseText to JSON Deserialize Online, everything is fine.
Response.responseText: {"Message":"The request is invalid.","ModelState":{"model.Amount":["The field Amount must be a string or array type with a minimum length of '4'."],"model.TotalAmount":["The field TotalAmount must be a string or array type with a minimum length of '4'."]}}

Okay, so the problem is that Response.responseText is already a string. You don't need to do JSON.stringify(Response.responseText). Please directly, parse it to JSON and then use the object:
$.ajax(Request).error(function (Response) {
var obj = JSON.parse(Response.responseText);
console.log("obj.Message", obj.Message);
});

Related

How can I most easily parse this HTTP response into JSON to then access responseText fields?

I'm trying to parse the plaintext that's returned from this URL -- https://www.cloudflare.com/cdn-cgi/trace -- in order to read the IP address value. This is the code snippet I'm using for the request:
ipAddress = $.get('https://www.cloudflare.com/cdn-cgi/trace', function(data) {
return data;
});
When logged in browser console, the response looks like the following:
I've tried using JSON.stringify(), accessing responseText using ['responseText'], as well as .responseText.. but no luck.
How can this response be parsed so that I can most easily access the ip value from within responseText?
$.get("https://www.cloudflare.com/cdn-cgi/trace", function(data) {
const [ ,ip ] = data.match(new RegExp("ip=" + "(.*)" + "\n"));
console.log(ip);
});
Outputs
"2a01:cb18:362:2200:e90e:fb09:8445:6302"
You can either parse on carriage return or simply look for the ip= in the string or use a regex search it.
If you look at ipAddress.reponseText it's a carriage return delimited string.
so
var myip=ipAddress.responseText.split('\n');
console.log(myip[2]); 'the text ip=192.168.1.10 for example if it's always in the same position.
If it's not in the same position then look through the array to find it.
its return document as response, so i'm not pretty clear about JSON, here some way
const ipAddress = $.get('https://www.cloudflare.com/cdn-cgi/trace', function (data) {
const arr = data.split("\n")
const ip = arr.filter(v => v.includes("ip="))[0]
console.log(ip.split("=")[1])
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Extract Data Values from JSON ouput

I am new to JSON and concepts. I want to extract the data from an API which i have mentioned below, basically i want to extract several details from this API about an Stock. The problem here is, after parsing the URL i dont know how to extract the value for each variablle.
I want to extract the data into an GoogleSheet.
the output of the below function shows, like this
[20-12-10 20:45:15:806 CET] [{symbol=JMIA, price=37.0497, volume=1.317713E7}]
The output i wanted is that:
JMIA
37.0497
1.317713E7
Code :
function CurrentPrice() {
var url = 'https://financialmodelingprep.com/api/v3/quote-short/JMIA?
apikey=7c2f5bcb573b33050c1aad41a54919';
var response = UrlFetchApp.fetch(url);
// convert json string to json object
var jsonSignal = JSON.parse(response);
Logger.log(jsonSignal);
}
I suggest you read this "Working with Objects" article.
The response is wrapped in brackets [] meaning it's an array. I assume you're only expecting one response, so you can grab that first element using jsonSignal[0], which will give you an object.
To get an object property, you have to specify the property name using either dot- or bracket-notation. (I'll use dot-notation.)
const jsonSignal = [{symbol:'JMIA', price:37.0497, volume:1.317713E7}];
console.log(jsonSignal[0].symbol); // 'JMIA'
function CurrentPrice() {
const url = 'https://financialmodelingprep.com/api/v3/quote-short/JMIA?apikey=API_KEY';
const response = UrlFetchApp.fetch(url);
// Convert HTTPResponse
// Use the first element in the response array
const signal = JSON.parse(response)[0];
console.log(signal.symbol); // 'JMIA'
console.log(signal.price); // 37.0497
console.log(signal.volume); // 1.317713E7
}
Try like this :
var json = response.getContentText();
var data = JSON.parse(json);
Logger.log(data);
I read that there : https://developers.google.com/apps-script/guides/services/external
Regards

Accessing an element of my json response in javascript

I have a response with two jsons, exactly like this -
{
"redirectUrl": "http:\/\/lumoslocal.heymath.com"
},
{
"status": "SUCCESS"
}
I need to redirect on getting the response to the redirectUrl. Something like window.location.href = response.redirectUrl. But it's not working. Possibly because of two json in my response. How do I use the 'redirectUrl' of my first json?
My understanding (from the OP's comments) is that the response is coming back as a string like this: authResp = '{"redirectUrl":"http:\/\/lumoslocal.heymath.com"}, {"status":"SUCCESS"}'
Technically this is not valid JSON as one big chunk, you'll get an error (test it out below)
JSON.parse('{"redirectUrl":"http:\/\/lumoslocal.heymath.com"}, {"status":"SUCCESS"}')
To successfully parse the data (and ultimately get the redirectUrl data), follow these steps:
split the string with a comma "," character
parse the "first JSON element"
redirect to extracted redirectUrl
Here's the code for each step:
authResp = '{"redirectUrl":"http:\/\/lumoslocal.heymath.com"}, {"status":"SUCCESS"}';
// 1. split the string with a comma character:
let authArr = authResp.split(',');
// 2. parse the first JSON element:
let redirectObj = JSON.parse(authArr[0]);
// 3. redirect to extracted redirectUrl
window.location.href = redirectObj.redirectUrl;
Or, if you want to parse the entire string into an array of JSON objects you can do this:
authResp = '{"redirectUrl":"http:\/\/lumoslocal.heymath.com"}, {"status":"SUCCESS"}';
// create array of json strings, then parse each into separate array elements
authArr = authResp.split(',').map(e => JSON.parse(e));
// Finally, follow #JackBashford's code:
window.location.href = authArr.find(e => e.redirectUrl).redirectUrl;
If your two responses are in an array, it's simple, even if they're unordered:
var myJSON = [{"redirectUrl": "http:\/\/lumoslocal.heymath.com"}, {"status": "SUCCESS"}];
window.location.href = myJSON.find(e => e.redirectURL).redirectURL;

why is javascript object "undefined"?

I have JSON coming from ajax into javascript. When I try to reference two properties of the object, it says they are undefined. Why?
data="{\"headers\":[{\"Name\":\"Margin-G\",\"Description\":\"Interactive Brokers Margin Account\",\"Value\":\"82632\",\"GrowthRate\":\"5\",\"PayoutRate\":\"4\",\"Contribution\":\"0\",\"TableName\":\"Account\"},{\"Name\":\"IRA-G\",\"Description\":\"Interactive Brokers IRA\",\"Value\":\"272607\",\"GrowthRate\":\"5\",\"PayoutRate\":\"4\",\"Contribution\":\"6500\",\"TableName\":\"Account\"},{\"Name\":\"Roth-G\",\"Description\":\"Vanguard Roth IRA\",\"Value\":\"69149\",\"GrowthRate\":\"5\",\"PayoutRate\":\"4\",\"Contribution\":\"0\",\"TableName\":\"Account\"},{\"Name\":\"DRIP-A\",\"Description\":\"Dividend Reinvestment stocks\",\"Value\":\"3825\",\"GrowthRate\":\"5\",\"PayoutRate\":\"4\",\"Contribution\":\"0\",\"TableName\":\"Account\"},{\"Name\":\"Roth 401K\",\"Description\":\"Everbank Foreign Currency\",\"Value\":\"17246\",\"GrowthRate\":\"5\",\"PayoutRate\":\"4\",\"Contribution\":\"0\",\"TableName\":\"Account\"},{\"Name\":\"Env 401K\",\"Description\":\"Enventive 401K\",\"Value\":\"22434\",\"GrowthRate\":\"5\",\"PayoutRate\":\"4\",\"Contribution\":\"24000\",\"TableName\":\"Account\"},{\"Name\":\"IRA-A\",\"Description\":\"IRA-A\",\"Value\":\"90094\",\"GrowthRate\":\"5\",\"PayoutRate\":\"4\",\"Contribution\":\"0\",\"TableName\":\"Account\"},{\"Name\":\"ULA 401K\",\"Description\":\"ULA 401K\",\"Value\":\"219421\",\"GrowthRate\":\"5\",\"PayoutRate\":\"4\",\"Contribution\":\"0\",\"TableName\":\"Account\"},{\"Name\":\"Roth-A\",\"Description\":\"Roth-Annette\",\"Value\":\"14658\",\"GrowthRate\":\"5\",\"PayoutRate\":\"4\",\"Contribution\":\"0\",\"TableName\":\"Account\"},{\"Name\":\"Pera 401K\",\"Description\":\"Pera 401K\",\"Value\":\"28448\",\"GrowthRate\":\"5\",\"PayoutRate\":\"4\",\"Contribution\":\"0\",\"TableName\":\"Account\"},{\"Name\":\"Pera Roth 401K\",\"Description\":\"Pera Roth 401K\",\"Value\":\"4075\",\"GrowthRate\":\"5\",\"PayoutRate\":\"4\",\"Contribution\":\"0\",\"TableName\":\"Account\"},{\"Name\":\"Pera 457 Roth\",\"Description\":\"Pera 457 Roth\",\"Value\":\"7335\",\"GrowthRate\":\"5\",\"PayoutRate\":\"4\",\"Contribution\":\"0\",\"TableName\":\"Account\"},{\"Name\":\"SS-A\",\"Description\":\"Social Security Annette\",\"Value\":\"28416\",\"GrowthRate\":\"0\",\"PayoutRate\":\"0\",\"Contribution\":\"1\",\"TableName\":\"Income\"},{\"Name\":\"SS-G\",\"Description\":\"Social Security Greg\",\"Value\":\"21756\",\"GrowthRate\":\"1\",\"PayoutRate\":\"0\",\"Contribution\":\"1\",\"TableName\":\"Income\"}],\"years\":[[2018,82632,0,272607,0,69149,0,3825,0,17246,0,22434,0,90094,0,219421,0,14658,0,28448,0,4075,0,7335,0,28416,21756,50172],[2019,86764,0,292737,0,72606,0,4016,0,18108,0,47556,0,94599,0,230392,0,15391,0,29870,0,4279,0,7702,0,28416,21756,50172],[2020,91102,0,313874,0,76236,0,4217,0,19013,0,73934,0,99329,0,241912,0,16161,0,31364,0,4493,0,8087,0,28416,21756,50172],[2021,95657,0,336068,0,80048,0,4428,0,19964,0,101631,0,104295,0,254008,0,16969,0,32932,0,4718,0,8491,0,28416,21756,50172],[2022,100440,0,359371,0,84050,0,4649,0,20962,0,130713,0,109510,0,266708,0,17817,0,34579,0,4954,0,8916,0,28416,21756,50172],[2023,105462,0,383840,0,88253,0,4881,0,22010,0,161249,0,114986,0,280043,0,18708,0,36308,0,5202,0,9362,0,28416,21756,50172],[2024,110735,4429,403032,16121,92666,3707,5125,205,23111,924,169311,6772,120735,4829,294045,11762,19643,786,38123,1525,5462,218,9830,393,28416,21756,101845],[2025,111843,4474,407063,16283,93592,3744,5176,207,23343,934,171005,6840,121943,4878,296985,11879,19839,794,38504,1540,5517,221,9929,397,28416,21756,102362],[2026,112961,4518,411133,16445,94528,3781,5228,209,23576,943,172715,6909,123162,4926,299955,11998,20037,801,38889,1556,5572,223,10028,401,28416,21756,102883],[2027,114091,4564,415245,16610,95473,3819,5280,211,23812,952,174442,6978,124394,4976,302955,12118,20238,810,39277,1571,5628,225,10128,405,28416,21756,103411],[2028,115232,4609,419397,16776,96428,3857,5333,213,24051,962,176186,7047,125638,5026,305985,12239,20440,818,39670,1587,5684,227,10229,409,28416,21756,103943],[2029,116385,4655,423591,16944,97392,3896,5387,215,24292,972,177948,7118,126894,5076,309045,12362,20644,826,40067,1603,5741,230,10331,413,28416,21756,104481],[2030,117549,4702,427827,17113,98366,3935,5441,218,24535,981,179727,7189,128163,5127,312135,12485,20850,834,40467,1619,5798,232,10435,417,28416,21756,105024],[2031,118724,4749,432105,17284,99349,3974,5495,220,24781,991,181524,7261,129444,5178,315257,12610,21059,842,40871,1635,5856,234,10540,422,28416,21756,105572],[2032,119911,4796,436426,17457,100342,4014,5550,222,25029,1001,183339,7334,130738,5230,318410,12736,21270,851,41280,1651,5915,237,10645,426,28416,21756,106126],[2033,121111,4844,440790,17632,101345,4054,5606,224,25279,1011,185172,7407,132045,5282,321595,12864,21483,859,41693,1668,5974,239,10751,430,28416,21756,106686],[2034,122323,4893,445198,17808,102358,4094,5662,226,25532,1021,187024,7481,133365,5335,324811,12992,21698,868,42110,1684,6034,241,10859,434,28416,21756,107251],[2035,123546,4942,449650,17986,103382,4135,5719,229,25788,1032,188894,7556,134698,5388,328060,13122,21915,877,42532,1701,6095,244,10968,439,28416,21756,107822],[2036,124781,4991,454147,18166,104416,4177,5776,231,26045,1042,190783,7631,136045,5442,331341,13254,22134,885,42958,1718,6156,246,11077,443,28416,21756,108398],[2037,126029,5041,458688,18348,105460,4218,5834,233,26305,1052,192691,7708,137405,5496,334654,13386,22356,894,43388,1736,6218,249,11188,448,28416,21756,108981],[2038,127289,5092,463274,18531,106515,4261,5893,236,26568,1063,194618,7785,138779,5551,338001,13520,22580,903,43821,1753,6280,251,11299,452,28416,21756,109569],[2039,128561,5142,467907,18716,107580,4303,5952,238,26833,1073,196564,7863,140167,5607,341381,13655,22806,912,44259,1770,6343,254,11412,456,28416,21756,110163],[2040,129847,5194,472586,18903,108656,4346,6012,240,27102,1084,198529,7941,141568,5663,344795,13792,23034,921,44702,1788,6406,256,11527,461,28416,21756,110763],[2041,131145,5246,477312,19092,109743,4390,6073,243,27373,1095,200514,8021,142983,5719,348243,13930,23265,931,45149,1806,6470,259,11642,466,28416,21756,111369],[2042,132456,5298,482086,19283,110840,4434,6134,245,27647,1106,202519,8101,144413,5777,351725,14069,23497,940,45600,1824,6535,261,11758,470,28416,21756,111980],[2043,133781,5351,486907,19476,111948,4478,6196,248,27923,1117,204544,8182,145857,5834,355242,14210,23732,949,46056,1842,6601,264,11876,475,28416,21756,112599],[2044,135119,5405,491776,19671,113067,4523,6258,250,28202,1128,206589,8264,147316,5893,358794,14352,23970,959,46517,1861,6667,267,11995,480,28416,21756,113223],[2045,136470,5459,496694,19868,114197,4568,6321,253,28484,1139,208654,8346,148789,5952,362382,14495,24210,968,46982,1879,6733,269,12115,485,28416,21756,113853],[2046,137835,5513,501661,20066,115339,4614,6384,255,28769,1151,210741,8430,150276,6011,366006,14640,24453,978,47452,1898,6801,272,12236,489,28416,21756,114490],[2047,139214,5569,506678,20267,116492,4660,6448,258,29056,1162,212848,8514,151779,6071,369666,14787,24698,988,47927,1917,6869,275,12359,494,28416,21756,115133],[2048,140606,5624,511745,20470,117657,4706,6512,260,29347,1174,214976,8599,153297,6132,373362,14934,24945,998,48406,1936,6937,277,12483,499,28416,21756,115783],[2049,142012,5680,516862,20674,118834,4753,6578,263,29640,1186,217126,8685,154830,6193,377096,15084,25194,1008,48890,1956,7007,280,12608,504,28416,21756,116439],[2050,143433,5737,522031,20881,120023,4801,6644,266,29936,1197,219297,8772,156379,6255,380867,15235,25446,1018,49379,1975,7077,283,12734,509,28416,21756,117102],[2051,144868,5795,527252,21090,121223,4849,6710,268,30236,1209,221490,8860,157943,6318,384675,15387,25700,1028,49873,1995,7148,286,12862,514,28416,21756,117771],[2052,146316,5853,532525,21301,122435,4897,6778,271,30539,1222,223705,8948,159522,6381,388522,15541,25957,1038,50372,2015,7219,289,12991,520,28416,21756,118447],[2053,147779,5911,537850,21514,123660,4946,6846,274,30844,1234,225942,9038,161117,6445,392407,15696,26217,1049,50876,2035,7291,292,13121,525,28416,21756,119130],[2054,149257,5970,543229,21729,124897,4996,6914,277,31152,1246,228201,9128,162728,6509,396331,15853,26479,1059,51385,2055,7364,295,13252,530,28416,21756,119820],[2055,150750,6030,548661,21946,126146,5046,6983,279,31464,1259,230483,9219,164355,6574,400295,16012,26744,1070,51899,2076,7437,297,13385,535,28416,21756,120516],[2056,152258,6090,554148,22166,127407,5096,7053,282,31778,1271,232788,9312,165999,6640,404298,16172,27011,1080,52418,2097,7512,300,13519,541,28416,21756,121220],[2057,153781,6151,559689,22388,128681,5147,7124,285,32096,1284,235115,9405,167659,6706,408341,16334,27282,1091,52942,2118,7588,304,13654,546,28416,21756,121930]]}"
//console.log("back from getIncomeSchedule.php: data="+JSON.stringify(data));
data = JSON.parse(JSON.stringify(data));
console.log(data.headers);
console.log(data.years);
You don't parse the string of the data, you parse the actual data. Try this:
data = JSON.parse(data);
Instead of:
data = JSON.parse(JSON.stringify(data));
Then you'll be able to access the headers and all.
Change data = JSON.parse(JSON.stringify(data)); to data = JSON.parse(data);
data="{\"headers\":[{\"Name\":\"Margin-G\",\"Description\":\"Interactive Brokers Margin Account\",\"Value\":\"82632\",\"GrowthRate\":\"5\",\"PayoutRate\":\"4\",\"Contribution\":\"0\",\"TableName\":\"Account\"},{\"Name\":\"IRA-G\",\"Description\":\"Interactive Brokers IRA\",\"Value\":\"272607\",\"GrowthRate\":\"5\",\"PayoutRate\":\"4\",\"Contribution\":\"6500\",\"TableName\":\"Account\"},{\"Name\":\"Roth-G\",\"Description\":\"Vanguard Roth IRA\",\"Value\":\"69149\",\"GrowthRate\":\"5\",\"PayoutRate\":\"4\",\"Contribution\":\"0\",\"TableName\":\"Account\"},{\"Name\":\"DRIP-A\",\"Description\":\"Dividend Reinvestment stocks\",\"Value\":\"3825\",\"GrowthRate\":\"5\",\"PayoutRate\":\"4\",\"Contribution\":\"0\",\"TableName\":\"Account\"},{\"Name\":\"Roth 401K\",\"Description\":\"Everbank Foreign Currency\",\"Value\":\"17246\",\"GrowthRate\":\"5\",\"PayoutRate\":\"4\",\"Contribution\":\"0\",\"TableName\":\"Account\"},{\"Name\":\"Env 401K\",\"Description\":\"Enventive 401K\",\"Value\":\"22434\",\"GrowthRate\":\"5\",\"PayoutRate\":\"4\",\"Contribution\":\"24000\",\"TableName\":\"Account\"},{\"Name\":\"IRA-A\",\"Description\":\"IRA-A\",\"Value\":\"90094\",\"GrowthRate\":\"5\",\"PayoutRate\":\"4\",\"Contribution\":\"0\",\"TableName\":\"Account\"},{\"Name\":\"ULA 401K\",\"Description\":\"ULA 401K\",\"Value\":\"219421\",\"GrowthRate\":\"5\",\"PayoutRate\":\"4\",\"Contribution\":\"0\",\"TableName\":\"Account\"},{\"Name\":\"Roth-A\",\"Description\":\"Roth-Annette\",\"Value\":\"14658\",\"GrowthRate\":\"5\",\"PayoutRate\":\"4\",\"Contribution\":\"0\",\"TableName\":\"Account\"},{\"Name\":\"Pera 401K\",\"Description\":\"Pera 401K\",\"Value\":\"28448\",\"GrowthRate\":\"5\",\"PayoutRate\":\"4\",\"Contribution\":\"0\",\"TableName\":\"Account\"},{\"Name\":\"Pera Roth 401K\",\"Description\":\"Pera Roth 401K\",\"Value\":\"4075\",\"GrowthRate\":\"5\",\"PayoutRate\":\"4\",\"Contribution\":\"0\",\"TableName\":\"Account\"},{\"Name\":\"Pera 457 Roth\",\"Description\":\"Pera 457 Roth\",\"Value\":\"7335\",\"GrowthRate\":\"5\",\"PayoutRate\":\"4\",\"Contribution\":\"0\",\"TableName\":\"Account\"},{\"Name\":\"SS-A\",\"Description\":\"Social Security Annette\",\"Value\":\"28416\",\"GrowthRate\":\"0\",\"PayoutRate\":\"0\",\"Contribution\":\"1\",\"TableName\":\"Income\"},{\"Name\":\"SS-G\",\"Description\":\"Social Security Greg\",\"Value\":\"21756\",\"GrowthRate\":\"1\",\"PayoutRate\":\"0\",\"Contribution\":\"1\",\"TableName\":\"Income\"}],\"years\":[[2018,82632,0,272607,0,69149,0,3825,0,17246,0,22434,0,90094,0,219421,0,14658,0,28448,0,4075,0,7335,0,28416,21756,50172],[2019,86764,0,292737,0,72606,0,4016,0,18108,0,47556,0,94599,0,230392,0,15391,0,29870,0,4279,0,7702,0,28416,21756,50172],[2020,91102,0,313874,0,76236,0,4217,0,19013,0,73934,0,99329,0,241912,0,16161,0,31364,0,4493,0,8087,0,28416,21756,50172],[2021,95657,0,336068,0,80048,0,4428,0,19964,0,101631,0,104295,0,254008,0,16969,0,32932,0,4718,0,8491,0,28416,21756,50172],[2022,100440,0,359371,0,84050,0,4649,0,20962,0,130713,0,109510,0,266708,0,17817,0,34579,0,4954,0,8916,0,28416,21756,50172],[2023,105462,0,383840,0,88253,0,4881,0,22010,0,161249,0,114986,0,280043,0,18708,0,36308,0,5202,0,9362,0,28416,21756,50172],[2024,110735,4429,403032,16121,92666,3707,5125,205,23111,924,169311,6772,120735,4829,294045,11762,19643,786,38123,1525,5462,218,9830,393,28416,21756,101845],[2025,111843,4474,407063,16283,93592,3744,5176,207,23343,934,171005,6840,121943,4878,296985,11879,19839,794,38504,1540,5517,221,9929,397,28416,21756,102362],[2026,112961,4518,411133,16445,94528,3781,5228,209,23576,943,172715,6909,123162,4926,299955,11998,20037,801,38889,1556,5572,223,10028,401,28416,21756,102883],[2027,114091,4564,415245,16610,95473,3819,5280,211,23812,952,174442,6978,124394,4976,302955,12118,20238,810,39277,1571,5628,225,10128,405,28416,21756,103411],[2028,115232,4609,419397,16776,96428,3857,5333,213,24051,962,176186,7047,125638,5026,305985,12239,20440,818,39670,1587,5684,227,10229,409,28416,21756,103943],[2029,116385,4655,423591,16944,97392,3896,5387,215,24292,972,177948,7118,126894,5076,309045,12362,20644,826,40067,1603,5741,230,10331,413,28416,21756,104481],[2030,117549,4702,427827,17113,98366,3935,5441,218,24535,981,179727,7189,128163,5127,312135,12485,20850,834,40467,1619,5798,232,10435,417,28416,21756,105024],[2031,118724,4749,432105,17284,99349,3974,5495,220,24781,991,181524,7261,129444,5178,315257,12610,21059,842,40871,1635,5856,234,10540,422,28416,21756,105572],[2032,119911,4796,436426,17457,100342,4014,5550,222,25029,1001,183339,7334,130738,5230,318410,12736,21270,851,41280,1651,5915,237,10645,426,28416,21756,106126],[2033,121111,4844,440790,17632,101345,4054,5606,224,25279,1011,185172,7407,132045,5282,321595,12864,21483,859,41693,1668,5974,239,10751,430,28416,21756,106686],[2034,122323,4893,445198,17808,102358,4094,5662,226,25532,1021,187024,7481,133365,5335,324811,12992,21698,868,42110,1684,6034,241,10859,434,28416,21756,107251],[2035,123546,4942,449650,17986,103382,4135,5719,229,25788,1032,188894,7556,134698,5388,328060,13122,21915,877,42532,1701,6095,244,10968,439,28416,21756,107822],[2036,124781,4991,454147,18166,104416,4177,5776,231,26045,1042,190783,7631,136045,5442,331341,13254,22134,885,42958,1718,6156,246,11077,443,28416,21756,108398],[2037,126029,5041,458688,18348,105460,4218,5834,233,26305,1052,192691,7708,137405,5496,334654,13386,22356,894,43388,1736,6218,249,11188,448,28416,21756,108981],[2038,127289,5092,463274,18531,106515,4261,5893,236,26568,1063,194618,7785,138779,5551,338001,13520,22580,903,43821,1753,6280,251,11299,452,28416,21756,109569],[2039,128561,5142,467907,18716,107580,4303,5952,238,26833,1073,196564,7863,140167,5607,341381,13655,22806,912,44259,1770,6343,254,11412,456,28416,21756,110163],[2040,129847,5194,472586,18903,108656,4346,6012,240,27102,1084,198529,7941,141568,5663,344795,13792,23034,921,44702,1788,6406,256,11527,461,28416,21756,110763],[2041,131145,5246,477312,19092,109743,4390,6073,243,27373,1095,200514,8021,142983,5719,348243,13930,23265,931,45149,1806,6470,259,11642,466,28416,21756,111369],[2042,132456,5298,482086,19283,110840,4434,6134,245,27647,1106,202519,8101,144413,5777,351725,14069,23497,940,45600,1824,6535,261,11758,470,28416,21756,111980],[2043,133781,5351,486907,19476,111948,4478,6196,248,27923,1117,204544,8182,145857,5834,355242,14210,23732,949,46056,1842,6601,264,11876,475,28416,21756,112599],[2044,135119,5405,491776,19671,113067,4523,6258,250,28202,1128,206589,8264,147316,5893,358794,14352,23970,959,46517,1861,6667,267,11995,480,28416,21756,113223],[2045,136470,5459,496694,19868,114197,4568,6321,253,28484,1139,208654,8346,148789,5952,362382,14495,24210,968,46982,1879,6733,269,12115,485,28416,21756,113853],[2046,137835,5513,501661,20066,115339,4614,6384,255,28769,1151,210741,8430,150276,6011,366006,14640,24453,978,47452,1898,6801,272,12236,489,28416,21756,114490],[2047,139214,5569,506678,20267,116492,4660,6448,258,29056,1162,212848,8514,151779,6071,369666,14787,24698,988,47927,1917,6869,275,12359,494,28416,21756,115133],[2048,140606,5624,511745,20470,117657,4706,6512,260,29347,1174,214976,8599,153297,6132,373362,14934,24945,998,48406,1936,6937,277,12483,499,28416,21756,115783],[2049,142012,5680,516862,20674,118834,4753,6578,263,29640,1186,217126,8685,154830,6193,377096,15084,25194,1008,48890,1956,7007,280,12608,504,28416,21756,116439],[2050,143433,5737,522031,20881,120023,4801,6644,266,29936,1197,219297,8772,156379,6255,380867,15235,25446,1018,49379,1975,7077,283,12734,509,28416,21756,117102],[2051,144868,5795,527252,21090,121223,4849,6710,268,30236,1209,221490,8860,157943,6318,384675,15387,25700,1028,49873,1995,7148,286,12862,514,28416,21756,117771],[2052,146316,5853,532525,21301,122435,4897,6778,271,30539,1222,223705,8948,159522,6381,388522,15541,25957,1038,50372,2015,7219,289,12991,520,28416,21756,118447],[2053,147779,5911,537850,21514,123660,4946,6846,274,30844,1234,225942,9038,161117,6445,392407,15696,26217,1049,50876,2035,7291,292,13121,525,28416,21756,119130],[2054,149257,5970,543229,21729,124897,4996,6914,277,31152,1246,228201,9128,162728,6509,396331,15853,26479,1059,51385,2055,7364,295,13252,530,28416,21756,119820],[2055,150750,6030,548661,21946,126146,5046,6983,279,31464,1259,230483,9219,164355,6574,400295,16012,26744,1070,51899,2076,7437,297,13385,535,28416,21756,120516],[2056,152258,6090,554148,22166,127407,5096,7053,282,31778,1271,232788,9312,165999,6640,404298,16172,27011,1080,52418,2097,7512,300,13519,541,28416,21756,121220],[2057,153781,6151,559689,22388,128681,5147,7124,285,32096,1284,235115,9405,167659,6706,408341,16334,27282,1091,52942,2118,7588,304,13654,546,28416,21756,121930]]}"
//console.log("back from getIncomeSchedule.php: data="+JSON.stringify(data));
data = JSON.parse(data);
console.log(data.headers);
console.log(data.years);
Please check out what step is doing wrong.
P.S. Don't use the same variable name with different data type even JavaScript supports.
console.log(data); // it should be an object.
var json_str = JSON.stringify(data);
console.log(json_str); // it should be json string
var obj = JSON.parse(json_str); // it should be an object as data
console.log(obj); // it should be an object as data
var headers = obj.headers; // it should exist
var years = obj.years; // it should exist
Your first call to JSON.stringify(data) expects that data to be an object, but it's declared as a string.
To declare as an object it should be something like:
data = { "headers": [{ "Name":"Margin-G\"}] }
So in summary:
If you receive string use JSON.parse() to convert it to object
If you need to display an object as JSON use JSON.stringify()

Convert ajax String data to array to update ploty data

I am trying to update a jqplot chart dynamically with Ajax requests.
My server is returning a string represtation of the data as such:
"[['Juice',30],['Milk',30],['Water',30]]"
However I need to convert this string into an array of arrays.
Is this the correct approach to update the data and if so what is the best way to convert the string.
$.ajax({
url:'http://localhost',
success:function(plotData){
var data = plotData.split(",");
if(plot){
plot.series[0].data = data;
plot.redraw();
}
},
fail:function(error){
alert('error:'+error);
}
});
This code will convert into a one dimentional array:
0: "[['Helpdesk'"
1: "30]"
2: "['Users'"
3: "30]"
4: "['Auto Generated'"
5: "30]]"
You can use eval("var x= " + plotData) as an alternate solution. There are few dangers in using eval, please go through it before using it.
for convertiong a string u possibly could use this function
var plotData = "[['Juice',30],['Milk',30],['Water',30]]";
function strToArr(str) {
//pattern that checks for '[', ']'
var patt=/[\[\]]/gi;
//we replace the pattern with '' symbol
var tmp = str.replace(patt,'').split(',');
var result = []
for (var i = 0; i < tmp.length; i+=2) {
//now all data is in one array, we have to putt in pairs
result[i] = [ tmp[i], tmp[i+1] ]
}
return result;
}
console.log( strToArr(plotData) );
Format your data correctly
It looks like the response you're getting from the server is supposed to be JSON. But, it isn't valid json, and as such is represented as a string.
The change required is very trivial, This is invalid json:
[['Juice',30],['Milk',30],['Water',30]]
This is valid json:
[["Juice",30],["Milk",30],["Water",30]]
The only difference is the quotes. Changing the response string, may (depending on what you're doing server side) correct things immediately such that plotData is already an array of 3 arrays.
Return the right content type
If you are not already serving the response with correct http headers - ensure the response is served as application/json, this of course is in addition to serving a valid JSON string.
Force interpretation as json
To force jQuery to attempt to parse the response as json - you can set dataType explicitly:
$.ajax({
...
dataType: 'JSON'
...
});
I can't remember how strict this is that may work with no server side modifications.
Use JSON.parse
Alternatively, if you just want to handle the string as-is; you could just use JSON.parse:
input = "[['Juice',30],['Milk',30],['Water',30]]";
jsonString = input.replace(/'/g, '"'); // correct quotes as per point 1
result = JSON.parse(jsonString);
result would then be an array containing 3 arrays.

Categories