Unable to combine all my JSON from different calls

Hello,

I have succesfully uploaded about 5000 products which i got from an API call…
I have used a runner because otherwise i get an error 500 because of the overload…

I used this as test script in my Getproducts call…

var jsonData = pm.response.json();
pm.environment.set("productData", JSON.stringify(jsonData));

In my Post call to add the products in batches to woocommerce i used this pre-request-script:

var productData = pm.environment.get("productData");

if (productData) {
    var products = JSON.parse(productData);
    var batchSize = 100; // Batchgrootte instellen
    var startIndex = pm.environment.get("startIndex") || 0; // Haal de startindex op uit de omgevingsvariabele of gebruik 0

    var batchProducts = products.slice(startIndex, startIndex + batchSize);

    var createdProducts = [];

    for (var i = 0; i < batchProducts.length; i++) {
        var product = batchProducts[i];

        if (product.statusgroup === "New") {

            var postBody = {
                "name": product.title,
                "description": product.longdescription,
                "short_description": product.description,
                "sku": product.itemcode,
                "regular_price": product.suggestprice.toString(),
                "manage_stock": true,
                "stock_quantity": product.stockfree + product.externalstock,
                "backorders": (product.backorder === 1) ? "notify" : "no",
                "categories": [
                    {
                        "name": product.categoryname2
                    }
                ],
                "images": [
                    {
                        "src": product.productimage
                    }
                ]
            };

            createdProducts.push(postBody);
        }
    }

    if (createdProducts.length > 0) {
        // Pas de aanroep aan voor batchverwerking
        pm.request.url = 'https://xxxxxxxxx/wp-json/wc/v3/products/batch';
        pm.request.method = 'POST';
        pm.request.header = {
            'Content-Type': 'application/json',
            'Authorization': 'Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
        };
        pm.request.body.raw = JSON.stringify({ "create": createdProducts });

        // Update de startindex voor de volgende batch
        var nextIndex = startIndex + batchSize;
        pm.environment.set("startIndex", nextIndex);
    } else {
        console.log("Geen nieuwe producten om toe te voegen.");
    }

} else {
    console.error("productData is niet beschikbaar in de omgevingsvariabele.");
}

Apart from the categories, the sku, prices and stock all is being added nicely… If someone could tell me how to fix the categories i would be gratefull… I tried to use toString() and parseInt() but it made no difference…

The next problem i am facing is I am using a seperate call “Getproductimages” which gives me back a JSON like this:

