From Clomosy Docs
No edit summary |
No edit summary |
||
| (4 intermediate revisions by the same user not shown) | |||
| 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> | ||
< | <h3>Example 1 - Parsing and Displaying JSON Data</h3><br> | ||
<pre> | <pre> | ||
var | var | ||
| Line 207: | Line 207: | ||
</div> | </div> | ||
<h3> Example 2 - Parsing and Displaying JSON Data</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> | |||
<pre> | |||
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; | |||
} | |||
</pre> | |||
<b> Output:</b><br> | |||
<div class="alert alert-dark" role="alert" data-bs-theme="light"> | |||
Work | |||
</div> | |||
<h3> Example 3 - Fetching and Parsing JSON Data with REST API</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. | |||
<pre> | |||
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; | |||
} | |||
</pre> | |||
<b> Retrieved JSON Data: </b> | |||
<pre> | |||
{ | |||
"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" | |||
} | |||
} | |||
</pre> | |||
<b> Output:</b><br> | |||
<div class="alert alert-dark" role="alert" data-bs-theme="light"> | |||
{ | |||
:"lat": "-38.2386", | |||
:"lng": "57.2232" | |||
} | |||
</div> | |||
<h2> See Also </h2> | <h2> See Also </h2> | ||
Latest revision as of 11:57, 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.
function CLParseJSON(JsonData,JsonPath :String): String;
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'.
Accessing Specific Fields from the JSON Data
Example JSON Data:
{
"responses": [
{
"message": {
"segments": [
{
"body": "Hi there! I'm available to assist you. What do you need help with today?\n"
}
],
"sender": "system"
},
"completionStatus": "DONE",
"confidenceScore": 0.92
}
],
"processingInfo": {
"inputTokenCount": 10,
"outputTokenCount": 20,
"totalProcessedTokens": 30,
"inputDetails": [
{
"category": "TEXT",
"tokenCount": 10
}
],
"outputDetails": [
{
"category": "TEXT",
"tokenCount": 20
}
]
},
"engineVersion": "neural-3.1-fast"
}
Examples:
Example 1: Retrieve all data within the responses field
- JsonPath: 'responses'
- Extracted Data:
{
"message": {
"segments": [
{
"body": "Hi there! I'm available to assist you. What do you need help with today?\n"
}
],
"sender": "system"
},
"completionStatus": "DONE",
"confidenceScore": 0.92
}
Example 2: Access the message field
- JsonPath: 'responses.0.message'
- Extracted Data:
{
"segments": [
{
"body": "Hi there! I'm available to assist you. What do you need help with today?\n"
}
],
"sender": "system"
}
Example 3: Access the body field
- JsonPath: 'responses.0.message.segments.0.body'
- Extracted Data:
Hi there! I'm available to assist you. What do you need help with today?
Example 4: Access the outputTokenCount field
- JsonPath: 'processingInfo.outputTokenCount'
- Extracted Data:
20
Example 5: Access the tokenCount field inside inputDetails
- JsonPath: 'processingInfo.inputDetails.0.tokenCount'
- Extracted Data:
10
Example 1 - Parsing and Displaying JSON Data
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.
{
"id": 101,
"name": "Laptop",
"price": 15000,
"stock": 25
}
Example 2 - Parsing and Displaying JSON Data
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:
Work
Example 3 - Fetching and Parsing JSON Data with REST API
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:
{
- "lat": "-38.2386",
- "lng": "57.2232"
}