Skip to content

Commit 0339bea

Browse files
committed
Updated rest-call-from-json
1 parent 2b2e0b8 commit 0339bea

1 file changed

Lines changed: 69 additions & 72 deletions

File tree

.claude/skills/mendix/rest-call-from-json.md

Lines changed: 69 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ JSON Structure → Non-persistent entities → Import Mapping → REST CALL micr
1616

1717
```sql
1818
CREATE JSON STRUCTURE Module.JSON_MyStructure
19-
SNIPPET '{"key": "value", "count": 1}';
19+
FROM '{"key": "value", "count": 1}';
2020
```
2121

2222
- The executor **formats** the snippet (pretty-print) then **refreshes** (derives element tree) automatically.
@@ -37,26 +37,29 @@ DESCRIBE JSON STRUCTURE Module.JSON_MyStructure;
3737
Derive one entity per JSON object type. Name them after what they represent (not after JSON keys).
3838

3939
```sql
40-
CREATE ENTITY Module.MyRootObject (NON_PERSISTENT)
41-
stringField : String
42-
intField : Integer
43-
decimalField : Decimal
44-
boolField : Boolean DEFAULT false;
45-
46-
CREATE ENTITY Module.MyNestedObject (NON_PERSISTENT)
47-
name : String
48-
code : String;
40+
CREATE OR REPLACE NON-PERSISTENT ENTITY Module.MyRootObject (
41+
stringField: String(0),
42+
intField: Integer,
43+
decimalField: Decimal,
44+
boolField: Boolean DEFAULT false
45+
);
46+
47+
CREATE OR REPLACE NON-PERSISTENT ENTITY Module.MyNestedObject (
48+
name: String(0),
49+
code: String(0)
50+
);
4951

5052
CREATE ASSOCIATION Module.MyRootObject_MyNestedObject
5153
FROM Module.MyRootObject
5254
TO Module.MyNestedObject;
5355
```
5456

5557
**Rules:**
56-
- All string fields: bare `String` (no length — unlimited)
58+
- All string fields: `String(0)` (unlimited). **Do NOT write `String(unlimited)`** — the grammar only accepts number literals. `String(0)` stores as unlimited and DESCRIBE outputs `String(unlimited)`.
5759
- All number fields: `Integer`, `Decimal`, or `Long` — remove defaults for optional fields
5860
- Boolean fields **require** `DEFAULT true|false`
59-
- `NON_PERSISTENT` — these entities are not stored in the database
61+
- `NON-PERSISTENT` (hyphen) before `ENTITY` — not `(NON_PERSISTENT)` after the name
62+
- Attributes go inside `(...)` comma-separated with colon separator: `name: Type`
6063
- One association per parent→child relationship; name it `Parent_Child`
6164

6265
---
@@ -145,108 +148,99 @@ Applies an import mapping to a string variable (JSON content) to produce entity
145148

146149
```sql
147150
-- With assignment (non-persistent entities, need the result in the flow)
148-
$PetResponse = IMPORT FROM MAPPING Module.IMM_Pet($JsonContent);
151+
$OrderResponse = IMPORT FROM MAPPING Module.IMM_Order($JsonContent);
149152

150153
-- Without assignment (persistent entities, just stores to DB)
151-
IMPORT FROM MAPPING Module.IMM_Pet($JsonContent);
154+
IMPORT FROM MAPPING Module.IMM_Order($JsonContent);
152155
```
153156

154157
### Export to mapping
155158

156159
Applies an export mapping to an entity object to produce a JSON string:
157160

158161
```sql
159-
$JsonOutput = EXPORT TO MAPPING Module.EMM_Pet($PetResponse);
162+
$JsonOutput = EXPORT TO MAPPING Module.EMM_Order($OrderResponse);
160163
```
161164

162165
### Complete import → process → export microflow
163166

164167
```sql
165-
CREATE MICROFLOW Module.ProcessPetData ()
168+
CREATE MICROFLOW Module.ProcessOrderData ()
166169
BEGIN
167170
DECLARE $ResponseContent String = $latestHttpResponse/Content;
168-
$PetResponse = IMPORT FROM MAPPING Module.IMM_Pet($ResponseContent);
171+
$OrderResponse = IMPORT FROM MAPPING Module.IMM_Order($ResponseContent);
169172
-- Process the imported data...
170-
$JsonOutput = EXPORT TO MAPPING Module.EMM_Pet($PetResponse);
173+
$JsonOutput = EXPORT TO MAPPING Module.EMM_Order($OrderResponse);
171174
LOG INFO NODE 'Integration' 'Exported: ' + $JsonOutput;
172175
END;
173176
/
174177
```
175178