[
    {
        "mediaparentid": 8509838,
        "itemcode": "0318C001",
        "versions": [
            {
                "mediaid": 8510448,
                "url": "https://productimages.nl/media/image/high/9bfc9ab88f9e32357af0ab85b21bc0c71c19dec9d782b60c3d4c5701222108ae.jpg",
                "quality": "high",
                "filetype": "jpg",
                "filesize": 62347,
                "width": 1000,
                "height": 1000,
                "checksum": "9bfc9ab88f9e32357af0ab85b21bc0c71c19dec9d782b60c3d4c5701222108ae"
            },
            {
                "mediaid": 8510446,
                "url": "https://productimages.nl/media/image/low/a9318850c82328085aa3cad96b7e5c6f4acfc4fcf7c9a704269130838cddb060.jpg",
                "quality": "low",
                "filetype": "jpg",
                "filesize": 4606,
                "width": 200,
                "height": 200,
                "checksum": "a9318850c82328085aa3cad96b7e5c6f4acfc4fcf7c9a704269130838cddb060"
            },
            {
                "mediaid": 8510447,
                "url": "https://productimages.nl/media/image/medium/e8ef719f4a232a5463fce0d9b61ad395ec9ac57a7a995d49739e1ccb015e5846.jpg",
                "quality": "medium",
                "filetype": "jpg",
                "filesize": 18764,
                "width": 500,
                "height": 500,
                "checksum": "e8ef719f4a232a5463fce0d9b61ad395ec9ac57a7a995d49739e1ccb015e5846"
            },
            {
                "mediaid": 8510445,
                "url": "https://productimages.nl/media/image/thumb/b8aeee6abb32bafe38979d7901897025ce5b5366711c062daeea912a4e023895.jpg",
                "quality": "thumb",
                "filetype": "jpg",
                "filesize": 1304,
                "width": 75,
                "height": 75,
                "checksum": "b8aeee6abb32bafe38979d7901897025ce5b5366711c062daeea912a4e023895"
            }
        ]
    },

I want to add the pictures which has the quality: “high” to be added to the products… The products are allready created, the itemcode from the productimages could be used to correspond with the SKU in woocommerce… Would it be better to do this before i create the products and add the images upon creating the products? And can’t this to work and some help would be very awesome!

First of all, what is wrong with the categories?

Batch update products – WooCommerce REST API Documentation - WP REST API v3

It looks like the categories should have an ID element, not “name” like you have in your “postBody”.

"categories": [
    {
        "id": 11
    },
    {
        "id": 13
    }
],

You can also simplify some of your code.

Most of this code block is not needed.

pm.request.url = 'https: //xxxxxxxxx/wp-json/wc/v3/products/batch';
pm.request.method = 'POST';
pm.request.header = {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
};
pm.request.body.raw = JSON.stringify({"create": createdProducts});

The URL, request method and header appears to be static, so I can’t see why you would need to update this in your pre-request script. Just make sure its set correctly in the request.

Updating the body via ‘pm.request.body.raw’ is probably the only part you need here.

I can’t see where you have a loop. It would appear that the API supports pagination, so you should be able to read the response, and find out if there are more items and keep looping using setNextRequest() in the tests tab.

Pagination – WooCommerce REST API Documentation - WP REST API v3

If you have to make a separate call to Getproductimages then this is going to get a bit (or a lot) more complex.

I can’t tell which call that is in the API spec.

I can’t see any way of doing this without looping through your products. Either before or after.

If the getproductimages is a single call at a time, then it means looping through 5000 records.

Ideally this information would already be contained in your productData.

Are you sure the images is not contained in the initial getallproducts call?

Do you have an example response for that first request? (Just one of the product items).

Hello Mike and thanks for the reply…

I tried to add the categories with “categoryid2” from the JSON response (getproducts) as well… I made sure the categories in Woocommerce got the category id which is the same as the id’s in the JSON…

Here is a JSON response for 1 product from the getproducts call:

[
    {
        "itemcode": "0318C001",
        "title": "Canon 0318C001 inktcartridge 1 stuk(s) Origineel Hoog (XL) rendement Zwart",
        "longtitle": "Canon 0318C001 inktcartridge 1 stuk(s) Origineel Hoog (XL) rendement Zwart",
        "description": "Canon 0318C001 inktcartridge 1 stuk(s) Origineel Hoog (XL) r",
        "brandcode": "P3",
        "brand": "Canon",
        "groupcode": "A13",
        "group": "Canon Cartridge",
        "statusgroupcode": "10009",
        "statusgroup": "New",
        "price": 13.004,
        "chargeprice": 0,
        "totalprice": 13.004,
        "suggestprice": 18.59,
        "barcode": "4549292032826",
        "status": "A",
        "hasflyeroffer": 0,
        "hasspecialoffer": 0,
        "deliverydate": "2023-09-04T00:00:00+02:00",
        "stockfree": 0,
        "backorder": 0,
        "externalstock": 1,
        "categoryid1": 21,
        "categoryname1": "Printers",
        "categoryid2": 377,
        "categoryname2": "Inktcartridges",
        "icecatproductid": 29627554,
        "productimage": "https://productimages.nl/media/image/low/a9318850c82328085aa3cad96b7e5c6f4acfc4fcf7c9a704269130838cddb060.jpg",
        "productvideo": "",
        "longdescription": "Deze zwarte pigmentinkt met hoog rendement levert tot 1,6x meer afdrukken op dan het standaardequivalent. De pigmentzwarte inkt wordt gebruikt voor het afdrukken van documenten en zorgt voor haarscherpe tekst met een grote levensduur. Met de inkttank van 22 ml kun je tot 500 pagina's A4-kleurendocumenten afdrukken.",
        "warrantyinfo": "1 jaar via Complies",
        "bolcommissionpercentage": 5,
        "bolcommissionfixedamount": 0.48
    },

This output has 1 image added to it, but some products have more images (different angles i.e.)

I tried use a PUT with the getproductpictures call but somehow i cant get it to work and was so frustrated i had to quit trying :stuck_out_tongue:

My recommendation for the categories is to submit a single one until you get the format correct.

The API documentation has an example and its using the ID. So as long as the ID’s match, it should work.

If you can’t get the additional pictures with your initial call of all products than the only option would be to loop through the productData response and make individual calls to the getproductimages API using sendRequest and update the productData before resaving it as an environment variable.

Is there another call to the API where you can get all of the image data in a single call. (You might need to speak to the supplier on this).

Sort of defeats the purpose of pagination that they have elsewhere in the API if you then have to loop through all of the records to get this extra information.

If i make the call: getproductpictures i get all the main images from the products. The JSON (5,56MB) looks like this (i have only copied the json of 1 product):

[
    {
        "mediaparentid": 8509838,
        "itemcode": "0318C001",
        "versions": [
            {
                "mediaid": 8510448,
                "url": "https://productimages.nl/media/image/high/9bfc9ab88f9e32357af0ab85b21bc0c71c19dec9d782b60c3d4c5701222108ae.jpg",
                "quality": "high",
                "filetype": "jpg",
                "filesize": 62347,
                "width": 1000,
                "height": 1000,
                "checksum": "9bfc9ab88f9e32357af0ab85b21bc0c71c19dec9d782b60c3d4c5701222108ae"
            },
            {
                "mediaid": 8510446,
                "url": "https://productimages.nl/media/image/low/a9318850c82328085aa3cad96b7e5c6f4acfc4fcf7c9a704269130838cddb060.jpg",
                "quality": "low",
                "filetype": "jpg",
                "filesize": 4606,
                "width": 200,
                "height": 200,
                "checksum": "a9318850c82328085aa3cad96b7e5c6f4acfc4fcf7c9a704269130838cddb060"
            },
            {
                "mediaid": 8510447,
                "url": "https://productimages.nl/media/image/medium/e8ef719f4a232a5463fce0d9b61ad395ec9ac57a7a995d49739e1ccb015e5846.jpg",
                "quality": "medium",
                "filetype": "jpg",
                "filesize": 18764,
                "width": 500,
                "height": 500,
                "checksum": "e8ef719f4a232a5463fce0d9b61ad395ec9ac57a7a995d49739e1ccb015e5846"
            },
            {
                "mediaid": 8510445,
                "url": "https://productimages.nl/media/image/thumb/b8aeee6abb32bafe38979d7901897025ce5b5366711c062daeea912a4e023895.jpg",
                "quality": "thumb",
                "filetype": "jpg",
                "filesize": 1304,
                "width": 75,
                "height": 75,
                "checksum": "b8aeee6abb32bafe38979d7901897025ce5b5366711c062daeea912a4e023895"
            }
        ]
    },

If i make the call getgallerypictures i get a list of product- and galleryimages of the products… The JSON (29,3MB) looks like this (again only copies 1 product):

[
    {
        "mediaparentid": 8509838,
        "itemcode": "0318C001",
        "order": 0,
        "isproductpicture": 1,
        "versions": [
            {
                "mediaid": 8510448,
                "url": "https://productimages.nl/media/image/high/9bfc9ab88f9e32357af0ab85b21bc0c71c19dec9d782b60c3d4c5701222108ae.jpg",
                "quality": "high",
                "filetype": "jpg",
                "filesize": 62347,
                "width": 1000,
                "height": 1000,
                "checksum": "9bfc9ab88f9e32357af0ab85b21bc0c71c19dec9d782b60c3d4c5701222108ae"
            },
            {
                "mediaid": 8510446,
                "url": "https://productimages.nl/media/image/low/a9318850c82328085aa3cad96b7e5c6f4acfc4fcf7c9a704269130838cddb060.jpg",
                "quality": "low",
                "filetype": "jpg",
                "filesize": 4606,
                "width": 200,
                "height": 200,
                "checksum": "a9318850c82328085aa3cad96b7e5c6f4acfc4fcf7c9a704269130838cddb060"
            },
            {
                "mediaid": 8510447,
                "url": "https://productimages.nl/media/image/medium/e8ef719f4a232a5463fce0d9b61ad395ec9ac57a7a995d49739e1ccb015e5846.jpg",
                "quality": "medium",
                "filetype": "jpg",
                "filesize": 18764,
                "width": 500,
                "height": 500,
                "checksum": "e8ef719f4a232a5463fce0d9b61ad395ec9ac57a7a995d49739e1ccb015e5846"
            },
            {
                "mediaid": 8510445,
                "url": "https://productimages.nl/media/image/thumb/b8aeee6abb32bafe38979d7901897025ce5b5366711c062daeea912a4e023895.jpg",
                "quality": "thumb",
                "filetype": "jpg",
                "filesize": 1304,
                "width": 75,
                "height": 75,
                "checksum": "b8aeee6abb32bafe38979d7901897025ce5b5366711c062daeea912a4e023895"
            }
        ]
    },

I have 2 options for the pictures…

If i make the call getproductpictures i get a list of the main images of the products. The JSON (5,56MB) looks like this (i have only copied 1 product):

[
    {
        "mediaparentid": 8509838,
        "itemcode": "0318C001",
        "versions": [
            {
                "mediaid": 8510448,
                "url": "https://productimages.nl/media/image/high/9bfc9ab88f9e32357af0ab85b21bc0c71c19dec9d782b60c3d4c5701222108ae.jpg",
                "quality": "high",
                "filetype": "jpg",
                "filesize": 62347,
                "width": 1000,
                "height": 1000,
                "checksum": "9bfc9ab88f9e32357af0ab85b21bc0c71c19dec9d782b60c3d4c5701222108ae"
            },
            {
                "mediaid": 8510446,
                "url": "https://productimages.nl/media/image/low/a9318850c82328085aa3cad96b7e5c6f4acfc4fcf7c9a704269130838cddb060.jpg",
                "quality": "low",
                "filetype": "jpg",
                "filesize": 4606,
                "width": 200,
                "height": 200,
                "checksum": "a9318850c82328085aa3cad96b7e5c6f4acfc4fcf7c9a704269130838cddb060"
            },
            {
                "mediaid": 8510447,
                "url": "https://productimages.nl/media/image/medium/e8ef719f4a232a5463fce0d9b61ad395ec9ac57a7a995d49739e1ccb015e5846.jpg",
                "quality": "medium",
                "filetype": "jpg",
                "filesize": 18764,
                "width": 500,
                "height": 500,
                "checksum": "e8ef719f4a232a5463fce0d9b61ad395ec9ac57a7a995d49739e1ccb015e5846"
            },
            {
                "mediaid": 8510445,
                "url": "https://productimages.nl/media/image/thumb/b8aeee6abb32bafe38979d7901897025ce5b5366711c062daeea912a4e023895.jpg",
                "quality": "thumb",
                "filetype": "jpg",
                "filesize": 1304,
                "width": 75,
                "height": 75,
                "checksum": "b8aeee6abb32bafe38979d7901897025ce5b5366711c062daeea912a4e023895"
            }
        ]
    },

When i use the call getgalleryproducts i get a of product- and galleryimages of the products. The JSON (29,3MB) looks like this (again i only copied 1 product):

[
    {
        "mediaparentid": 8509838,
        "itemcode": "0318C001",
        "order": 0,
        "isproductpicture": 1,
        "versions": [
            {
                "mediaid": 8510448,
                "url": "https://productimages.nl/media/image/high/9bfc9ab88f9e32357af0ab85b21bc0c71c19dec9d782b60c3d4c5701222108ae.jpg",
                "quality": "high",
                "filetype": "jpg",
                "filesize": 62347,
                "width": 1000,
                "height": 1000,
                "checksum": "9bfc9ab88f9e32357af0ab85b21bc0c71c19dec9d782b60c3d4c5701222108ae"
            },
            {
                "mediaid": 8510446,
                "url": "https://productimages.nl/media/image/low/a9318850c82328085aa3cad96b7e5c6f4acfc4fcf7c9a704269130838cddb060.jpg",
                "quality": "low",
                "filetype": "jpg",
                "filesize": 4606,
                "width": 200,
                "height": 200,
                "checksum": "a9318850c82328085aa3cad96b7e5c6f4acfc4fcf7c9a704269130838cddb060"
            },
            {
                "mediaid": 8510447,
                "url": "https://productimages.nl/media/image/medium/e8ef719f4a232a5463fce0d9b61ad395ec9ac57a7a995d49739e1ccb015e5846.jpg",
                "quality": "medium",
                "filetype": "jpg",
                "filesize": 18764,
                "width": 500,
                "height": 500,
                "checksum": "e8ef719f4a232a5463fce0d9b61ad395ec9ac57a7a995d49739e1ccb015e5846"
            },
            {
                "mediaid": 8510445,
                "url": "https://productimages.nl/media/image/thumb/b8aeee6abb32bafe38979d7901897025ce5b5366711c062daeea912a4e023895.jpg",
                "quality": "thumb",
                "filetype": "jpg",
                "filesize": 1304,
                "width": 75,
                "height": 75,
                "checksum": "b8aeee6abb32bafe38979d7901897025ce5b5366711c062daeea912a4e023895"
            }
        ]
    },

Would it be better to use the last call? And then save it in productData as well? Or is it better to save it in something new?

As a side note: Per image the combination of the fields mediaid, mediaparentid and itemcode are unique. There are multiple images per item (itemcode).

If getproductpictures has the pictures you want, and is 5.56mb in size then I would use this (as the other file size is 29.3mb).

If you want to combine this with getproducts, then you are going to have to loop through one or the other list.

It sounds like you should run getproductpictures first and store the result in an collection or environment variable.

You then need to run your getproducts request and you will need to loop through the response, updating the url for the picture before saving that to its own collection\environment variable.

I’m assuming you will want to replace the productimage element in getproducts with the url from getproductpictures.

That can be done with a JavaScript find function.

From there, I suspect you can then use your existing code, but might want to have a look at the pagination options.

Here is some example code that you can put in the tests tab for your getProducts request.

It’s based on you running the getProductPictures request first and storing the result in a collection\environment variable.

const response = pm.response.json();
console.log(response); // initial response

let getProductPictures = pm.collectionVariables.get("getProductPictures");
// console.log(getProductPictures);

response.forEach(product => {
    
    let itemcode = product.itemcode // itemcode for current iteration
    // console.log(itemcode);
    
    let search = getProductPictures.find(obj => {return obj.itemcode === itemcode}) // find itemcode in product pictures
    // console.log(search);
    
    let hq = search.versions.find(obj => {return obj.quality === 'high'}) // find the object with the high quality picture in
    // console.log(hq);
    
    let url = hq.url // retrieve the URL for the high quality picture
    // console.log(url);
    
    product.productimage = url // overwrite productimage with the high quality picture
});

console.log(response); // updated response

pm.collectionVariables.set("getProducts", response); // save to collection variable

This topic was automatically closed 45 days after the last reply. New replies are no longer allowed.