0

I have the following JSON.

{
    "lang": [
        {
            "SECTION_NAME": {
                "english": "My title"
            },
            "SECTION_NAME_2": {
                "english": "My title"
            }
        }
    ]
}

And I'm looking to print the value like this:

$.getJSON('json/lang.json', function(data) {
    var text = data['lang']['SECTION_NAME'];
    $('#title').html(text.english);
});

But I have the following error:

TypeError: undefined is not an object (evaluating 'text.english')

Any help please.

Thanks.

2 Answers 2

1

You have to access it via index as lang is an array of object

like this

console.log(data['lang'][0]['SECTION_NAME'])

JSFIDDLE

Sign up to request clarification or add additional context in comments.

3 Comments

How can I change my array to make something like data['lang']['SECTION_NAME'].english ? Because, if my son change, I need to change all the [x]...
then object design should be { lang : { section : { english : 'My title' } } @roberto
oh my bad :( , { lang : { SECTION_NAME : { english : 'My title' } } . now try :) @roberto
1

The value of lang is an array which contains an object.

You are ignoring the array and trying to access the objects as if it was the value of lang directly.

Comments