Dhanush Bala
Dhanush Bala

Reputation: 1132

read particular json value jquery ajax

I have a JSON file and i have to read the fullname from the json

[{
        "id": 2,
        "username": "admin",
        "firstname": "Stella",
        "lastname": "Cristy",
        "fullname": "Stella Cristy",
        "email": "[email protected]",
        "department": "",
        "firstaccess": 12121212,
        "lastaccess": 14101499879,
        "description": "",
        "descriptionformat": 1,
        "country": "IN",
        "profileimageurlsmall": "http:\/\/ddd\/lms\/dd.php\/5\/user\/icon\/f2",
        "profileimageurl": "http:\/\ddd\/lms\/pluginddddfile.php\/5\/user\/icon\/f1",
        "groups": [],
        "roles": [{
            "roleid": 5,
            "name": "",
            "shortname": "student",
            "sortorder": 0
        }],
        "preferences": [{
            "name": "assign_filter",
            "value": "notsubmitted"
        }],
        "enrolledcourses": [{
            "id": 14,
            "fullname": "DIGITAL LOGIC LABORATORY",
            "shortname": "CSEB111(P)"
        }, {
            "id": 4,
            "fullname": "COMPUTATIONAL MATHEMATICS -I",
            "shortname": "CSEB101 "
        }]
    }]

from the above json i want to get the fullname , i tried but not getting the fullname.

my jquery code is

 $.ajax({
                     url: "url",
                     dataType: "json",
                     type: "POST",
                     success: function(data) {
                 var json = $.parseJSON(data);
                 console.log(json.fullname);
                     }
            });

the error is TypeError: json is null how to get the fullname from the above json

Upvotes: 1

Views: 71

Answers (3)

Naveed Yousaf
Naveed Yousaf

Reputation: 133

There is an issue with your Json. it is not in proper format.
Your json is throwing error at
"profileimageurl": "http:\/\ddd\/lms\/pluginddddfile.php\/5\/user\/icon\/f1"
you have to fix your json first then search in your json for full name. You can fix your json like "profileimageurl": "http:\/ddd\/lms\/pluginddddfile.php\/5\/user\/icon\/f1" i-e removing \ from http:\/\
or by adding / this to your url http:\/\/.
Further for next time if you test json first check is your json correct or not ? there are many tools available online to test your json such as this tool after fixing the issues in json you can access it as said by Konstanin D

Upvotes: 0

moyeen52
moyeen52

Reputation: 425

First issue try to correct

"profileimageurlsmall": "http:\/\/ddd\/lms\/dd.php\/5\/user\/icon\/f2",
"profileimageurl": "http:\/\ddd\/lms\/pluginddddfile.php\/5\/user\/icon\/f1",

Then your json object has no key fullname You need to use json[0].fullname

       $.ajax({
                 url: "url",
                 dataType: "json",
                 type: "POST",
                 success: function(data) {
                 var json = $.parseJSON(data);
                 console.log(json[0].fullname);
                 }
        });

Upvotes: 1

Konstantin Dinev
Konstantin Dinev

Reputation: 34915

This is an array so to get the fullname of the first array member you have to do:

json[0].fullname

Upvotes: 3

Related Questions