-2

I've spent a while trying to work out how to desterilise this JSON file and get each products

ID,NAME,COLLECTION COST, AND WEATHER ITS ON OFFER

Im currently attempting this with Newtonsoft.Json in a C# class.

could someone please point me in the right direction. Many thanks

for the JSON text visit: https://pastebin.com/bkQwpsAy

{
    "_embedded": {
        "products": [{
                "uuid": "74f1501f-4a52-490a-b2b9-668f16e2db95",
                "createdAt": "2020-04-20T13:44:22.000+00:00",
                "itemId": "DRN543",
                "altItemGroupId": "",
                "popularityScore": 101.22,
                "itemName": "Coca-Cola Bottles (GB) 6x1.5L",
                "jsonFriendlyItemName": "Coca-Cola Bottles (GB) 6x1.5L",
                "seoFriendlyItemName": "coca-cola-bottles-gb-6x1-5l",
                "brand": "Coca Cola",
                "imageLinks": ["https://jjproducts.global.ssl.fastly.net/jjfoodservice/image/upload/v1588074299/products/DRN543/_S/ggktoxjzbplky4uovec4.jpg"],
                "price": 9.99,
                "depth": 29.0,
                "height": 32.0,
                "itemNetWeight": 9.0,
                "jadMobileItemName": "Coca Cola (GB) 6x1.5L",
                "category1Id": "100005",
                "category2Id": "200261",
                "category3Id": "300194",
                "category4Id": "400084",
                "category5Id": "",
                "category1Name": "Drinks",
                "category2Name": "Soft Drinks",
                "category3Name": "Fizzy Drinks",
                "category4Name": "Cola",
                "category5Name": "",
                "origin": "United Kingdom",
                "catchOrigin": "",
                "productDescription": "",
                "sellingPoints": "Coca Cola\nCocaCola",
                "shelfLife": 135,
                "sizeOrCut": "6x1.5l",
                "qtyPerLayer": 20.0,
                "standardPalletQty": 80.0,
                "unitVolume": 17632.0,
                "width": 19.0,
                "allergensDeclaration": "",
                "storageCondition": "Store cool and dry.",
                "storedAt": "2020-11-03T23:52:28.789210Z",
                "branches": [{
                        "locationId": "EN-MW",
                        "locationName": "Enfield Branch",
                        "warehouseArea": "DA",
                        "warehouseZone": "GZ"
                    }, {
                        "locationId": "LS-MW",
                        "locationName": "Leicester Branch",
                        "warehouseArea": "DA",
                        "warehouseZone": "GZ"
                    }
                ],
                "branchesBeforeLastUpdate": [{
                        "locationId": "EN-MW",
                        "locationName": "Enfield Branch",
                        "warehouseArea": "DA",
                        "warehouseZone": "GZ"
                    }, {
                        "locationId": "LS-MW",
                        "locationName": "Leicester Branch",
                        "warehouseArea": "DA",
                        "warehouseZone": "GZ"
                    }
                ],
                "video": [],
                "categoryList": "[{\"id\":\"100005\",\"name\":\"Drinks\"},{\"id\":\"200261\",\"name\":\"Soft Drinks\"},{\"id\":\"300194\",\"name\":\"Fizzy Drinks\"}]",
                "categoryId": "100005,200261,300194",
                "categoryName": "Drinks,Soft Drinks,Fizzy Drinks",
                "categoryNormalised": "[Cola Drinks Fizzy Soft]",
                "productFeatures": ["Popular", "Ambient", "Vegan", "Vegetarian"],
                "unitSize": "1.5L",
                "unitPriceDivider": 0.16666,
                "unitPriceTypeDisplayText": "each",
                "offer": {
                    "itemId": "DRN543",
                    "promoForCc": false,
                    "promoTagId": "Monthly",
                    "promoTag": "Monthly Special Promotions",
                    "promoEnd": "31/12/2020",
                    "promoDisAmt": 0,
                    "promoDisPct": 0,
                    "promoDiscountText": [],
                    "id": "DRN543"
                },
                "delivery": {
                    "price": 8.29,
                    "priceInc": 8.29,
                    "unitPriceDisplay": "£1.38 each",
                    "step": 1.0,
                    "max": 15.0,
                    "collection": false
                },
                "collection": {
                    "price": 7.29,
                    "priceInc": 7.29,
                    "unitPriceDisplay": "£1.21 each",
                    "step": 1.0,
                    "max": 15.0,
                    "collection": true
                },
                "previouslyPurchased": false,
                "favourite": false,
                "available": true,
                "new": false,
                "popular": true,
                "popularOnCategory1": true,
                "popularOnCategory2": true,
                "popularOnCategory3": true,
                "ageRestriction": false,
                "halal": false,
                "vegan": true,
                "vegeterian": true,
                "numberOfPackage": 6,
                "numberOfUnitsInPackage": 1.5,
                "unitType": "litre",
                "CCMAltItemGroup": "",
                "JJeBrand": "Coca Cola",
                "JadConsumableDepth": 0.0,
                "JadConsumableHeight": 0.0,
                "JadConsumableWidth": 0.0,
                "JJeCategory1Id": "100005",
                "JJeCategory2Id": "200261",
                "JJeCategory3Id": "300194",
                "JJeCategory4Id": "400084",
                "JJeCategory5Id": "",
                "JJeCategory1": "Drinks",
                "JJeCategory2": "Soft Drinks",
                "JJeCategory3": "Fizzy Drinks",
                "JJeCategory4": "Cola",
                "JJeCategory5": "",
                "JJeCookingInstruction": "Best served chilled.",
                "JJeIngredients": "Carbonated Water, Sugar, Colour (Caramel E150d), Phosphoric Acid, Natural Flavourings including Caffeine.",
                "JadIngredientsHTML": "Carbonated Water, Sugar, Colour (Caramel E150d), Phosphoric Acid, Natural Flavourings including Caffeine.",
                "JJeOrigin": "United Kingdom",
                "JadCatchOrigin": "",
                "JJeProductDescription": "",
                "JJeSellingPoints": "Coca Cola\nCocaCola",
                "JJeShelfLife": 135,
                "JJeSizeOrCut": "6x1.5L",
                "JadAllergensDeclaration": "",
                "JadStorageCondition": "Store cool and dry.",
                "JJeEnergyKJ": 180.0,
                "JJeEnergyKCAL": 42.0,
                "JJeFatG": "0",
                "JadFatSaturatesG": "0",
                "JJeCarbohydrateG": "10.6",
                "JadCarbohydrateSugarsG": "10.6",
                "JJeProteinG": "0",
                "JadSodiumG": "0",
                "IsAgeRestriction": false,
                "IsHalal": false,
                "IsVegan": true,
                "IsVegeterian": true
            }
        ]
    },
    "_links": {
        "maintenance-message": {
            "href": "[]"
        },
        "announcement-message": {
            "href": "[]"
        }
    },
    "page": {
        "size": 12,
        "totalElements": 18,
        "totalPages": 2,
        "number": 0
    }
}
Peter B
  • 22,460
  • 5
  • 32
  • 69
