Error with REST Response - handling base64 images

All,

We are consuming a rest API which returns a list of image URL's. 

An example response format is below:

"products": [
      {
        "name": "Details about   Crushed Velvet Fabric Faux Leather 2 Seater Sofa Modern Design Living Office",
        "description": "Details about   Crushed Velvet Fabric Faux Leather 2 Seater Sofa Modern Design Living Office",
        "images": [
          "https://i.ebayimg.com/images/g/G8gAAOSwULNZfwWb/s-l300.jpg",
          "https://i.ebayimg.com/images/g/G8gAAOSwULNZfwWb/s-l300.jpg"
        ],
        "type": "Product",
        "offers": [{
          "price": "159.99",
          "currency": "GBP",
          "availability": "https://schema.org/InStock"
        }]
      }


In some instances we receive base 64 encoded images instead of URL's, for example in the following response:


"products":[
         {
            "name":"Titleist 718 AP3 Irons",            "images":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=",
            "description":"Titleist's AP3 truly is the best of everything! :",
            "type":"Product",
            "offers":[
               {
                  "currency":"GBP",
                  "availability":"http://schema.org/InStock"
               },
               {
                  "currency":"GBP",
                  "availability":"http://schema.org/InStock"
               }
            ]
         }]

}


This is leading to the parsing error:


Failed to deserialize JSON to GetHttpsResponse4:Error converting value "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=" to type 'System.String[]'. Path 'hybridGraph.products[0].images', line 1, position 698.


I'm guessing there might be a way to use the AdvancedResponse method to try and "fix" the response, but i'm not sure how to do this, or if it is feasible?

Can anybody offer any help?


Thanks

Tom

Hi, if you pasted the JSON properly, then I bet the error is in the images section not being a valid JSON list:

- First JSON:

"images": [
          "https://i.ebayimg.com/images/g/G8gAAOSwULNZfwWb/s-l300.jpg",
          "https://i.ebayimg.com/images/g/G8gAAOSwULNZfwWb/s-l300.jpg"
        ],

- Second JSON:

 "images":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=",


Note that in the first JSON sample you have the square brackets, indicating that you have a list. In the second sample, there are no square brackets.

Pedro,

Thanks for the quick reply. 

It seems that the API response returns a list [] if there are multiple images, but doesn't if it is a single item? Not sure if this is because of the presence of a base64 image instead of a regular url or the fact its only found one image. I can look further and have emailed the provider.


In either case - what's the best way of catching these issues and "fixing" the problem before the parser kicks in?


Thanks

Tom


Hi Tom, 

Well If it is indeed related to the number of images returned, then I would suggest that in the "OnAfterRequest" event of the API (assuming you are using via the Service Studio Rest integration), you try to find out how many images you are getting: 

If it's just the one, de-serialize it into a single image JSON

If it's more than one, de-serialize it into a multi-image JSON


Now this it's just some general guidelines for this case as I don't know how the service is configured, so you might need to do some tweaks, or not needing it at all.

Pedro - thanks i will give it a go. 

Do you think it would make sense to build something in c# to do this via a node based approach, or try and rely on text functions and or SQL to identify the []'s and go down that route?


Thanks

Tom

I think it might be possible just to search for the literal "images : [" string using the Actions from the Text Extension, if it returns -1, it's just one image if it returns different than -1, it's more than one images.

Hi Tom,

What I personally do in these cases (we've consumed an API that has the same basic problem) is "fixing" the JSON in the OnAfterRequest. In this case this means replacing the '"images":"xxx",' by '"images":["xxx"],'. You can use the basic built-in Text manipulation Functions (like Replace), or perhaps the regular expression Actions from the Text Extension.

Thanks both - ended up building some global actions to parse and correct the JSON as it had similar issues in different places.

Working now, thank you!

Hi Tom,

Great to hear you got it working! If there's any of our posts that specifically put you in the right direction, please mark it as the Solution, thanks!