From Clomosy Docs
No edit summary |
No edit summary |
||
| Line 207: | Line 207: | ||
</div> | </div> | ||
<b> Example 2 </b><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'; // 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; | |||
} | |||
</pre> | |||
<b> Output:</b><br> | |||
<div class="alert alert-dark" role="alert" data-bs-theme="light"> | |||
Work | |||
</div> | |||
<h2> See Also </h2> | <h2> See Also </h2> | ||
Revision as of 13:19, 10 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
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
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'; // 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:
Work