From Clomosy Docs

No edit summary
No edit summary
Line 1: Line 1:
"JSON Data Source Query" is a term that typically refers to a query or resource definition used by an application or a database system to perform operations like data retrieval, updating, or querying in JSON format. JSON is a popular format for data transport and storage, and many programming languages and database management systems provide the capability to work with JSON data.<br>
JSON (JavaScript Object Notation) is a lightweight, human-readable format used for data exchange. JSON consists of key-value pairs and arrays.
<br><br>
<h2 style="font-size:18px;border-bottom:none;font-weight: bold;
">Using JSON with Databases</h2>
In Clomosy applications, create a TCLJSONQuery object to work with JSON datasets.
<pre>
var
qry:TCLJSONQuery;
</pre>
Create the TCLJSONQuery object
<pre>
qry = TCLJSONQuery.Create(nil);
</pre>
<h2 style="font-size:18px;border-bottom:none;font-weight: bold;
">Converting JSON Data</h2>
Use the ClDataSetFromJSON function to take a JSON array and convert it into a dataset object.
<pre>
Clomosy.ClDataSetFromJSON('[{"name":"Anna","age":20},{"name":"David","age":27}]');
</pre>
<h2 style="font-size:18px;border-bottom:none;font-weight: bold;
">Converting Datasets to JSON</h2>
Use the DBDatasetGetAsJSON function to retrieve a dataset from the database and convert it into JSON format.
<pre>
var jsonStr: String;
jsonStr = Clomosy.DBDatasetGetAsJSON(SearchCode);
</pre>
<h2 style="font-size:18px;border-bottom:none;font-weight: bold;
">Using JSON Data</h2>
Use the getJSONString property to return the data inside a TCLJSONQuery object as a JSON string.
<pre>
var
qry: TCLJSONQuery;
qry = TCLJSONQuery.Create(nil);
qry = Clomosy.ClDataSetFromJSON('[{"name":"Arda","age":20},{"name":"Ayse","age":27}]');
ShowMessage(qry.getJSONString);
</pre>
<h2 style="font-size:18px;border-bottom:none;font-weight: bold;
">Counting JSON Records</h2>
Use the RecordCount property to return the number of records in a TCLJSONQuery object.
<pre>
var
recordCount: Integer;
recordCount = qry.RecordCount;
ShowMessage(recordCount);
</pre>
JSON data is transformed into a TCLJSONQuery object. This object contains the JSON data and allows query operations to be performed on it. Following this, operations can be performed on this data.


In the context of Clomosy, it allows for data to be retrieved in "ClDataSetFromJSON" format, which can then be transformed into a dataset.<br>
Example:


It is used to convert JSON data into a TCLJSONQuery object and process the data by performing operations on it. Let's take a look at how it can be used:<br>
<b>TRObject Syntax</b>


A variable belonging to the TCLJSONQuery object is declared. Then, this object is created.
<pre>
var
var
qry:TCLJSONQuery;
  qry: TCLJSONQuery;
 
{
qry := TCLJSONQuery.Create(nil);
  try
 
    qry = TCLJSONQuery.Create(nil);
JSON data is transformed into a TCLJSONQuery object. This object contains the JSON data and allows query operations to be performed on it.
    qry = Clomosy.ClDataSetFromJSON('[{"name":"Anna","age":20},{"name":"David","age":27}]');   
Following this, operations can be performed on this data.
    with qry do
 
     {
'''Example:'''<br>
       if (Found)  
:'''Base Syntax'''
var
  qry:TCLJSONQuery;
begin
    try
      qry := TCLJSONQuery.Create(nil);
      qry := Clomosy.ClDataSetFromJSON('[{"name":"Anna","age":20},{"name":"David","age":27}]');
  //ShowMessage(qry.getJSONString);
      with qry do
      begin
        if Found then
        begin
          First;
          while not EOF do
          begin
            ShowMessage(FieldByName('name').AsString);
            Next;
          end;
        end;
      end;
    except
      ShowMessage('Exception Class: '+LastExceptionClassName+' Exception Message: '+LastExceptionMessage);
     end;
end;
 
:'''TRObject Syntax'''
 
  var
  qry:TCLJSONQuery;
  {
    try
       qry = TCLJSONQuery.Create(nil);
      qry = Clomosy.ClDataSetFromJSON('[{"name":"Anna","age":20},{"name":"David","age":27}]');
      //ShowMessage(qry.getJSONString);
      with qry do
       {
       {
         if (Found)
         First;
        while (not EOF) // Loop until the end of the dataset is reached
         {
         {
           First;
           ShowMessage(FieldByName('name').AsString); // Display the 'name' field of each record
          while (not EOF)
          Next; // Move to the next record
          {
            ShowMessage(FieldByName('name').AsString);
            Next;
          }
         }
         }
       }
       }
    except
      ShowMessage('Exception Class: '+LastExceptionClassName+' Exception Message: '+LastExceptionMessage);
     }
     }
  except
    // If an error occurs, display the exception class and message
    ShowMessage('Exception Class: '+LastExceptionClassName+' Exception Message: '+LastExceptionMessage);
   }
   }
}
</pre>
<b>Base Syntax</b>
<pre>
var
  qry:TCLJSONQuery;
