From Clomosy Docs

No edit summary
No edit summary
Line 22: Line 22:


<div class="alert alert-ligth border border-3 border-warning rounded-5 p-4 shadow-sm" role="alert">
<div class="alert alert-ligth border border-3 border-warning rounded-5 p-4 shadow-sm" role="alert">
<strong>Accessing Specific Fields from the JSON Data</strong><br><br> <b>Example JSON Data: </b><br>
<strong><h3>Accessing Specific Fields from the JSON Data</h3></strong><br><br> <b>Example JSON Data: </b><br>
<pre>
<pre>
{
{
Line 116: Line 116:
</div>
</div>


<b>Example 1</b><br>
<h3>Example 1</h3><br>
<pre>
<pre>
var
var
Line 207: Line 207:
</div>
</div>


<b> Example 2 </b><br>
<h3> Example 2 </h3><br>


This JSON data contains information about users and their address details. Mert has two addresses (home and work) in the JSON data. The goal is to retrieve the "type" value of Mert's second address.<br>
This JSON data contains information about users and their address details. Mert has two addresses (home and work) in the JSON data. The goal is to retrieve the "type" value of Mert's second address.<br>
Line 300: Line 300:
</div>
</div>


<b> Example 3 - Fetching and Displaying JSON Data</b><br>
<h3> Example 3 - Fetching and Displaying JSON Data</h3><br>
This example creates an application that fetches JSON data from a REST API and displays a specific part of it on the screen.
This example creates an application that fetches JSON data from a REST API and displays a specific part of it on the screen.



Revision as of 05:58, 11 March 2025

The Clomosy.CLParseJSON function is used to process data in JSON format. This function parses the JSON data according to a specified structure and retrieves the specified field (path). For example, it is used to obtain the value of a specific index or key within a JSON object.


JsonData : Data is retrieved in JSON format. Example:

{
 "products": [
   {
     "id": 101,
     "name": "Laptop",
     "price": 15000,
     "stock": 25
   }
 ]
}

JsonPath  : A string value specifies the path to the desired field within the JSON data. For example, to access the price field of the item at index 0 in the above example, it should be defined as 'products.0.price'.


Example 1


var
  Form1:TCLForm;
  ReceivedDataMemo,jsonDataMemo: TclMemo;
  Btn1 : TclButton;
  JsonPathLbl : TclLabel;
  strJSONData,strJsonPath : String;

void GetJsonData;
{
  ReceivedDataMemo.Lines.Text = Clomosy.CLParseJSON(strJSONData,strJsonPath);
}

{
  Form1 = TCLForm.Create(Self);
  strJSONData = '{
  "products": [
    {
      "id": 101,
      "name": "Laptop",
      "price": 15000,
      "stock": 25
    },
    {
      "id": 102,
      "name": "Smartphone",
      "price": 12000,
      "stock": 50
    },
    {
      "id": 103,
      "name": "Headphones",
      "price": 1000,
      "stock": 100
    }
  ]
}';
  strJsonPath = 'products.0'; // or 'products' or 'products.2.name'
  //products.3.name => If the specified index is not present in the JSON structure, an 'Error: Invalid array index.' error is returned.
  jsonDataMemo= Form1.AddNewMemo(Form1,'jsonDataMemo','--');
  jsonDataMemo.Align = alMostTop;
  jsonDataMemo.Height = Form1.clHeight / 3;
  jsonDataMemo.Margins.Left= 5;
  jsonDataMemo.Margins.Right= 5; 
  jsonDataMemo.ReadOnly = True;
  jsonDataMemo.WordWrap = True;
  jsonDataMemo.Lines.Text = strJSONData;
  
  JsonPathLbl= Form1.AddNewLabel(Form1,'JsonPathLbl','--');
  JsonPathLbl.Align = alMostTop;
  JsonPathLbl.Height = 70;
  JsonPathLbl.Margins.Left= 5;
  JsonPathLbl.Margins.Right= 5; 
  JsonPathLbl.StyledSettings = ssFamily;
  JsonPathLbl.TextSettings.Font.Size=20;
  JsonPathLbl.WordWrap = True;
  JsonPathLbl.Autosize = True;
  JsonPathLbl.Text = strJsonPath;
  
  ReceivedDataMemo = Form1.AddNewMemo(Form1,'ReceivedDataMemo','');
  ReceivedDataMemo.Align = alTop;
  ReceivedDataMemo.Height = Form1.clHeight / 3;
  ReceivedDataMemo.Margins.Left= 10;
  ReceivedDataMemo.Margins.Right= 10; 
  ReceivedDataMemo.Margins.Top= 10;
  ReceivedDataMemo.ReadOnly = True;
  ReceivedDataMemo.TextSettings.WordWrap = True;
  
  Btn1 = Form1.AddNewButton(Form1,'Btn1','Get Json Data');
  Btn1.Align = AlBottom;
  Btn1.Height = 80;
  Btn1.Margins.Right = 10;
  Btn1.Margins.Left = 10;
  Form1.AddNewEvent(Btn1,tbeOnClick,'GetJsonData');
  
  Form1.Run;
}

Output: The generated JSON output is as follows.

Example 2


This JSON data contains information about users and their address details. Mert has two addresses (home and work) in the JSON data. The goal is to retrieve the "type" value of Mert's second address.

var
  Form1:TCLForm;
  ReceivedDataMemo,jsonDataMemo: TclMemo;
  Btn1 : TclButton;
  JsonPathLbl : TclLabel;
  strJSONData,strJsonPath : String;

void GetJsonData;
{
  ReceivedDataMemo.Lines.Text = Clomosy.CLParseJSON(strJSONData,strJsonPath);
}

{
  Form1 = TCLForm.Create(Self);
  strJSONData = '{
  "users": [
    {
      "id": 1,
      "name": "Mert",
      "addresses": [
        {
          "type": "Home",
          "city": "Istanbul"
        },
        {
          "type": "Work",
          "city": "Ankara"
        }
      ]
    },
    {
      "id": 2,
      "name": "Zeynep",
      "addresses": [
        {
          "type": "Home",
          "city": "Izmir"
        }
      ]
    }
  ]
}';
  strJsonPath = 'users.0.addresses.1.type'; 
  jsonDataMemo= Form1.AddNewMemo(Form1,'jsonDataMemo','--');
  jsonDataMemo.Align = alMostTop;
  jsonDataMemo.Height = Form1.clHeight / 3;
  jsonDataMemo.Margins.Left= 5;
  jsonDataMemo.Margins.Right= 5; 
  jsonDataMemo.ReadOnly = True;
  jsonDataMemo.WordWrap = True;
  jsonDataMemo.Lines.Text = strJSONData;
  
  JsonPathLbl= Form1.AddNewLabel(Form1,'JsonPathLbl','--');
  JsonPathLbl.Align = alMostTop;
  JsonPathLbl.Height = 70;
  JsonPathLbl.Margins.Left= 5;
  JsonPathLbl.Margins.Right= 5; 
  JsonPathLbl.StyledSettings = ssFamily;
  JsonPathLbl.TextSettings.Font.Size=20;
  JsonPathLbl.WordWrap = True;
  JsonPathLbl.Autosize = True;
  JsonPathLbl.Text = strJsonPath;
  
  ReceivedDataMemo = Form1.AddNewMemo(Form1,'ReceivedDataMemo','');
  ReceivedDataMemo.Align = alTop;
  ReceivedDataMemo.Height = Form1.clHeight / 3;
  ReceivedDataMemo.Margins.Left= 10;
  ReceivedDataMemo.Margins.Right= 10; 
  ReceivedDataMemo.Margins.Top= 10;
  ReceivedDataMemo.ReadOnly = True;
  ReceivedDataMemo.TextSettings.WordWrap = True;
  
  Btn1 = Form1.AddNewButton(Form1,'Btn1','Get Json Data');
  Btn1.Align = AlBottom;
  Btn1.Height = 80;
  Btn1.Margins.Right = 10;
  Btn1.Margins.Left = 10;
  Form1.AddNewEvent(Btn1,tbeOnClick,'GetJsonData');
  
  Form1.Run;
}

