OCLOperators JSonToObjects
No edit summary
No edit summary
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
<message>Write the content here to display this box</message>
<message>Write the content here to display this box</message>
{{Edited|July|12|2024}}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.
[[Category:OCLOperators]]
[[Category:OCLOperators]]
{{Edited|July|12|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.
In order to capture the JSON data,  the model classes attributes have to match the JSON properties.
 
=== 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]]
 


Inorder for the JSON data to be captured,  the model classes attributes have to match the JSON properties.


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


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.
'''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
'''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.


Create a class that with act as the root for the JSON structure
===== Classes Structure Fitting the JSON data =====
[[File:2024-08-18 09h18 22.png|none|thumb|495x495px]]
Only leave attributes and associations that need to be saved.


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.
Use JsonToObjects to save the JSON data.
selfVM.JSonToObjects(APIResult, vData)  
<blockquote>vData is the JSON data to be converted and saved.


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.
Only attributes and associations available in the classes will be captured while the rest will be ignored.


Other Pages:
</blockquote>


==== See also: ====
* [[Import_xml_and_JSon_with_MDriven]]
* [[Import_xml_and_JSon_with_MDriven]]

Latest revision as of 05:13, 19 August 2024


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 32 days ago on 08/19/2024. What links here