0

This is an external 'data.json' sample to be formatted like this:

    {
    "A": [{"title": "Accountant", "name": "Adam"}],
    "B": [{"title": "Beefeeter", "name": "Brandon"}],
    "C": [{"title": "Coder", "name": "Charles"}],
    "D": [{"title": "Doctor", "name": "David"}]
    }

And this is the JS part for it with questions in //....

    <script>
    $(document).ready(function(){
    $.getJSON("data.json", function(data){
        $('title').html(data....title); // What is the best way to access/get the value of title, say Beefeeter in B?
        $('name').html(data....name); // What is the best way to access/get the value of name, say Coder in C?
        .....
        });
    });
    </script>

Thank you for helping.

1
  • data.B[0].title, data.C[0].name Commented Sep 9, 2017 at 22:55

2 Answers 2

2

Simply do

$.getJSON("data.json", function(data){
    $('title').html(data.B[0].title); // Beefeeter in B
    $('name').html(data.C[0].title); // Coder in C
    .....
    });

Looking at it again I am not quite sure what is behind your jquery selectors 'title' and 'name'. These are not standard HTML tag names. Did you maybe intend to refer to elements with id="title" and id="name"? In that case the selectors should be $('#title') and $('#name') instead.

Edit:

If you want to access the actual keys 'A', 'B', 'C' and so on, you could list them together with the values using the Object.entries() method:

var arr=Object.entries(data); // this turns data into an array arr
// You can then step through that array and use the key (obj[0]) itself
// and of course all properties of the object-type values (obj[1]):
arr.forEach(function(i,obj){ // i is just a numeric index: 0,1,2,...
  console.log( obj[0] /* A, B, C... */, obj[1].title, obj[1].name);
}
Sign up to request clarification or add additional context in comments.

4 Comments

You're right. At the last minute I switched 'title' to 'name' before I submitted and did not think about it. And yes, 'title' is the HTML element, but 'name' is not but a class identifier. Lovely, simple and clear answer. I humbly thank you so much.
BTW, if I would like to access the value 'A', 'B', 'C', or 'D' (not value of but the value A, value B, value C, etc, itselves) in the data.json of {...}, would I be able to do that? Thanks much.
I think I go it: Object.keys(data) to return A, B, C, D. Object.keys(data)[2]) to return C. Thank you.
Yep, if you need the keys only, then Object.keys() is the easiest way to go.
0

If you don't know your categories in advance (A, B, C, etc.) the only approach is to iterate through the json.

var list = jQuery.parseJSON( data );
for( var key in list) {
    $('title').html(list[key][0].title);
    $('name').html(list[key][0].name);
 }

In addition, you can append as many divs as your json file has categories (if you want).

1 Comment

Another big THANK YOU to the master. I benefit from asking this question.

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.