Output:

Example 3 - Fetching and Displaying JSON Data


This example creates an application that fetches JSON data from a REST API and displays a specific part of it on the screen.

var
  Form1:TCLForm;
  ReceivedDataMemo,jsonDataMemo: TclMemo;
  Btn1 : TclButton;
  JsonPathLbl : TclLabel;
  strIncomingData : String;
  Rest: TclRest;


void RestCreate(ABaseUrl :String);
{
  Rest = TclRest.Create;
  Rest.Accept = 'application/json';
  Rest.ContentType = 'application/json';
  Rest.Method = rmGet;
  Rest.BaseURL = ABaseUrl; 
  
  Rest.Execute;
}

void GetJsonData;
{
  strIncomingData = Clomosy.CLParseJSON(Rest.Response,'9');
  jsonDataMemo.Lines.Clear;
  jsonDataMemo.Lines.Add(strIncomingData);
  ReceivedDataMemo.Lines.Text = Clomosy.CLParseJSON(strIncomingData,'address.geo');
}

{
  Form1 = TCLForm.Create(Self);
  
  RestCreate('https://jsonplaceholder.typicode.com/users');
  jsonDataMemo= Form1.AddNewMemo(Form1,'jsonDataMemo','--');
  jsonDataMemo.Align = alMostTop;
  jsonDataMemo.Height = Form1.clHeight / 3;
  jsonDataMemo.Margins.Left= 5;
  jsonDataMemo.Margins.Right= 5; 
  jsonDataMemo.ReadOnly = True;
  jsonDataMemo.WordWrap = True;
  
  JsonPathLbl= Form1.AddNewLabel(Form1,'JsonPathLbl','--');
  JsonPathLbl.Align = alMostTop;
  JsonPathLbl.Height = 70;
  JsonPathLbl.Margins.Left= 5;
  JsonPathLbl.Margins.Right= 5; 
  JsonPathLbl.StyledSettings = ssFamily;
  JsonPathLbl.TextSettings.Font.Size=20;
  JsonPathLbl.WordWrap = True;
  JsonPathLbl.Autosize = True;
  JsonPathLbl.Text = 'address.geo';
  
  ReceivedDataMemo = Form1.AddNewMemo(Form1,'ReceivedDataMemo','');
  ReceivedDataMemo.Align = alTop;
  ReceivedDataMemo.Height = Form1.clHeight / 3;
  ReceivedDataMemo.Margins.Left= 10;
  ReceivedDataMemo.Margins.Right= 10; 
  ReceivedDataMemo.Margins.Top= 10;
  ReceivedDataMemo.ReadOnly = True;
  ReceivedDataMemo.TextSettings.WordWrap = True;
  
  Btn1 = Form1.AddNewButton(Form1,'Btn1','Get Json Data');
  Btn1.Align = AlBottom;
  Btn1.Height = 80;
  Btn1.Margins.Right = 10;
  Btn1.Margins.Left = 10;
  Form1.AddNewEvent(Btn1,tbeOnClick,'GetJsonData');
  
  Form1.Run;
}

Retrieved JSON Data:

{
  "id": 10,
  "name": "Clementina DuBuque",
  "username": "Moriah.Stanton",
  "email": "Rey.Padberg@karina.biz",
  "address": {
    "street": "Kattie Turnpike",
    "suite": "Suite 198",
    "city": "Lebsackbury",
    "zipcode": "31428-2261",
    "geo": {
      "lat": "-38.2386",
      "lng": "57.2232"
    }
  },
  "phone": "024-648-3804",
  "website": "ambrose.net",
  "company": {
    "name": "Hoeger LLC",
    "catchPhrase": "Centralized empowering task-force",
    "bs": "target end-to-end models"
  }
}

Output:

See Also