begin
  try
    qry := TCLJSONQuery.Create(nil);
    qry := Clomosy.ClDataSetFromJSON('[{"name":"Anna","age":20},{"name":"David","age":27}]');
//ShowMessage(qry.getJSONString);
    with qry do
    begin
      if Found then
      begin
        First;
        while not EOF do
        begin
          ShowMessage(FieldByName('name').AsString);
          Next;
        end;
      end;
    end;
  except
    ShowMessage('Exception Class: '+LastExceptionClassName+' Exception Message: '+LastExceptionMessage);
  end;
end;
</pre>

Revision as of 13:59, 15 October 2024

JSON (JavaScript Object Notation) is a lightweight, human-readable format used for data exchange. JSON consists of key-value pairs and arrays.

Using JSON with Databases

In Clomosy applications, create a TCLJSONQuery object to work with JSON datasets.

var
qry:TCLJSONQuery;

Create the TCLJSONQuery object

qry = TCLJSONQuery.Create(nil); 

Converting JSON Data

Use the ClDataSetFromJSON function to take a JSON array and convert it into a dataset object.

Clomosy.ClDataSetFromJSON('[{"name":"Anna","age":20},{"name":"David","age":27}]');

Converting Datasets to JSON

Use the DBDatasetGetAsJSON function to retrieve a dataset from the database and convert it into JSON format.

var jsonStr: String;
jsonStr = Clomosy.DBDatasetGetAsJSON(SearchCode);

Using JSON Data

Use the getJSONString property to return the data inside a TCLJSONQuery object as a JSON string.

var 
qry: TCLJSONQuery;
qry = TCLJSONQuery.Create(nil);
qry = Clomosy.ClDataSetFromJSON('[{"name":"Arda","age":20},{"name":"Ayse","age":27}]');
ShowMessage(qry.getJSONString);

Counting JSON Records

Use the RecordCount property to return the number of records in a TCLJSONQuery object.

var 
recordCount: Integer;
recordCount = qry.RecordCount;
ShowMessage(recordCount);

JSON data is transformed into a TCLJSONQuery object. This object contains the JSON data and allows query operations to be performed on it. Following this, operations can be performed on this data.

Example:

TRObject Syntax

var
  qry: TCLJSONQuery;  
{
  try
    qry = TCLJSONQuery.Create(nil);  
    qry = Clomosy.ClDataSetFromJSON('[{"name":"Anna","age":20},{"name":"David","age":27}]');  
    with qry do
    {
      if (Found) 
      {
        First; 
        while (not EOF)  // Loop until the end of the dataset is reached
        {
          ShowMessage(FieldByName('name').AsString);  // Display the 'name' field of each record
          Next;  // Move to the next record
        }
      }
    }
  except
    // If an error occurs, display the exception class and message
    ShowMessage('Exception Class: '+LastExceptionClassName+' Exception Message: '+LastExceptionMessage);
  }
}

Base Syntax

var
  qry:TCLJSONQuery;
begin
   try
     qry := TCLJSONQuery.Create(nil);
     qry := Clomosy.ClDataSetFromJSON('[{"name":"Anna","age":20},{"name":"David","age":27}]');
//ShowMessage(qry.getJSONString);
     with qry do
     begin
       if Found then
       begin
         First;
         while not EOF do
         begin
           ShowMessage(FieldByName('name').AsString);
           Next;
         end;
       end;
     end;
   except
     ShowMessage('Exception Class: '+LastExceptionClassName+' Exception Message: '+LastExceptionMessage);
   end;
end;