Recent

Author Topic: Database table record to Json string.  (Read 565 times)

BSaidus

  • Hero Member
  • *****
  • Posts: 638
  • lazarus 1.8.4 Win8.1 / cross FreeBSD
Database table record to Json string.
« on: October 09, 2025, 08:37:19 pm »
Hello  :).
Is there any way in FPC/Lazarus to export a Table record to a Json string?
Ex:
Code: [Select]
  // Table
 ________________________________ 
| al_Name    | al_Address     | al_Active |
|_______________________________ |
| aa             | 32, street       | true        |
|_______________________________ |
 
After export I get:
   {"al_Name": "aa", "al_address":"32, street", "al_Active","1"}

I tried this : https://github.com/solussaude/dataset-serialize2, but, it hide the '_' on the fields name,
 I.E the result is:

Code: [Select]
   {"alName": "aa", "aladdress":"32, street", "alActive","1"}

Thank you.
lazarus 1.8.4 Win8.1 / cross FreeBSD
dhukmucmur vernadh!

Thausand

  • Sr. Member
  • ****
  • Posts: 399
Re: Database table record to Json string.
« Reply #1 on: October 09, 2025, 10:12:51 pm »
I tried this : https://github.com/solussaude/dataset-serialize2, but, it hide the '_' on the fields name,
 I.E the result is:

Code: [Select]
   {"alName": "aa", "aladdress":"32, street", "alActive","1"}

result my:
Code: [Select]
{ "al_Name" : "John Doe", "al_Address" : "123 Elm St, Springfield", "al_Active" : true }, { "al_Name" : "Jane Smith", "al_Address" : "456 Oak St, Springfield", "al_Active" : false }, { "al_Name" : "Alice Johnson", "al_Address" : "789 Pine St, Springfield", "al_Active" : true }, { "al_Name" : "Bob Brown", "al_Address" : "321 Maple St, Springfield", "al_Active" : true }, { "al_Name" : "Charlie Davis", "al_Address" : "654 Cedar St, Springfield", "al_Active" : false }, { "al_Name" : "Diana Evans", "al_Address" : "987 Birch St, Springfield", "al_Active" : true }, { "al_Name" : "Ethan White", "al_Address" : "135 Willow St, Springfield", "al_Active" : true }, { "al_Name" : "Fiona Green", "al_Address" : "246 Spruce St, Springfield", "al_Active" : false }, { "al_Name" : "George Black", "al_Address" : "357 Ash St, Springfield", "al_Active" : true }, { "al_Name" : "Hannah Blue", "al_Address" : "468 Cherry St, Springfield", "al_Active" : true }, { "al_Name" : "Ian Gray", "al_Address" : "579 Dogwood St, Springfield", "al_Active" : false }, { "al_Name" : "Julia Red", "al_Address" : "680 Hawthorn St, Springfield", "al_Active" : true }, { "al_Name" : "Kevin Yellow", "al_Address" : "791 Larch St, Springfield", "al_Active" : true }, { "al_Name" : "Laura Purple", "al_Address" : "802 Fir St, Springfield", "al_Active" : false }, { "al_Name" : "Mike Orange", "al_Address" : "913 Poplar St, Springfield", "al_Active" : true }, { "al_Name" : "Nina Pink", "al_Address" : "024 Sycamore St, Springfield", "al_Active" : true }, { "al_Name" : "Oscar Teal", "al_Address" : "135 Walnut St, Springfield", "al_Active" : false }, { "al_Name" : "Paula Cyan", "al_Address" : "246 Chestnut St, Springfield", "al_Active" : true }, { "al_Name" : "Quinn Magenta", "al_Address" : "357 Acacia St, Springfield", "al_Active" : true }, { "al_Name" : "Rita Indigo", "al_Address" : "468 Mulberry St, Springfield", "al_Active" : false }, { "al_Name" : "Sam Violet", "al_Address" : "579 Almond St, Springfield", "al_Active" : true }]

Code: Pascal  [Select][+][-]
  1. procedure Serialize;
  2. var
  3.   LJSONArray: TJSONArray;
  4. begin
  5.   DBQuery.SQL.Text:='SELECT * FROM Tablet';
  6.   DBQuery.Open;
  7.   TDataSetSerializeConfig.GetInstance.CaseNameDefinition:=TCaseNameDefinition.cndNone; // see readme.md
  8.   LJSONArray:=DBQuery.ToJSONArray; // export all records
  9.   DBQuery.Close;
  10.   writeln(LJSONArray.AsJSON);
  11.   LJSONArray.Free;
  12. end;
  13.  
« Last Edit: October 09, 2025, 10:30:56 pm by Thausand »

BSaidus

  • Hero Member
  • *****
  • Posts: 638
  • lazarus 1.8.4 Win8.1 / cross FreeBSD
Re: Database table record to Json string.
« Reply #2 on: October 09, 2025, 10:36:36 pm »
Thanks, I indeed use it, I hacked then function
Code: Pascal  [Select][+][-]
  1. function TDataSetSerialize.DataSetToJSONObject(const ADataSet: TDataSet; const AValue: Boolean = True): TJSONObject;
  2.    // ....
  3.        LKey := LField.FieldName;//TDataSetSerializeUtils.FormatCaseNameDefinition(LField.FieldName);
  4.   // ....
  5.  
