i have a json output code like this:
{"a":{"p1":"1"},"a":{"p2":"2"},"b":{"b1":"b2"}}
how to convert it to below using javascript or jquery or php ?
{"a":{"p1":"1","p2":"2"},"b":{"b1":"b2"}}
EDIT:
i generate json code by this code:
parts2.push('"'+$(this).attr('alt')+'":{"'+$(this).attr('title') + '"' + ":" + '"'+$(this).attr('value') + '"}' );
however $(this).attr(‘alt’) maybe repeated in loop and i want to prevent duplicate key and instead append value to that key
Each property of an object is supposed to have a unique key name. If you try to parse JSON with duplicate key names, only the last occurring value is used, so it isn’t possible to parse this with the native
JSON.parseand still expect data to be preserved.As per your edit, you can prevent the duplicates from ever occurring: