Import mapping from Rest JSON response

0
I’m currently trying to import some data from an external source using the REST components via microflows. However, the REST response list given back is seemingly dynamic giving 2 types of items in the same list with each object given a unique key. How do I map them to standard Mendix entities?    I’ve included an JSON sample from the external source:  { "results": [                             {                                 "id": "xxxxxyyyyyy",                                 "fileName": "dcd43c01ea8425aed5c425408d6e222d.jpeg",                                 "urls": {                                     "9556d59952934e41a53965e8864d41c5": {                                         "url": "https://xxx-delivery.sitecorecontenthub.cloud/api/public/content/xxxxxx?v=xxxxxx",                                         }                                     },                                     "2ef685eeedee447ebefddd0e8f2a6412": {                                         "url": "https://xxx-delivery.sitecorecontenthub.cloud/api/public/content/xxxxxxx?v=yyyyyy",                                         "expiredOn": null,                                         "resource": "preview",                                         "metadata": {                                             "width": "514",                                             "height": "268",                                             "content_type": "image/jpeg",                                             "filesizebytes": "42644"                                         }                                     }                                 }                             }                         ] }
asked
1 answers
1

 

The first thing to notice is that your JSON is invalid.

 

 

 

This would be the correct and valid JSON

 

{
  "results": [
    {
      "id": "xxxxxyyyyyy",
      "fileName": "dcd43c01ea8425aed5c425408d6e222d.jpeg",
      "urls": {
        "9556d59952934e41a53965e8864d41c5": {
          "url": "https://xxx-delivery.sitecorecontenthub.cloud/api/public/content/xxxxxx?v=xxxxxx"
        },
        "2ef685eeedee447ebefddd0e8f2a6412": {
          "url": "https://xxx-delivery.sitecorecontenthub.cloud/api/public/content/xxxxxx?v=xxxxxx",
          "expiredOn": null,
          "resource": "preview",
          "metadata": {
            "width": "514",
            "height": "268",
            "content_type": "image/jpeg",
            "filesizebytes": "42644"
          }
        }
      }
    }
  ]
}

 

Another point to look into is that a set of unique urls should be in an array, instead of an object.

If it is an array (with the squared brackets []) mendix can handle the unique keys perfectly all the time.

If you have unique keys and you map them, only does keys will be accepted.

 

The JSON should then look something like this.

 

{
  "results": [
    {
      "id": "xxxxxyyyyyy",
      "fileName": "dcd43c01ea8425aed5c425408d6e222d.jpeg",
      "urls": [
        {
          "id": "9556d59952934e41a53965e8864d41c5",
          "url": "https://xxx-delivery.sitecorecontenthub.cloud/api/public/content/xxxxxx?v=xxxxxx"
        },
        {
          "id": "2ef685eeedee447ebefddd0e8f2a6412",
          "url": "https://xxx-delivery.sitecorecontenthub.cloud/api/public/content/xxxxxx?v=xxxxxx",
          "expiredOn": null,
          "resource": "preview",
          "metadata": {
            "width": "514",
            "height": "268",
            "content_type": "image/jpeg",
            "filesizebytes": "42644"
          }
        }
      ]
    }
  ]
}

 

If you cannot change the ‘source’ of the JSON, want you can do instead is handle the incoming data from the REST Call as a string.

 

 

Perform some changes on it and after that perform a manual ‘Import with mapping’ action.

 

answered