to use the fields name without Formatting them.

Thank you.
lazarus 1.8.4 Win8.1 / cross FreeBSD
dhukmucmur vernadh!

BSaidus

  • Hero Member
  • *****
  • Posts: 638
  • lazarus 1.8.4 Win8.1 / cross FreeBSD
Re: Database table record to Json string.
« Reply #3 on: October 10, 2025, 02:32:00 pm »
I tried this : https://github.com/solussaude/dataset-serialize2, but, it hide the '_' on the fields name,
 I.E the result is:

Code: [Select]
   {"alName": "aa", "aladdress":"32, street", "alActive","1"}

result my:
Code: [Select]
{ "al_Name" : "John Doe", "al_Address" : "123 Elm St, Springfield", "al_Active" : true }, { "al_Name" : "Jane Smith", "al_Address" : "456 Oak St, Springfield", "al_Active" : false }, { "al_Name" : "Alice Johnson", "al_Address" : "789 Pine St, Springfield", "al_Active" : true }, { "al_Name" : "Bob Brown", "al_Address" : "321 Maple St, Springfield", "al_Active" : true }, { "al_Name" : "Charlie Davis", "al_Address" : "654 Cedar St, Springfield", "al_Active" : false }, { "al_Name" : "Diana Evans", "al_Address" : "987 Birch St, Springfield", "al_Active" : true }, { "al_Name" : "Ethan White", "al_Address" : "135 Willow St, Springfield", "al_Active" : true }, { "al_Name" : "Fiona Green", "al_Address" : "246 Spruce St, Springfield", "al_Active" : false }, { "al_Name" : "George Black", "al_Address" : "357 Ash St, Springfield", "al_Active" : true }, { "al_Name" : "Hannah Blue", "al_Address" : "468 Cherry St, Springfield", "al_Active" : true }, { "al_Name" : "Ian Gray", "al_Address" : "579 Dogwood St, Springfield", "al_Active" : false }, { "al_Name" : "Julia Red", "al_Address" : "680 Hawthorn St, Springfield", "al_Active" : true }, { "al_Name" : "Kevin Yellow", "al_Address" : "791 Larch St, Springfield", "al_Active" : true }, { "al_Name" : "Laura Purple", "al_Address" : "802 Fir St, Springfield", "al_Active" : false }, { "al_Name" : "Mike Orange", "al_Address" : "913 Poplar St, Springfield", "al_Active" : true }, { "al_Name" : "Nina Pink", "al_Address" : "024 Sycamore St, Springfield", "al_Active" : true }, { "al_Name" : "Oscar Teal", "al_Address" : "135 Walnut St, Springfield", "al_Active" : false }, { "al_Name" : "Paula Cyan", "al_Address" : "246 Chestnut St, Springfield", "al_Active" : true }, { "al_Name" : "Quinn Magenta", "al_Address" : "357 Acacia St, Springfield", "al_Active" : true }, { "al_Name" : "Rita Indigo", "al_Address" : "468 Mulberry St, Springfield", "al_Active" : false }, { "al_Name" : "Sam Violet", "al_Address" : "579 Almond St, Springfield", "al_Active" : true }]

Code: Pascal  [Select][+][-]
  1. procedure Serialize;
  2. var
  3.   LJSONArray: TJSONArray;
  4. begin
  5.   DBQuery.SQL.Text:='SELECT * FROM Tablet';
  6.   DBQuery.Open;
  7.   TDataSetSerializeConfig.GetInstance.CaseNameDefinition:=TCaseNameDefinition.cndNone; // see readme.md
  8.   LJSONArray:=DBQuery.ToJSONArray; // export all records
  9.   DBQuery.Close;
  10.   writeln(LJSONArray.AsJSON);
  11.   LJSONArray.Free;
  12. end;
  13.  

Oops, Thank you! I didn't know that
Code: Pascal  [Select][+][-]
  1.  TDataSetSerializeConfig.GetInstance.CaseNameDefinition:=TCaseNameDefinition.cndNone; // see readme.md  
is feasible.
I got it.

Thank you.
lazarus 1.8.4 Win8.1 / cross FreeBSD
dhukmucmur vernadh!

Thausand

  • Sr. Member
  • ****
  • Posts: 399
Re: Database table record to Json string.
« Reply #4 on: October 10, 2025, 10:23:44 pm »
I not clear and sorry BSaidus.

I read post you and think have problem: only have one record then I make solution and post.

Then I read better you post and read have problem underscore. I search solution (and find) then I have make modify my post for fix underscore. I not tell I make modify.

Sorry for confuse.

This code:
Code: Pascal  [Select][+][-]
  1. TDataSetSerializeConfig.GetInstance.CaseNameDefinition:=TCaseNameDefinition.cndNone; // see readme.md  
  2.  
make fix undercore. There also be other cndXXX value and make other conversion (uppercase, lowercase, etc).

Readme tell this is how make configuration for serialization and have many option and explain in readme: https://github.com/solussaude/dataset-serialize2/blob/master/README.md#configurations

Good and have read it make solve problem for you  :)
« Last Edit: October 10, 2025, 10:36:17 pm by Thausand »

 

TinyPortal © 2005-2018