0

I have the following json in a local file:

    {
   "fields": {

    "input": {
        "name": "txtCpf",
        "id": "txtCpf",
        "value": "",
        "type": "text",
        "Mask": "000.000.000-00",
        "class": "input"
    },
    "input": {
        "name": "txtTelephone",
        "id": "txtTelefone",
        "value": "",
        "type": "text",
        "Mask": "(00) 00000-0000",
        "class": "input"
    },

    "button": {
        "name": "btnSave",
        "id": "btnSave",
        "value": "",
        "class": "input"
        }
   }
}

This is my javascript code:

var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function () {
    if (this.readyState == 4 && this.status == 200) {

        myObj = JSON.parse(this.responseText);

        document.getElementById("demo").innerHTML = myObj.fields.input.name;

        Object.keys(myObj).forEach(function (item) {
            console.log(myObj.fields.input);

        });
        /* myObj.foreach(function(input){
             var fields = input.fields;
             alert(fields);
         }); */



    }
};

xmlhttp.open("GET", "mock.json", true);
xmlhttp.send();

This myObj.fields.input.name is returning only the last position of the input object so it shows txtTelephone

Would like a return of all objects of the input and not only of the last element

Using javascript only..

9
  • 4
    yours json incorrect, inputs must be into array Commented Jun 14, 2017 at 14:48
  • 1
    To rephrase slightly differently: you can't have duplicate keys in a JS object. Or a JSON object. Commented Jun 14, 2017 at 14:49
  • 2
    You cannot have a duplicated key in a JSON, try JSONLint online tool Commented Jun 14, 2017 at 14:49
  • As @GeorgeVassilev has written, you're JSON is invalid as it contains a duplicate key. I believe your data structure should be {"fields:[{"name": "txtTelephone", ect}]} Commented Jun 14, 2017 at 14:50
  • 1
    Actually technically duplicate keys are still considered valid JSON, although is highly discouraged. Commented Jun 14, 2017 at 14:51

2 Answers 2

3

Correct json to :

{
    "fields": {
        "input": [{
                "name": "txtCpf",
                "id": "txtCpf",
                "value": "",
                "type": "text",
                "Mask": "000.000.000-00",
                "class": "input"
            },
            {
                "name": "txtTelephone",
                "id": "txtTelefone",
                "value": "",
                "type": "text",
                "Mask": "(00) 00000-0000",
                "class": "input"
            }
        ],
        "button": {
            "name": "btnSave",
            "id": "btnSave",
            "value": "",
            "class": "input"
        }
    }
}

to retrieve all input names, change your script to:

myObj.fields.input.forEach(function (item) {
    console.log(item.name);
})
Sign up to request clarification or add additional context in comments.

4 Comments

That still does not answer the question: How to retrieve all the names?
@FlorianAlbrecht I'm guessing ones the Json is fixed the current JavaScript will do that already.
@Fran nope, not in its current form.
@FlorianAlbrecht I didn't realize that OP still has issue getting the items ones the Json is fixed. You should post a complete answer then I think.
0

Your json is not correct, You can't have with a json object 2 keys identical (input) If there is 2 identicals keys; the first one is overwritten with the last one.

Convert it to an array for example or change your key name.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.