why is javascript object "undefined"? - javascript
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()
Related
problem in parsing json in javascript and access value by key
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); });
Access specific data on Json - NodeJS
I'm trying to access data from a json but i can't... I have tried some explanations i found on the internet, but i guess there is something wrong on my code. I'm getting data from a sql server database using the following code: async function getLogin1(Operador, Senha) { try { let pool = await sql.connect(config); let getLogin1 = await pool.request() .input('input_parameter', sql.VarChar, Operador) .input('input_parameter1', sql.VarChar, Senha) .query("SELECT Codigo, Operador, Senha FROM Usuario where Operador = #input_parameter and Senha = #input_parameter1"); return getLogin1.recordsets; } catch (error) { console.log(error); } } So i get the recordsets here and put in a json: router.route("/Login1").get((request, response) => { console.log(request.body.operador); console.log(request.body.senha); Operador = request.body.operador; Senha = request.body.senha; dboperations.getLogin1(Operador, Senha).then(result => { console.log(Operador, Senha); response.json(result); var json = JSON.stringify(result); console.log(json); }) }) On the console it shows the json: [[{"Codigo":1,"Operador":"Username","Senha":"123456"}]] I would like to get the individual data (codigo, operador and senha) to put in a individual string each one, but i cant access the data. When I try like json[0] for example i get all the json (because my json has every info on the first position i guess), and when i try json.Codigo (for example) i get a "undefined" error. What am i doing wrong and what the best way to solve? And sorry for the low knowledge, this is my very first api. (And yes, this is a login code and its not the best way to treat user data but its a very small system for intern use)
Your JSON "result" is: [[{"Codigo":1,"Operador":"MASTER","Senha":"TERA0205"}]] So it is an array of array containing one object with keys Codigo, Operador, Senha. To get the value of the Codigo of that object you would likely need to access it like var CodigoVal = result[0][0].Codigo; // 1 var OperadorVal = result[0][0].Operador; // "MASTER"
Looks like json has nested array, if so json[0] will give inner array so you have to probably do like json[0][0].Codigo [ [ { "Codigo":1, "Operador":"Username", "Senha":"123456" } ] ]
If we look at the JSON you posted above, we have an array which has an array with an object at its first index. So to get access to that object you need to do: const obj = json[0][0]; Now from obj you can extract Codigo, Operado and Senha like const condigo = obg.Condigo; const operado = obg.Operado; const senha = obg.Senha; You can also directly access them like var codigo = json[0][0].Codigo; var operador = json[0][0].Operador; var senha = json[0][0].Senha;
json.Codigo doesnt work because your json has a array in it. Which has the object you are trying to get data from. json[0] returns everything because that gives you the object from that array so if you want Codigo try json[0][0].Codigo I hope this helped you!
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
Read property from a Json object in typescript/javascript
I am working with an auth token with I receive from a third-party API. I have given a sample of the decoded token below, { "nbf": 1564128888, "exp": 1564132488, "iss": "http://example.com:5002", "aud": "http://example.com:5002/resources", "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress": "Micky#gmail.com", "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name": "Micky Mouse", "amr": ["custom"] } I am struggling to read the "name" claim in javascript. How can I read that property in javascript or typescript?
You can access complex property names like this: const name = token["http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name"] You could also abstract this away for reusability (like the ClaimTypes in C#) const ClaimTypes = { name: "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name", // other relevant claims }; const name = token[ClaimTypes.name];
You will get your data in form of a string, convert it to json let jsonData = '{"nbf": 1564128888,"exp": 1564132488,"iss": "http://example.com:5002","aud": "http://example.com:5002/resources","http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress": "Micky#gmail.com","http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name": "Micky Mouse","amr": ["custom"]}' let parsedJSON = JSON.parse(jsonData) console.log(parsedJSON["http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name"]) // Micky Mouse console.log(parsedJSON["nbf"]) // 1564128888 console.log(parsedJSON["http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress"]) // Micky#gmail.com And then read it like parsedJSON["your key"]. Things on the left sides are property names or keys. You can retrive them by let jsonData = '{"nbf": 1564128888,"exp": 1564132488,"iss": "http://example.com:5002","aud": "http://example.com:5002/resources","http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress": "Micky#gmail.com","http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name": "Micky Mouse","amr": ["custom"]}' let parsedJSON = JSON.parse(jsonData) console.log(Object.keys(parsedJSON))
JSON.parse(yourData) - convert JSON to JS JSON.stringify(yourData) - from JS to JSON so after JSON.parse you will get JS object and able to get yourData.name Here you can read: MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse you can try for example: https://jsonformatter.org/json-parser
AngularJS set localstorage (object json)
How can I change/modify/refresh my localstorage of AngularJS? My localstorage content is an object json: //data content: {Id:1,Name:"Juanito"} localStorageService.set("MyObj",data); console.log(localStorageService.get("MyObj")); //SHOW object JSON {Id:1,Name:"Juanito"} But if I modify a unique key like this: localStorageService.set("MyObj.Id","otherid"), It does not work. How do I fix this?
Try retrieving, then updating and re-setting: var data = {id: 123} localStorageService.set("MyObj",data); var dataFromLS = localStorageService.get("MyObj"); dataFromLS.id = 456; localStorageService.set("MyObj",data); console.log(localStorageService.get("MyObj"));
When you change/modify your key name, a brand new object will be created/stored in your service. If you want to change/alter key's data/value, perform intermediate operations and set it with same key.