176179
---
177180

178-
## Complete Example — Bible Verse API
181+
## Complete Example — Generic Nested API
182+
183+
JSON shape: a root object containing metadata fields and a nested `item` object.
179184

180185
```sql
181186
-- Step 1: JSON Structure
182-
CREATE JSON STRUCTURE Integrations.JSON_BibleVerse
183-
SNIPPET '{"translation":{"identifier":"web","name":"World English Bible","language":"English","language_code":"eng","license":"Public Domain"},"random_verse":{"book_id":"1SA","book":"1 Samuel","chapter":17,"verse":49,"text":"David put his hand in his bag, took a stone, and slung it."}}';
187+
CREATE JSON STRUCTURE MyModule.JSON_MyApi
188+
FROM '{"item":{"code":"ABC","label":"Example","count":42},"status":"OK","version":"1.0"}';
184189

185190
-- Step 2: Entities
186-
CREATE ENTITY Integrations.BibleApiResponse (NON_PERSISTENT);
187-
188-
CREATE ENTITY Integrations.BibleTranslation (NON_PERSISTENT)
189-
identifier : String
190-
name : String
191-
language : String
192-
language_code : String
193-
license : String;
194-
195-
CREATE ENTITY Integrations.BibleVerse (NON_PERSISTENT)
196-
book_id : String
197-
book : String
198-
chapter : Integer
199-
verse : Integer
200-
text : String;
201-
202-
CREATE ASSOCIATION Integrations.BibleApiResponse_BibleTranslation
203-
FROM Integrations.BibleApiResponse
204-
TO Integrations.BibleTranslation;
205-
206-
CREATE ASSOCIATION Integrations.BibleApiResponse_BibleVerse
207-
FROM Integrations.BibleApiResponse
208-
TO Integrations.BibleVerse;
191+
CREATE OR REPLACE NON-PERSISTENT ENTITY MyModule.MyApiResponse (
192+
ApiStatus: String(0),
193+
Version: String(0)
194+
);
195+
196+
CREATE OR REPLACE NON-PERSISTENT ENTITY MyModule.MyApiItem (
197+
Code: String(0),
198+
Label: String(0),
199+
Count: Integer
200+
);
201+
202+
CREATE ASSOCIATION MyModule.MyApiResponse_MyApiItem
203+
FROM MyModule.MyApiResponse
204+
TO MyModule.MyApiItem;
209205

210206
-- Step 3: Import Mapping
211-
CREATE IMPORT MAPPING Integrations.IMM_BibleVerse
212-
WITH JSON STRUCTURE Integrations.JSON_BibleVerse
207+
CREATE IMPORT MAPPING MyModule.IMM_MyApi
208+
WITH JSON STRUCTURE MyModule.JSON_MyApi
213209
{
214-
CREATE Integrations.BibleApiResponse {
215-
CREATE Integrations.BibleApiResponse_BibleTranslation/Integrations.BibleTranslation = translation {
216-
identifier = identifier,
217-
language = language,
218-
language_code = language_code,
219-
license = license,
220-
name = name
221-
},
222-
CREATE Integrations.BibleApiResponse_BibleVerse/Integrations.BibleVerse = random_verse {
223-
book = book,
224-
book_id = book_id,
225-
chapter = chapter,
226-
text = text,
227-
verse = verse
210+
CREATE MyModule.MyApiResponse {
211+
ApiStatus = status,
212+
Version = version,
213+
CREATE MyModule.MyApiResponse_MyApiItem/MyModule.MyApiItem = item {
214+
Code = code,
215+
Label = label,
216+
Count = count
228217
}
229218
}
230219
};
231220

232221
-- Step 4: Microflow
233-
CREATE MICROFLOW Integrations.GET_BibleVerse_Random ()
222+
CREATE MICROFLOW MyModule.GET_MyApi_Item ()
234223
BEGIN
235224
@position(-5, 200)
236-
DECLARE $baseUrl String = 'https://bible-api.com';
225+
DECLARE $url String = 'https://api.example.com/item';
237226
@position(185, 200)
238-
DECLARE $endpoint String = $baseUrl + '/data/web/random';
239-
@position(375, 200)
240-
$Result = REST CALL GET '{1}' WITH ({1} = $endpoint)
227+
$MyApiResponse = REST CALL GET '{1}' WITH ({1} = $url)
241228
HEADER 'Accept' = 'application/json'
242229
TIMEOUT 300
243-
RETURNS MAPPING Integrations.IMM_BibleVerse AS Integrations.BibleApiResponse ON ERROR ROLLBACK;
230+
RETURNS MAPPING MyModule.IMM_MyApi AS MyModule.MyApiResponse ON ERROR ROLLBACK;
231+
@position(375, 200)
232+
RETRIEVE $MyApiItem FROM $MyApiResponse/MyModule.MyApiResponse_MyApiItem;
244233
@position(565, 200)
245-
LOG INFO NODE 'Integration' 'Retrieved Bible verse' WITH ();
234+
LOG INFO NODE 'Integration' 'Status={1} Code={2} Count={3}'
235+
WITH ({1} = $MyApiResponse/ApiStatus,
236+
{2} = $MyApiItem/Code,
237+
{3} = toString($MyApiItem/Count));
246238
END;
247239
/
248240
```
249241

