How to get the JSON with duplicate keys completely in javascript

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? Here is my js Code

$('document').ready(function(){
    var s = $.getJSON("new.json");
    console.log(s);
});

following is my new.json

{
"s": "wae",
"s": "asd"
}

But in console i am getting the json Object as follows

responseJSON: Object
s: "asd"

Thanks in advance


Solution 1:

If you can't change the server response, for simple JSON data you can request the json like text and parse it like a string:

var check = new RegExp('["\']([^\'"]*)[\'"][^:]*:[^"\']*["\']([^\'"]*)[\'"]',"g");
    $.ajax({
        url : "text.json",
        dataType : "text",
        success : function(data){
            var newData = {};
            data.replace(check,function(a,b,c){
                if(typeof newData[b] == "undefined"){
                    newData[b] = c;
                }else if(typeof newData[b] == "object"){
                    newData[b].push(c);
                }else{
                    var ca = newData[b];
                    newData[b] = [ca,c];                     
                }
                return a;
            });
            console.log(newData);
            console.log($.parseJSON(data));
        },
        error : function(e,a){
            console.log(e,a);
        }
    });

in this code newData with your json is:

{"s": ["wae","asd"]}

Solution 2:

Keys in a JSON object should be unique. Otherwise the last key with the value is usually the one presented when requesting that key. Having keys the same also makes it more difficult to differentiate between attributes of an object. The whole point of a key is to make it accessible and available.

To get around this you could always:

  1. Change the keys in your JSON

  2. Change the JSON to contain an array

    {"s": ["wae","asd"]}

The JavaScript Object Notation (JSON) Data Interchange Format) (RFC7159) says:

The names within an object SHOULD be unique.

In this context should must be understood as specified in RFC 2119

SHOULD This word, or the adjective "RECOMMENDED", mean that there may exist valid reasons in particular circumstances to ignore a particular item, but the full implications must be understood and carefully weighed before choosing a different course.


RFC 7159 explains why unique keys are good: > An object whose names are all unique is interoperable in the sense > that all software implementations receiving that object will agree on > the name-value mappings. When the names within an object are not > unique, the behavior of software that receives such an object is > unpredictable. Many implementations report the last name/value pair > only. Other implementations report an error or fail to parse the > object, and some implementations report all of the name/value pairs, > including duplicates. > > JSON parsing libraries have been observed to differ as to whether > or not they make the ordering of object members visible to calling > software. Implementations whose behavior does not depend on member > ordering will be interoperable in the sense that they will not be > affected by these differences.