This project has moved. For the latest updates, please go here.

Nested JSON data formats

Topics: Extensions
Aug 19, 2016 at 12:24 PM
Sorry if this is a stupid question - new to all this JSON stuff.

I have a file that serves up json - the json works fine with my angular app (with the exception that for the repeat I have to use the object name twice like this ng-repeat="entry in categories.categories"

I want to pull out a specific category and then a specific model within that category but not having much luck.

example json (from 3rd party service that I have cust and pasted into a local package.json file and wrapped in the angular.callbacks._0 ()...
{ "categories": [
    {
        "id": "D3CAE149-AC5E-483D-8F4E-713590EBD6C7",
        "name": "Accessories",
        "slug": "accessories",
        "path": "Accessories",
        "description": "",
        "images": {
            "s": "",
            "l": "",
            "o": ""
        },
        "models": [
            {
                "id": "5E3950CD-6A54-450F-BAC0-B65A258115C1",
                "name": "Garden Games",
                "slug": "garden-games",
                "sku": "GG",
                "price": "120",
                "images": [ ]
            }
        ]
    },
    {
        "id": "5A683C23-5720-4C24-8491-361E8D6AF077",
        "name": "Fairy lights",
        "slug": "fairy-lights",
        "path": "Lighting/Fairy lights",
        "description": "",
        "images": {
            "s": "",
            "l": "",
            "o": ""
        },
        "models": [
            {
                "id": "6E9120B6-4177-4016-B552-FA0C990F0970",
                "name": "10m Ivy with Fairy Lights",
                "slug": "10m-ivy-with-fairy-lights",
                "sku": "",
                "price": "25",
                "images": [ ]
            },
            {
                "id": "E48366ED-1CC8-43EB-928E-AE98D6DD75A2",
                "name": "10m Ivy with Fairy Lights and Flowers",
                "slug": "10m-ivy-with-fairy-lights-and-flowers",
                "sku": "",
                "price": "45",
                "images": [ ]
            }
        ]
    }
] }
I get the data like this
var getProductDetails = function(catname, productname){
            return $http.jsonp("http://localhost.dev:8888/pluralsight/package2.json?JSON_CALLBACK")
                .then(function(response){
                    console.log(response.data);

                    People = [
                        { ID: 1, FirstName: "Chris", LastName: "Pearson", BookIDs: [1001, 1002, 1003] },
                        { ID: 2, FirstName: "Kate", LastName: "Johnson", BookIDs: [2001, 2002, 2003] },
                        { ID: 3, FirstName: "Josh", LastName: "Sutherland", BookIDs: [3001, 3002, 3003] },
                        { ID: 4, FirstName: "John", LastName: "Ronald", BookIDs: [4001, 4002, 4003] },
                        { ID: 10, FirstName: "Kate", LastName: "Pinkerton", BookIDs: [4001, 3002, 2003] }
                    ];

                    catdata = [
                        {
                            "id": "D3CAE149-AC5E-483D-8F4E-713590EBD6C7",
                            "name": "Accessories",
                            "slug": "accessories",
                            "path": "Accessories",
                            "description": "",
                            "images": {
                                "s": "",
                                "l": "",
                                "o": ""
                            },
                            "models": [
                                {
                                    "id": "5E3950CD-6A54-450F-BAC0-B65A258115C1",
                                    "name": "Garden Games",
                                    "slug": "garden-games",
                                    "sku": "GG",
                                    "price": "120",
                                    "images": [ ]
                                }
                            ]
                        },
                        {
                            "id": "5A683C23-5720-4C24-8491-361E8D6AF077",
                            "name": "Fairy lights",
                            "slug": "fairy-lights",
                            "path": "Lighting/Fairy lights",
                            "description": "",
                            "images": {
                                "s": "",
                                "l": "",
                                "o": ""
                            },
                            "models": [
                                {
                                    "id": "6E9120B6-4177-4016-B552-FA0C990F0970",
                                    "name": "10m Ivy with Fairy Lights",
                                    "slug": "10m-ivy-with-fairy-lights",
                                    "sku": "",
                                    "price": "25",
                                    "images": [ ]
                                },
                                {
                                    "id": "E48366ED-1CC8-43EB-928E-AE98D6DD75A2",
                                    "name": "10m Ivy with Fairy Lights and Flowers",
                                    "slug": "10m-ivy-with-fairy-lights-and-flowers",
                                    "sku": "",
                                    "price": "45",
                                    "images": [ ]
                                }
                            ]
                        }
                    ];

                    var filteredPost = JSLINQ(response.data)
                        .Where(function(item){ return item.name == "Accessories" });

                    var filteredPost4 = JSLINQ(catdata)
                        .Where(function(item){ return item.name == "Accessories" });

                    console.log(filteredPost4);

                    return filteredPost;

                });
        };
The data I setup locally finds the element but the data from the $http.jsonp does not return anything...

in google dev tools the console writes display the data from the $http.jsonp as

Object {categories: Array[35]}

and the data from local data 'catdata' as [Object, Object]

I have tried different ways of converting the response.data but nothing works!

Any help anyone can offer would be great - thanks