2

Magento 2 REST API returns product search results (V1/products) for custom attributes in the following way by default (exemplary product):

{
  ...
  "custom_attributes": [
    {
      "attribute_code": "description",
      "value": "<p>The sporty Joust Duffle Bag can't be beat - not in the gym, not on the luggage carousel, not anywhere. Big enough to haul a basketball or soccer ball and some sneakers with plenty of room to spare, it's ideal for athletes with places to go.<p>\n<ul>\n<li>Dual top handles.</li>\n<li>Adjustable shoulder strap.</li>\n<li>Full-length zipper.</li>\n<li>L 29\" x W 13\" x H 11\".</li>\n</ul>"
    },
    {
      "attribute_code": "image",
      "value": "/m/b/mb01-blue-0.jpg"
    },
    {
      "attribute_code": "small_image",
      "value": "/m/b/mb01-blue-0.jpg"
    },
    {
      "attribute_code": "thumbnail",
      "value": "/m/b/mb01-blue-0.jpg"
    },
    {
      "attribute_code": "required_options",
      "value": "0"
    },
    {
      "attribute_code": "has_options",
      "value": "0"
    },
    {
      "attribute_code": "url_key",
      "value": "joust-duffle-bag"
    }
  ]
}

What to do if I want to return it in the following way:

{
  ...
  "custom_attributes": [
      "description": "value": "<p>The sporty Joust Duffle Bag can't be beat - not in the gym, not on the luggage carousel, not anywhere. Big enough to haul a basketball or soccer ball and some sneakers with plenty of room to spare, it's ideal for athletes with places to go.<p>\n<ul>\n<li>Dual top handles.</li>\n<li>Adjustable shoulder strap.</li>\n<li>Full-length zipper.</li>\n<li>L 29\" x W 13\" x H 11\".</li>\n</ul>",
      "image": "/m/b/mb01-blue-0.jpg",
      "small_image": "/m/b/mb01-blue-0.jpg",
      "thumbnail": "/m/b/mb01-blue-0.jpg",
      "required_options": "0",
      "has_options": "0",
      "url_key": "joust-duffle-bag"
  ]
}

or:

{
  ...
  "description": "value": "<p>The sporty Joust Duffle Bag can't be beat - not in the gym, not on the luggage carousel, not anywhere. Big enough to haul a basketball or soccer ball and some sneakers with plenty of room to spare, it's ideal for athletes with places to go.<p>\n<ul>\n<li>Dual top handles.</li>\n<li>Adjustable shoulder strap.</li>\n<li>Full-length zipper.</li>\n<li>L 29\" x W 13\" x H 11\".</li>\n</ul>",
  "image": "/m/b/mb01-blue-0.jpg",
  "small_image": "/m/b/mb01-blue-0.jpg",
  "thumbnail": "/m/b/mb01-blue-0.jpg",
  "required_options": "0",
  "has_options": "0",
  "url_key": "joust-duffle-bag"
}

I will be grateful for working solutions or any clues.

3

you can achieve this by creating your own module.

  • Create an endpoint in 'webapi.xml'
  • implement your own repository
  • Create custom Interface thats holds data structure
  • Repository method ie: getList must return your Interface[]

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.