Debug School

rakesh kumar
rakesh kumar

Posted on

How to extracting elements from json array of object

Extracting all single field in array from json object of array
Get only first element of array
Filtering array
Get First Element of particular value of key

My data is

[
    {
        "id": 107,
        "user_id": "22",
        "user_name": "sourav",
        "user_email": "vibiv32093@bitofee.com",
        "file_pic": "1703569022.png",
        "slug_id": null,
        "slug": "ravi-kumar",
        "slugname": null,
        "country_id": "101",
        "state_id": "4025",
        "city_id": "58078",
        "mobile": "9142239665",
        "digital_marketer": null,
        "bio": "i am influencer",
        "social_site": "{\"facebook\":\"https:\\/\\/www.facebook.com\\/ravi.cotocus\",\"twitter\":\"https:\\/\\/www.twitter.com\\/rajesh.cotocus\",\"youtube\":\"https:\\/\\/www.youtube.com\\/rajesh.cotocus\",\"wordpress\":null,\"tumblr\":null,\"instagram\":null,\"quora\":null,\"pinterest\":null,\"reddit\":null,\"koo\":null,\"scoopit\":null,\"slashdot\":null,\"telegram\":null,\"fb_grp\":null,\"linkedin_grp\":null,\"linkedin\":null,\"roposo\":null,\"chingari\":null,\"mitron\":null}",
        "social_price": "{\"facebook\":\"24\",\"twitter\":\"67\",\"youtube\":\"90\",\"wordpress\":null,\"tumblr\":null,\"instagram\":null,\"quora\":null,\"pinterest\":null,\"reddit\":null,\"koo\":null,\"scoopit\":null,\"slashdot\":null,\"telegram\":null,\"fb_grp\":null,\"linkedin_grp\":null,\"linkedin\":null,\"roposo\":null,\"chingari\":null,\"mitron\":null}",
        "influencer_admin_id": 22,
        "cart_socials": "{\"facebook\":\"24\",\"twitter\":\"67\",\"youtube\":\"90\"}",
        "admin_id": "18",
        "country_name": "India",
        "state_name": "Jharkhand",
        "city_name": "Bokāro"
    },
    {
        "id": 106,
        "user_id": "19",
        "user_name": "rajesh devops",
        "user_email": "ropam89018@giratex.com",
        "file_pic": "",
        "slug_id": null,
        "slug": "rajesh-devops",
        "slugname": null,
        "country_id": "101",
        "state_id": "4026",
        "city_id": "57848",
        "mobile": "7488127637",
        "digital_marketer": null,
        "bio": "rajeshinfluencers",
        "social_site": "{\"facebook\":\"https:\\/\\/www.facebook.com\\/rajeshdevops.cotocus\",\"twitter\":\"https:\\/\\/www.twitter.com\\/rajeshdevops.cotocus\",\"youtube\":null,\"wordpress\":null,\"tumblr\":null,\"instagram\":null,\"quora\":null,\"pinterest\":null,\"reddit\":null,\"koo\":null,\"scoopit\":null,\"slashdot\":null,\"telegram\":null,\"fb_grp\":null,\"linkedin_grp\":null,\"linkedin\":null,\"roposo\":null,\"chingari\":null,\"mitron\":null}",
        "social_price": "{\"facebook\":\"55\",\"twitter\":\"67\",\"youtube\":null,\"wordpress\":null,\"tumblr\":null,\"instagram\":null,\"quora\":null,\"pinterest\":null,\"reddit\":null,\"koo\":null,\"scoopit\":null,\"slashdot\":null,\"telegram\":null,\"fb_grp\":null,\"linkedin_grp\":null,\"linkedin\":null,\"roposo\":null,\"chingari\":null,\"mitron\":null}",
        "influencer_admin_id": null,
        "cart_socials": null,
        "admin_id": null,
        "country_name": "India",
        "state_name": "Karnataka",
        "city_name": "Bangalore Urban"
    },
    }
]
Enter fullscreen mode Exit fullscreen mode

extract all admin_id in array
Extracting all single field in array from json object of array

var jsonArray = [
    // ... (your JSON array)
];

var adminIds = $.map(jsonArray, function(obj) {
    return obj.admin_id;
});

console.log(adminIds);
Enter fullscreen mode Exit fullscreen mode

output

["18", null, "18"]
Enter fullscreen mode Exit fullscreen mode

Get only first element of array

var jsonArray = [
    // ... (your JSON array)
];

var firstAdminId = jsonArray[0].admin_id;

console.log(firstAdminId);
Enter fullscreen mode Exit fullscreen mode

Filtering array

var adminIds = ["18", null, "18"];

// Use the filter function to remove null values
var nonNullAdminIds = adminIds.filter(function(adminId) {
    return adminId !== null;
});

// Get the first non-null admin_id
var firstNonNullAdminId = nonNullAdminIds[0];

console.log(firstNonNullAdminId);
Enter fullscreen mode Exit fullscreen mode

Another Examples

var jsonArray = [
    { id: 1, name: 'John', age: 25 },
    { id: 2, name: 'Jane', age: 30 },
    { id: 3, name: 'Bob', age: 22 }
];
Enter fullscreen mode Exit fullscreen mode
var names = [];

$.each(jsonArray, function(index, obj) {
    names.push(obj.name);
});

console.log(names);
Enter fullscreen mode Exit fullscreen mode
// Output: ["John", "Jane", "Bob"]
Enter fullscreen mode Exit fullscreen mode
var names = [];

$.each(jsonArray, function(index, obj) {
    names.push(obj.name);
});

console.log(names);
// Output: ["John", "Jane", "Bob"]
Enter fullscreen mode Exit fullscreen mode

Get First Element of particular value of key

var jsonArray = [
    { id: 1, name: 'John', age: 25 },
    { id: 2, name: 'Jane', age: 30 },
    { id: 3, name: 'Bob', age: 22 }
];

// Get the name of the first element in the array
var firstName = jsonArray[0].name;

console.log(firstName);
Enter fullscreen mode Exit fullscreen mode

Output

John

Top comments (0)