1

When dealing with the Magento v2 API, I faced a problem where getting product details have inconsistent data from REST API.

Example 1:

enter image description here

Example 2:

enter image description here

These are Note 8 and iPhone 8 data and we can see that the custom_attributes[10] is having different attribute code. The Note 8's custom_attrbutes[10] is url_key. The iPhone 8's custom_attributes[10] is country_of_manufacture.

Can i have some suggestions how to handle this kind of data?

0

You can convert the array to a different structure with the attribute codes as keys. How exactly depends on which language you are using to process the result.

In JavaScript you can use reduce:

var input = [
    {'attribute_code' : 'meta_title', 'value' : 'lorem'},
    {'attribute_code' : 'meta_keyword', 'value' : 'ipsum'},
];

var customAttributes = input.reduce(function(result, item) {
    result[item.attribute_code] = item.value;
    return result;
}, {});

This code creates an object by walking through all array items in input (the custom_attributes result from the API) and applying a function on it.

1
  • 1
    Isn't there anyway to get this directly in the result by altering the request? Something like /rest/V1/products?fields=items[sku,price,name,custom_attributes[my_custom_field]] (I know this doesn't work, it's an example)
    – Wietse
    Aug 9 '17 at 15:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.