OCLOperators JSonToObjects


The JsonToObjects OCL operator converts a JSON string to a class object. This operator accommodates Model structures that have more than one class with relationships. In order to capture the JSON data, the model classes attributes have to match the JSON properties.

Example

{
  "status": "ok",
  "totalResults": 2,
  "articles": [
    {
      "source": { "id": "bbc-news", "name": "BBC News" },
      "author": null,
      "title": "17/08/2024 05:01 GMT",
      "description": "The latest five minute news bulletin from BBC World Service.",
      "url": "https://www.bbc.co.uk/programmes/w172zgf70jl0qf7",
      "urlToImage": "https://ichef.bbci.co.uk/images/ic/1200x675/p060dh18.jpg",
      "publishedAt": "2024-08-17T05:06:00Z",
      "content": "The latest five minute news bulletin from BBC World Service."
    },
    {
      "source": { "id": "bbc-news", "name": "BBC News" },
      "author": null,
      "title": "17/08/2024 04:01 GMT",
      "description": "The latest five minute news bulletin from BBC World Service.",
      "url": "https://www.bbc.co.uk/programmes/w172zgf70jl0lp3",
      "urlToImage": "https://ichef.bbci.co.uk/images/ic/1200x675/p060dh18.jpg",
      "publishedAt": "2024-08-17T04:06:00Z",
      "content": "The latest five minute news bulletin from BBC World Service."
    }
  ]
}

The MDriven Designer has a feature to add attributes, associations and classes from clipboard JSON. This feature is very handy in ensuring your Model classes match and fit the structure of the JSON data.

2024-08-18 09h06 32.png


1. Create a class that will act as the root for the JSON structure.

2. Copy the JSON data that needs to be saved in the Model, making sure the curly braces ({}) are at the root and included in the data copied.

3. Right-click on the root class and select Add attributes, assoc, and classes from clipboard json or xml option which will try to parse the copied JSON from the clipboard and create a class or classes structure that fits the JSON data.

Classes Structure Fitting the JSON data
2024-08-18 09h18 22.png

Only leave attributes and associations that need to be saved.

Use JsonToObjects to save the JSON data.

selfVM.JSonToObjects(APIResult, vData) 

vData is the JSON data to be converted and saved.

Only attributes and associations available in the classes will be captured while the rest will be ignored.

See also:

This page was edited 126 days ago on 08/19/2024. What links here