From Clomosy Docs

No edit summary
No edit summary
Line 47: Line 47:
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.
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:
<b>Example</b><br>
 
<b>TRObject Syntax</b>


<pre>
<pre>
Line 75: Line 73:
   }
   }
}
}
</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>
</pre>

Revision as of 11:21, 13 November 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

Define a TCLJSONQuery object to work with JSON datasets in Clomosy applications.

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.

qry = 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

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);
  }
}

See Also