H
H
Hello America2018-12-22 16:49:08
JavaScript
Hello America, 2018-12-22 16:49:08

Why is the same JSON not translated from string to object in JavaScript?

In general, I filed the problem in the sandbox on jsFiddle https://jsfiddle.net/WebSofter/wdnkhszp/
Here is the whole code

//Случай 1. Есть такой JSON объект

var data1 = {"pages":"only","menu":[{"skrytoe-menyu":[{}]},{"services":[{"field_3":{"id":"201","menutype":"services","alias":"iphone-remont","title":"Ремонт IPhone","parent_id":"1"},}]},{"mainmenu":[{"field_8":{"id":"1227","menutype":"mainmenu","alias":"joomprice","title":"JoomPrice","parent_id":"881"}}]},{"kompaniya":[{"field_4":{"id":"234","menutype":"kompaniya","alias":"blog","title":"Контакты","parent_id":"1"},"field_5":{"id":"242","menutype":"kompaniya","alias":"contact-page","title":"Contact Us","parent_id":"1"},"field_6":{"id":"243","menutype":"kompaniya","alias":"about-us-page","title":"О нас","parent_id":"1"},"field_7":{"id":"244","menutype":"kompaniya","alias":"products-page","title":"Products","parent_id":"1"},}]},{"droptables":[{}]}]};

//Гоняю в строку
var str1 = JSON.stringify(data1);
//Гоняю обратно в объект
var json1 = JSON.parse(str1);
//Все выводится все ок
console.log(json1);

//Случай 2. Есть тот же JSON объект в виде строки

var data2 = '{"pages":"only","menu":[{"skrytoe-menyu":[{}]},{"services":[{"field_3":{"id":"201","menutype":"services","alias":"iphone-remont","title":"Ремонт IPhone","parent_id":"1"},}]},{"mainmenu":[{"field_8":{"id":"1227","menutype":"mainmenu","alias":"joomprice","title":"JoomPrice","parent_id":"881"}}]},{"kompaniya":[{"field_4":{"id":"234","menutype":"kompaniya","alias":"blog","title":"Контакты","parent_id":"1"},"field_5":{"id":"242","menutype":"kompaniya","alias":"contact-page","title":"Contact Us","parent_id":"1"},"field_6":{"id":"243","menutype":"kompaniya","alias":"about-us-page","title":"О нас","parent_id":"1"},"field_7":{"id":"244","menutype":"kompaniya","alias":"products-page","title":"Products","parent_id":"1"},}]},{"droptables":[{}]}]}';

//Пытаюсь гонять в объект и бац ошибка
var json2 = JSON.parse(data2);
console.log(json2);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2018-12-22
@websofter

Extra commas after the last property in the object. In js this is possible, in json it is not.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question