Alan P
  • 41
  • 8
  • @JaromandaX sorry i forgot to mention i receive the same data but JSON in my class. stack overflow wouldn't allow me to show the whole JSON text, thanks:) – Alan P Nov 04 '20 at 22:26
  • @JaromandaX ill add some json – Alan P Nov 04 '20 at 22:27
  • @JaromandaX thank you, i didnt realise you could do that, im new to JSON and fairly new to web scraping – Alan P Nov 04 '20 at 22:28
  • @JaromandaX ive hit a carachter limit sorry. the main issue im having is not knowing how to use the Newtonsoft.Json package. the tutorials ive searched for all dont seem to go into how to get arrays within an array – Alan P Nov 04 '20 at 22:33
  • thanks, ill add your link to the post – Alan P Nov 04 '20 at 22:34
  • 1
    you problem isn't arrays within arrays ... the problem is JSON within JSON ... the problem is that `categoryList` is just a String (as JSON format) so you have to parse that value after parsing the whole JSON – Jaromanda X Nov 04 '20 at 22:36
  • @JaromandaX thanks, i see what you mean. ill give it a go. thanks for you help, i apreciate it – Alan P Nov 04 '20 at 22:38
  • There are great free tools to generate C# classes from JSON, that you can then use for the deserialization process. I have good experiences with this one: https://app.quicktype.io/?l=csharp – Peter B Nov 04 '20 at 22:49
  • As mentioned above `categoryList` is double-serialized JSON inside JSON. If you want to automatically deserialize it to a `List` for some appropriate type `Category` inside some data model, you can use [tag:json.net] with `EmbeddedLiteralConverter>` from [this answer](https://stackoverflow.com/a/39154630/3744182) to [How do I convert an escaped JSON string within a JSON object?](https://stackoverflow.com/q/39154043/3744182). – dbc Nov 04 '20 at 22:56
  • 1
    @dbc thanks, as i only needed 6 values i decided to cast the objects into a dynamic data type so i didnt have to account for all the feilds – Alan P Nov 05 '20 at 00:09

1 Answers1

0
        public static List<JJs.ITEMS> JJSGetProductHTML(String Term)
    {
        string url = "https://www.website.com/api/product-search-agg/api/v1/product/websearch?b=DG-MW&page=0&q=" + Term+ "&size=12&sortType=search&format=json";
        WebClient WC = new WebClient();
        string JSON = WC.DownloadString(url);
        var onject,= JsonConvert.DeserializeObject<dynamic>(JSON);
        string s = onject._embedded.products[0].ToString();
        List<JJs.ITEMS> products = new List<JJs.ITEMS>();
        foreach (var m in onject._embedded.products)
        {
            JJs.ITEMS newitem = new JJs.ITEMS();
            newitem.Name = m.jsonFriendlyItemName.ToString();
            newitem.itemId = m.itemId.ToString();
            newitem.price = m.collection.price.ToString();
            try
            { m.offer.ToString(); newitem.Promoend = m.offer.promoEnd.ToString(); newitem.Promo = true; } 
            catch 
            { newitem.Promo = false; };
            products.Add(newitem);
        }

        return products;
    }

As I didnt want all the feilds, i serialised it then used ".jsonFriendlyItemName",".itemId".. to only get the values I was looking for

thanks @JaromandaX, @ Peter B and @dbc for your help

Alan P
  • 41
  • 8