242+
> **Note on association retrieve:** `RETRIEVE $Child FROM $Parent/Module.Assoc` on a Reference-type association traversed forward (from FK-owner to referenced entity) returns a **single entity**, not a list. `LIMIT 1` is redundant and will be dropped from the BSON roundtrip.
243+
250244
---
251245

252246
## Gotchas and Common Errors
@@ -259,6 +253,9 @@ END;
259253
| Studio Pro shows "List of X" but mapping returns single X | `ForceSingleOccurrence` not set | Executor auto-detects from JSON structure root element type |
260254
| StartEvent behind first activities | Default posX=200 vs @position(-5,...) | Fixed: executor pre-scans for first @position and shifts StartEvent left |
261255
| `TypeCacheUnknownTypeException` | Wrong BSON `$Type` names | `ImportMappings$ObjectMappingElement` / `ImportMappings$ValueMappingElement` (no `Import` prefix) |
256+
| `mxcli check` rejects `WITH JSON STRUCTURE` | Stale binary | Rebuild: `go build -o ./bin/mxcli ./cmd/mxcli` then retry |
257+
| `mismatched input 'unlimited'` | `String(unlimited)` in CREATE | Use `String(0)` for unlimited strings; `unlimited` is DESCRIBE output only |
258+
| `mismatched input ')'` on entity | Wrong entity form `CREATE ENTITY X (NON_PERSISTENT)` | Use `CREATE OR REPLACE NON-PERSISTENT ENTITY X (attrs)` |
262259
| Attribute not found in Studio Pro | Attribute not fully qualified | Must be `Module.Entity.AttributeName` in the BSON |
263260

264261
---
@@ -267,9 +264,9 @@ END;
267264

268265
| Artifact | Pattern | Example |
269266
|----------|---------|---------|
270-
| JSON Structure | `JSON_<ApiName>` | `JSON_BibleVerse` |
271-
| Import Mapping | `IMM_<ApiName>` | `IMM_BibleVerse` |
272-
| Root entity | Describes the API response | `BibleApiResponse` |
273-
| Nested entities | Describes the domain concept | `BibleVerse`, `BibleTranslation` |
274-
| Microflow | `METHOD_Resource_Operation` | `GET_BibleVerse_Random` |
267+
| JSON Structure | `JSON_<ApiName>` | `JSON_OrderApi` |
268+
| Import Mapping | `IMM_<ApiName>` | `IMM_OrderApi` |
269+
| Root entity | Describes the API response | `OrderApiResponse` |
270+
| Nested entities | Describes the domain concept | `OrderItem`, `OrderAddress` |
271+
| Microflow | `METHOD_Resource_Operation` | `GET_Order_ById` |
275272
| Folder | `Private/` for mappings/structures, `Operations/` for public microflows ||

0 commit comments

Comments
 (0)