Skip to content

Commit a6c359e

Browse files
akoclaude
andcommitted
docs: add Countries REST API example to mapping skill
Realistic end-to-end example: single country (flat JSON, shared entity for import/export), country list (array, different domain models for import vs export), and a fetch→import→export microflow pipeline. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent dc0f823 commit a6c359e

1 file changed

Lines changed: 197 additions & 0 deletions

File tree

.claude/skills/mendix/json-structures-and-mappings.md

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,203 @@ DROP EXPORT MAPPING Module.Name;
306306

307307
---
308308

309+
## Export Workflow: PE → NPE → JSON
310+
311+
Export mappings work on non-persistent entity (NPE) structures that mirror the target JSON. When the source data is in persistent entities (PE) in the database, the typical workflow is:
312+
313+
1. **Retrieve** persistent data from the database
314+
2. **Build NPE tree** in a microflow: create NPE objects, set attributes, link via associations to match the JSON structure
315+
3. **Export to mapping** to serialize the NPE tree to JSON
316+
317+
```sql
318+
-- Example: build NPE tree from persistent Order data, then export
319+
CREATE MICROFLOW Module.ExportOrder ($Order: Module.Order)
320+
RETURNS String AS $Json
321+
BEGIN
322+
-- Build the NPE tree matching the JSON structure
323+
$Root = CREATE Module.ExRoot (OrderId = $Order/OrderId);
324+
325+
RETRIEVE $Customer FROM $Order/Module.Order_Customer;
326+
$ExCust = CREATE Module.ExCustomer (Name = $Customer/Name, Email = $Customer/Email);
327+
-- Link customer to root...
328+
329+
-- Export
330+
$Json = EXPORT TO MAPPING Module.EMM_Order($Root);
331+
RETURN $Json;
332+
END;
333+
/
334+
```
335+
336+
### Shortcut with View Entities
337+
338+
View Entities (OQL-backed) can retrieve data directly into the export-ready structure, skipping the manual NPE assembly:
339+
340+
```sql
341+
CREATE VIEW ENTITY Module.ExOrderView (
342+
OrderId: Integer,
343+
CustomerName: String,
344+
CustomerEmail: String
345+
) AS SELECT o.OrderId, c.Name, c.Email
346+
FROM Module.Order o
347+
JOIN Module.Order_Customer/Module.Customer c;
348+
```
349+
350+
This can reduce the microflow to a single retrieve + export step.
351+
352+
---
353+
354+
## Realistic Example: Countries REST API
355+
356+
A complete example consuming a Countries REST API, importing the response, and
357+
exporting country data back to JSON.
358+
359+
### Step 1: JSON Structures
360+
361+
```sql
362+
-- Single country (flat object)
363+
CREATE JSON STRUCTURE Integration.JSON_Country
364+
SNIPPET '{"name": "Netherlands", "officialName": "Kingdom of the Netherlands", "capital": "Amsterdam", "region": "Europe", "population": 18100436, "flagUrl": "https://flagcdn.com/w320/nl.png"}';
365+
366+
-- List of countries (array of objects)
367+
CREATE JSON STRUCTURE Integration.JSON_CountryList
368+
SNIPPET '[{"name": "Netherlands", "capital": "Amsterdam", "region": "Europe", "population": 18100436}]';
369+
```
370+
371+
### Step 2: Import — Single Country
372+
373+
```sql
374+
CREATE NON-PERSISTENT ENTITY Integration.Country (
375+
Name: String,
376+
OfficialName: String,
377+
Capital: String,
378+
Region: String,
379+
Population: Integer,
380+
FlagUrl: String
381+
);
382+
/
383+
384+
CREATE IMPORT MAPPING Integration.IMM_Country
385+
WITH JSON STRUCTURE Integration.JSON_Country
386+
{
387+
CREATE Integration.Country {
388+
Name = name,
389+
OfficialName = officialName,
390+
Capital = capital,
391+
Region = region,
392+
Population = population,
393+
FlagUrl = flagUrl
394+
}
395+
};
396+
```
397+
398+
### Step 3: Import — List of Countries
399+
400+
For a list response, the import mapping maps the array item directly (no container):
401+
402+
```sql
403+
CREATE NON-PERSISTENT ENTITY Integration.CountryListItem (
404+
Name: String,
405+
Capital: String,
406+
Region: String,
407+
Population: Integer
408+
);
409+
/
410+
411+
CREATE IMPORT MAPPING Integration.IMM_CountryList
412+
WITH JSON STRUCTURE Integration.JSON_CountryList
413+
{
414+
CREATE Integration.CountryListItem {
415+
Name = name,
416+
Capital = capital,
417+
Region = region,
418+
Population = population
419+
}
420+
};
421+
```
422+
423+
### Step 4: Export — Serialize Country to JSON
424+
425+
For the flat country, the same entity works for both import and export:
426+
427+
```sql
428+
CREATE EXPORT MAPPING Integration.EMM_Country
429+
WITH JSON STRUCTURE Integration.JSON_Country
430+
{
431+
Integration.Country {
432+
name = Name,
433+
officialName = OfficialName,
434+
capital = Capital,
435+
region = Region,
436+
population = Population,
437+
flagUrl = FlagUrl
438+
}
439+
};
440+
```
441+
442+
### Step 5: Export — List of Countries
443+
444+
For exporting a list, the export domain model needs a root container + item entities:
445+
446+
```sql
447+
-- Container entity wrapping the array
448+
CREATE NON-PERSISTENT ENTITY Integration.ExCountryList;
449+
/
450+
451+
-- Item entity for each country in the array
452+
CREATE NON-PERSISTENT ENTITY Integration.ExCountryItem (
453+
Name: String,
454+
Capital: String,
455+
Region: String,
456+
Population: Integer
457+
);
458+
/
459+
460+
CREATE ASSOCIATION Integration.ExCountryItem_ExCountryList
461+
FROM Integration.ExCountryItem
462+
TO Integration.ExCountryList;
463+
/
464+
465+
CREATE EXPORT MAPPING Integration.EMM_CountryList
466+
WITH JSON STRUCTURE Integration.JSON_CountryList
467+
{
468+
Integration.ExCountryList {
469+
Integration.ExCountryItem_ExCountryList/Integration.ExCountryItem AS Root {
470+
name = Name,
471+
capital = Capital,
472+
region = Region,
473+
population = Population
474+
}
475+
}
476+
};
477+
```
478+
479+
### Step 6: Microflow — Fetch, Import, Process, Export
480+
481+
```sql
482+
CREATE MICROFLOW Integration.GetCountryInfo ()
483+
RETURNS String AS $Json
484+
BEGIN
485+
-- Fetch country data from REST API
486+
$Response = REST CALL GET 'https://restcountries.com/v3.1/name/netherlands'
487+
HEADER Accept = 'application/json'
488+
TIMEOUT 30
489+
RETURNS String
490+
ON ERROR CONTINUE;
491+
492+
-- Import JSON into entity
493+
$Country = IMPORT FROM MAPPING Integration.IMM_Country($Response);
494+
495+
-- Export back to our own JSON format
496+
$Json = EXPORT TO MAPPING Integration.EMM_Country($Country);
497+
LOG INFO NODE 'Integration' 'Country: ' + $Json;
498+
499+
RETURN $Json;
500+
END;
501+
/
502+
```
503+
504+
---
505+
309506
## Common Mistakes
310507

311508
| Mistake | Fix |

0 commit comments

Comments
 (0)