Import mapping a JSON response which is of type array and contains objects which van be both array of objects or object.

0
I have a situation when consuming  api response for one of service, response is complex object and it contains nested objects which are further having objects or array of objects.    I am facing issue in mapping the response to domain model which was generated automatically using json structure & import mapping from response json. Few places I have many to one mapping for nested objects, like in below json I have categories which is array of object and categories is not owner side of relationship. For these scenarios I am not able to navigate inside categories mapping to retrieve categories list. This behaviour  is for all objects which are having array of objects. Below is screenshot and attached is json.       Import Screen screenshot..                     I want to get data from JsonObject_2_Categories relationship as mapped in below entity mapping from domains.   Below is sample json for reference.. { "limit": 20, "offset": 0, "count": 10, "total": 10, "results": [ { "id": "684668c7-1ff6-4734-917f-4bb45dfd3809", "version": 3, "productType": { "typeId": "product-type", "id": "0896cf7e-563c-4c3e-8a3c-dc7f234dc3b0" }, "name": { "en-US": "Ben Pillow Cover", "en-GB": "Ben Pillow Cover", "de-DE": "Kissenbezug \"Ben\"" }, "description": { "en-US": "The Ben Pillow Cover is a brown linen decorative pillowcase used to add visual interest and texture to a room. It is made from a natural linen fabric, which is known for its durability, breathability, and classic look. The pillowcase is designed to fit over a standard size rectangle decorative pillow. It features a neutral brown color that can complement a wide variety of decor styles and color schemes. The linen fabric has a soft and slightly textured surface that adds a cozy and inviting feel to the pillow. The edges of the pillowcase are typically finished with a neat hem, which adds a polished look to the overall design. The pillowcase is often used to add a touch of warmth and natural elegance to a room, whether it is placed on a bed, a sofa, or an accent chair. It can be used on its own or paired with other decorative pillows in different shapes and colors to create a layered and cohesive look. Overall, a brown linen decorative pillowcase is a versatile and timeless accent piece that can enhance the comfort and style of any room in the home.", "de-DE": "Der Kissenbezug \"Ben\" ist ein dekorativer Kissenbezug aus braunem Leinen. Er ist ein visueller Akzent in jedem Raum. Er besteht aus einem natürlichen Leinenstoff, der für seine Haltbarkeit, Atmungsaktivität und seinen klassischen Look bekannt ist. Der Kissenbezug ist so konzipiert, dass er über ein rechteckiges dekoratives Kissen in Standardgröße passt. Es verfügt über eine neutrale braune Farbe, die eine Vielzahl von Einrichtungsstilen und Farbschemata ergänzen kann. Der Leinenstoff hat eine weiche und leicht strukturierte Oberfläche, die dem Kissen ein gemütliches und einladendes Gefühl verleiht. Die Kanten des Kissenbezugs sind in der Regel mit einem sauberen Saum versehen, der dem Gesamtdesign einen eleganten Look verleiht. Der Kissenbezug wird oft verwendet, um einem Raum einen Hauch von Wärme und natürlicher Eleganz zu verleihen, egal ob er auf einem Bett, einem Sofa oder einem Akzentstuhl platziert wird. Es kann allein verwendet oder mit anderen dekorativen Kissen in verschiedenen Formen und Farben kombiniert werden, um einen mehrschichtigen und zusammenhängenden Look zu schaffen. Insgesamt ist ein dekorativer Kissenbezug aus braunem Leinen ein vielseitiges und zeitloses Akzentstück, das den Komfort und Stil jedes Raums im Haus verbessern kann." }, "categories": [ { "typeId": "category", "id": "3a8f2d21-5fc4-4c19-a60b-b8591ca41197" }, { "typeId": "category", "id": "1188948b-a961-4a1e-ace7-fab6f9b1bfd2" } ], "categoryOrderHints": {}, "slug": { "en-US": "ben-pillow-cover", "en-GB": "ben-pillow-cover", "de-DE": "ben-kissenbezug" }, "variants": [], "masterVariant": { "attributes": [ { "name": "productspec", "value": { "en-GB": "- Machine-washable\n- Cover is removable\n- Does not include pillow", "de-DE": "- Waschmaschinenfest\n- Bezug ist abnehmbar\n- Kissen nicht im Lieferumfang enthalten", "en-US": "- Machine-washable\n- Cover is removable\n- Does not include pillow" } }, { "name": "color-filter", "value": { "key": "#D2B48C", "label": { "de-DE": "Bräunen", "en-GB": "Tan", "en-US": "Tan" } } }, { "name": "color", "value": { "en-GB": "#A69465", "de-DE": "#A69465", "en-US": "#A69465" } }, { "name": "colorlabel", "value": { "en-GB": "Tan", "de-DE": "Bräunen", "en-US": "Tan" } } ], "availability": { "isOnStock": true, "availableQuantity": 100, "version": 1, "id": "778179e4-7a4a-4eb8-ad84-c63e731e4d92" }, "assets": [], "images": [ { "url": "https://storage.googleapis.com", "dimensions": { "w": 6240, "h": 4160 } } ], "prices": [ { "id": "fd565efc-3de4-4d45-a67e-62dfe9a87374", "value": { "type": "centPrecision", "currencyCode": "EUR", "centAmount": 1299, "fractionDigits": 2 }, "country": "DE" }, { "id": "25d345c9-935d-42d1-be8d-6f8aebea1028", "value": { "type": "centPrecision", "currencyCode": "GBP", "centAmount": 1299, "fractionDigits": 2 }, "country": "GB" }, { "id": "b18ee7c6-0ecc-40e3-bf52-28cb5d4ba3b3", "value": { "type": "centPrecision", "currencyCode": "USD", "centAmount": 1299, "fractionDigits": 2 }, "country": "US" } ], "sku": "LBPC-09", "id": 1 }, "searchKeywords": {}, "hasStagedChanges": false, "published": true, "key": "ben-pillow-cover", "taxCategory": { "typeId": "tax-category", "id": "7171c6bf-c075-4812-b9a6-fe9d5983393a" }, "createdAt": "2024-02-05T14:24:52.964Z", "lastModifiedAt": "2024-02-05T14:24:55.987Z" } ], "facets": {} }
asked
1 answers
1

The Mendix mapper has a tendency to create 'unnecessary' objects when faced with the scenario you present here. You can get rid of those objects by unticking the checkbox on the key that holds the array, in your case that would be the checkbox next to 'categories' just below the collapsed 'description' node. This will get rid of the empty Categories entity that serves no purpose in your data model.

 

The resulting domain model will have a 1-* association between JsonObject and JsonObject2. If you'd like to make those names more descriptive, you can rename them in the domain model, or, if you have not used map automatically yet, and no entities exist, you can use the 'Custom Name' column in the 'Select schema elements for import mapping' screen.

 

image.png

answered