I am trying to get JSON from the url, But in the response object duplicate keys are removed. Is there any way to fetch it completely without removing the duplicate keys? Her
This is not possible with JSON.parse(). I believe that the more modern ES spec states that subsequent keys override earlier ones.
However, the JSON spec does not disallow JSON like this. And there are alternative parsers and serializers which can produce and consume such JSON from within JavaScript. For example, you can use the SAX-style JSON parser clarinet:
const clarinet = require('clarinet');
const parser = clarinet.parser();
const result = [];
parser.onkey = parser.onopenobject = k => {
result.push({key: k, value: null});
};
parser.onvalue = v => {
result[result.length - 1].value = v;
};
parser.write('{"a": "1", "a": "2"}').close();
console.log(result);
// [ { key: 'a', value: '1' }, { key: 'a', value: '2' } ]
If you are wondering about how to use require() from the browser, learn how to use webpack.