OCLOperators JSonToObjects
No edit summary
No edit summary
Line 5: Line 5:
Inorder for the JSON data to be captured,  the model classes attributes have to match the JSON properties.
Inorder for the JSON data to be captured,  the model classes attributes have to match the JSON properties.


Example
==== Example ====
<syntaxhighlight lang="json">
{
  "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."
    }
  ]
}
</syntaxhighlight>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.
[[File:2024-08-18 09h06 32.png|none|thumb|447x447px]]


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.


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


Create a class that with 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.


Copy the JSON data that needs to be saved in the Model making sure the curly braces ({}) are 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.


Right Click on the root class and  select '''Add attributes, assoc, and classes from clipboard json or xml''' option from options 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 =====
[[File:2024-08-18 09h18 22.png|none|thumb|566x566px]]
Only leave attributes and associations needed to be saved.


Other Pages:
Use JsonToObjects to save the JSON data.
selfVM.JSonToObjects(APIResult, vData)
<blockquote>where vData is the JSON data to be converted and saved.


Only attributes and associations available in the classes will be captured and the rest ignored.
</blockquote>
==== Other Pages: ====
* [[Import_xml_and_JSon_with_MDriven]]
* [[Import_xml_and_JSon_with_MDriven]]

Revision as of 06:34, 18 August 2024

The JsonToObjects OCL operator is for converting a JSON string to a class object. The operator accommodates Model structures having more than one class with relationships.

Inorder for the JSON data to be captured, 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 needed to be saved.

Use JsonToObjects to save the JSON data.

selfVM.JSonToObjects(APIResult, vData) 

where vData is the JSON data to be converted and saved.

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

Other Pages:

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