Delphi: Accessing JSON Objects within a JSON Array - sql

I have a JSON Object, let's name it jObject that looks like this:
{
"id": 0,
"data": "[{DAT_INCL: \"08/03/2012 10:07:08\", NUM_ORDE: 1, NUM_ATND: 1, NUM_ACAO: 2, NUM_RESU: 3},
{DAT_INCL: \"08/03/2012 10:07:09\", NUM_ORDE: 2, NUM_ATND: 1, NUM_ACAO: 4, NUM_RESU: 5},
{DAT_INCL: \"08/03/2012 10:07:09\", NUM_ORDE: 3, NUM_ATND: 1, NUM_ACAO: 8, NUM_RESU: NULL}]"
}
As you can see, it contains two pairs, one of which is an array with three objects in this case (the amount of objects is dynamic) with multiple "key: values"(these don't vary, being always the same 5 fields), which I want to insert into an SQL database, "key" being column, "value" being field. Question is, how do I access each object individually?
Code-wise what I did was extract the pair that contained this array by putting it in jPair
jPair := OriginalObject.Get(1);
and then captured the array
jArray:= TJSONArray(jPair.JsonValue);
(Also, as a bonus, when I evaluate jArray.Size, the result is 6226004. What?)

If you have an array from DBXJSON, then it is a TJSONArray. Call its Get method to get an element of the array.
var
Value: TJSONValue;
Value := jArray.Get(0);
You can also go through the entire array with a for loop:
for Value in jArray do
But if you check the Size property and get 6226004 instead of 3, that suggests there's something else wrong here. My guess is that what you think is a TJSONArray isn't really that type. Use as to do a checked type cast:
jArray := jPair.JsonValue as TJSONArray;
You'll get an EInvalidCast exception if that fails.

here is an sample code to parse and output your json data. I've modified your JSON data and added ArrayData field, wich contains your initial array of objects:
program Project1;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils, dbxjson;
const JSON_DATA = '{"ArrayData":['+
'{"DAT_INCL":"07/03/2012 17:33:03", "NUM_ORDE":1,"NUM_ATND":1, "NUM_ACAO":2, "NUM_RESU":3},'+
'{"DAT_INCL":"07/03/2012 17:33:05", "NUM_ORDE":2,"NUM_ATND":1, "NUM_ACAO":4, "NUM_RESU":5},'+
'{"DAT_INCL":"07/03/2012 17:33:05", "NUM_ORDE":3,"NUM_ATND":1, "NUM_ACAO":8, "NUM_RESU":null}'+
']}';
var jsv : TJsonValue;
originalObject : TJsonObject;
jsPair : TJsonPair;
jsArr : TJsonArray;
jso : TJsonObject;
i : integer;
begin
try
//parse json string
jsv := TJSONObject.ParseJSONValue(JSON_DATA);
try
//value as object
originalObject := jsv as TJsonObject;
//get pair, wich contains Array of objects
jspair := originalObject.Get('ArrayData');
//pair value as array
jsArr := jsPair.jsonValue as TJsonArray;
writeln('array size: ', jsArr.Size);
//enumerate objects in array
for i := 0 to jsArr.Size - 1 do begin
writeln('element ', i);
// i-th object
jso := jsArr.Get(i) as TJsonObject;
//enumerate object fields
for jsPair in jso do begin
writeln(' ', jsPair.JsonString.Value, ': ', jsPair.JsonValue.Value);
end;
end;
finally
jsv.Free();
readln;
end;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.

Practical example of adding JSON data from an array to a TComboBox
This example assumes you have included the JSON unit. It also assumes you have a TComboBox named cmbCompany. There is nothing wrong with the accepted answers, I just wanted to document.
uses JSON;
procedure LoadCompanies;
var
i : Integer;
companyArray : TJsonArray;
company : TJsonObject;
begin
//get the company data
companyData := TJSONObject.ParseJSONValue('{data:[{"name": "One"},{"name": "Two"}]}') as TJSONObject;
try
cmbCompany.Items.Clear;
with companyData do
begin
//Important bit which relates to the question!
companyArray := (Get('data').JsonValue as TJSONArray);
try
for i := 0 to companyArray.Size-1 do
begin
//Get one of the array values
company := (companyArray.Get(i) as TJSONObject);
//Add name to combo box, you can obviously get other values in similar fashion
cmbCompany.Items.Add(company.GetValue<String>('name'));
end;
finally
companyArray.Free;
end;
end;
finally
companyData.Free;
end;
end;

Related

Why is my script not building in HelpNDoc? Error: Unknown name

I am trying to write a script:
const
// Define the output file
OUTPUT_FILE = 'd:\topics.txt';
var
// Current topic ID
aTopicId: string;
// List of output
aList: TStringList;
begin
// Init list
aList := TStringList.Create;
aList.Add('Topic Caption | Help ID | Help Context | Meta Description');
try
// Get first topic
aTopicId := HndTopics.GetTopicFirst();
// Loop through all topics
while aTopicId <> '' do
begin
// Add the topic to the list
aList.Add(Format('%s | %s | %d | %s', [
HndTopics.GetTopicCaption(aTopicId),
HndTopics.GetTopicHelpId(aTopicId),
HndTopics.GetTopicHelpContext(aTopicId),
HndTopics.GetTopicDescription(aTopicId)
]));
// Get next topic
aTopicId := HndTopics.GetTopicNext(aTopicId);
end;
// Create the file
aList.SaveToFile(OUTPUT_FILE);
finally
aList.Free;
end;
end.
When I build it in HelpNDoc:
Why is it saying:
[Error] script(9, 3): Unknown name "aList"
The authors of HelpNDoc provided some clarification about this issue. Starting with HelpNDoc 7, you need to add the var keyword each time you change the type: Migrating scripts from V6 to V7.
So my variable declaration section needed to be changed to something like:
var
// Current topic ID
aTopicId: string;
var
// List of output
aList: TStringList;
This is a limitation of HelpNDoc's scripting engine.

Build a query on the selected record , position of the dataset pointer

to display the content of a geometry/spatial field I made a small component. This component does it's job when I provide all the information to query the table correctly.
tablename
fieldname of the gemoetry field
fieldname of a field to select the correct element by a query
a connection to connect the query to the correct database on my server
a datasource to display other elements of my table for reference
as my component should be similliar to all the other dbmemo, dbedit ... VCL components, I do not want to share
a) fieldname of a field to select the correct element by a query
and if possible also remove from parameter transfer to my component
b) the connection
here come my current working solution and need help on a) and b)
unit Unit_DBmemoSpatial;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DB, ADODB ;
type
TDBmemoSpatial = class(TMemo)
private
{ Private-Deklarationen }
Fdatasource : Tdatasource ;
FConnection : TADOConnection;
FdataField : String;
procedure Setdatasource(const Value: Tdatasource);
procedure SetdataField(const Value: String);
procedure AfterSCROLL(DataSet: TDataSet);
protected
{ Protected-Deklarationen }
public
{ Public-Deklarationen }
published
{ Published-Deklarationen }
property Connection : TADOConnection read FConnection write FConnection ;
property datasoure : Tdatasource read Fdatasource write Setdatasource ;
property dataField : String read FdataField write SetdataField ;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('TOOLS', [ TDBmemoSpatial]);
end;
{ TDBmemoSpatial }
procedure TDBmemoSpatial.AfterSCROLL(DataSet: TDataSet);
var aQuery : TADOQuery ;
SQLSTR : String ;
RIndex : Integer ;
begin
/// this will only work on my table , not for others !!!!!
RIndex := Fdatasource.DataSet.FieldByName('MyrecordIndex').AsInteger;
self.Lines.Clear;
self.Lines.Add( 'DBCOMP' + TimetoStr(now) );
aQuery := TADOQuery.Create(nil);
try
aQuery.Connection := FConnection;
SQLSTR := 'SELECT ' + FdataField+'.STAsText() FROM ' + 'MyTable' + ' where MyrecordIndex=' + IntToStr(RIndex);
aQuery.SQL.Add(SQLSTR);
aQuery.Open;
self.Lines.Add( 'record affected ' + IntToStr(aQuery.RecordCount) );
self.Lines.Add( 'Text ' + (aQuery.Fields[0].asString) );
finally
aQuery.Free;
end;
end;
procedure TDBmemoSpatial.SetdataField(const Value: String);
begin
FdataField := Value;
end;
procedure TDBmemoSpatial.Setdatasource(const Value: Tdatasource);
begin
Fdatasource := Value;
Fdatasource.DataSet.AfterScroll := AfterSCROLL;
end;
end.
the calling code for my component goes like this
dbmsptl1.Connection := FCon;
dbmsptl1.datasoure := Fdatasource;
dbmsptl1.dataField :='geometry_by_Fieldname';

How can I read All Items in table of table with lua api?

I have a table in a lua file.
This is the table:
main = {}
main["first"] = {
{name = "sammy", type = "dog", age = 2, color = "blue"},
{name = "tom", type = "cat", age = 3, color = "orange"},
.
.
.
.
}
main["second"] = {
{name = "grim", type = "cow", age = 1, color = "green"},
{name = "jerry", type = "horse", age = 2, color = "grey"},
.
.
.
.
}
The table has two tables. I want to read all tables in main. So I wrote this code. (I use delphi. plua_state means pointer of lua_state)
This is my code:
procedure TForm1.PrintTable(l: Plua_State);
var
TempStr : String;
begin
lua_pushnil(l);
TempStr := '';
while lua_next(l,-2) <> 0 do
begin
case lua_type(l,-1) of
LUA_TTABLE :
PrintTable(l);
else
TempStr := TempStr + lual_checkstring(L, -2) + '='
+ lua_typename(L, lua_type(L, -1)) + ',';
end;
lua_pop(l,1);
end;
fStringList.Add(TempStr);
end;
This code prints the key and value type in the main table.However, If I change lua_typename(L, lua_type(L, -1)) to lual_checkstring(L, -1), It does not work. I want to print the key and value.
How can I print the value?
You must be careful when using lua_next as several string API functions will alter the value on the stack which confuses lua_next. From the manual:
While traversing a table, do not call lua_tolstring directly on a key, unless you know that the key is actually a string. Recall that lua_tolstring may change the value at the given index; this confuses the next call to lua_next.
So, calling lua_tostring, lua_tolstring, or luaL_checklstring on a key pushed by lua_next will change the key into a string.
As you have discovered it is best to use lua_type to make sure the value is indeed a string before calling lua_tolstring (or any API function that calls it), or push a copy of the key which can be popped leaving the original key returned by lua_next unmodified.

Delphi load image save as blob in a sql database

I'm trying to load a Image control from a image blob saved previously in a sql database.I have testd so many ways and i can't make it work. The image blob is saved as:
qry.SQL.Text := 'update tbl set pic = :blobVal where id = :idVal';
qry.Parameters.ParamByName('blobVal').LoadFromFile('c:\sample.jpg', ftBlob);
qry.Parameters.ParamByName('idVal').Value := 1;
any suggestion?
There a a lot of treads here about loading images to as database, but I did not find one with update or insert parameters.
You might simply assign a graphic object to your parameter.
If you want to store different graphic types you should add a column
keeping the Information which kind of graphic should be stored (e.g. jpeg,bmp,png).
to be able to create the needed TGraphic class descendant if you want to retrieve the picture from the database.
uses jpeg, pngimage;
type
TitTYPES=(itJPG,itPNG,itBMP);
procedure TDEMO.Button1Click(Sender: TObject);
var
jp:TJpegimage;
g:TGraphic;
begin
jp:=TJpegimage.Create;
try
ads.Close;
jp.LoadFromFile('C:\Bilder1\PIC.jpg');
ads.SQL.Text := 'Insert into IMGBlob (ID,Blob,typ) Values (:ID,:BLOB,:typ)';
ads.Parameters[0].Value := 1;
ads.Parameters[1].Assign(jp);
ads.Parameters[2].Value := itJPG;
ads.ExecSQL;
ads.SQL.Text := 'Select * from IMGBlob where ID=:ID';
ads.Parameters[0].Value := 1;
ads.Open;
try
case TitTYPES(ads.FieldByName('typ').AsInteger) of
itJPG: g:=TJpegimage.Create;
itPNG: g:=TPNGImage.Create;
itBMP: g:=TBitmap.Create;
end;
g.Assign(ads.FieldByName('Blob'));
Image1.Picture.Assign(g);
finally
g.Free;
end;
finally
jp.Free;
end;
end;
To load a BLOB field into an image, you need to use TDataSet.CreateBlobStream.
var
Stream: TStream;
JPG: TJpegImage;
begin
JPG := TJpegImage.Create;
try
Stream := Qry.CreateBlobStream(Qry.FieldByName('BLOBVAL'), bmRead);
try
JPG.LoadFromStream(Stream);
finally
Stream.Free; // edited
end;
finally
JPG.Free;
end;
end;
To store the image back, you'll need to do the reverse:
var
Stream: TBlobStream;
Jpg: TJpegImage;
begin
Jpg := TJpegImage.Create;
try
Jpg.Assign(Image1.Picture.Graphic);
// Assign other query parameters here
Stream := Qry.CreateBlobStream(Qry.FieldByName('BLOBVAL'), bmWrite);
try
Jpg.SaveToStream(Stream);
Qry.ExecSQL;
finally
Stream.Free;
end;
finally
Jpg.Free;
end;
end;
TDBImage is only designed to work with bitmaps (when the field is ftGraphic), so it won't work with JPEG images directly. The easiest thing to do is to load the blob as a JPEG, and assign it to a standard TImage.Picture.Graphic in an event handler for the dataset (such as it's AfterScroll event).
save to db:
var
ms:tmemorystream;
Begin
ms:=tmemorystream.create;
ms.position:=0;
image1.picture.bitmap.savetostream(ms);
ms.position:=0;
with yourfield as tblobfield do
loadfromstream(ms);
freeandnil(ms);
end;
Load from db:
var
ms:tmemorystream;
Begin
ms:=tmemorystream.create;
ms.position:=0;
with yourfield as tblobfield do
savetostream(ms);
ms.position:=0;
image1.picture.bitmap.loadfromstream(ms);
freeandnil(ms);
end;
It does not work with all graphic types like PNG etc.
This one does work on PNG as well:
var mBitmap : TGraphic;
var mStream : TStream;
var mClass : TGraphicClass;
begin
mStream := Query.CreateBlobStream(Query.FieldByName('yourBlobfield'), bmRead);
mClass := GetGraphicClassForFileExtension(mStream.ReadAnsiString);
mBitmap := mClass.Create;
mBitmap.LoadFromStream(mStream);
Image4.Picture.Assign(mBitmap);
end;

Oracle 11g - Find Records in a CLOB with Carriage Return Line Feed

I am on Oracle 11g. I am trying to do a Find and Replace functionality on a CLOB field (using REPLACE).
Now the data in my CLOB has CRLFs in them, the replace works just fine until I want to find a string that contains CRLFs. Perhaps this would be best explained by example:
So say the text in my field is:
----------------------------------
Hi there this is some text
that has CRLFS in it.
Some other text that
is there also.
Have a nice day
Now what I want to do is replace all occurrences of this INCLUDING the CRLFs:
Search Text
--------------------------------------------------------------------------------
Some other text that
is there also.
With this text INCLUING the CRLFs:
Replace Text
------------------------------------
Some other text that
has some new text that is there also.
So That could come out to be:
----------------------------------
Hi there this is some text
that has CRLFS in it.
Some other text that
has some new text that is there also.
Have a nice day
Now I am doing this in a stored procedure and the Search Text and Replace Text come in as variables, but When I try and say where like % || ReplaceText || '%' it returns 0 rows.
Does anyone have an idea how to do this?
Here is my stored procedure (iOldResponsibilities is the Search Text, iNewResponsibilities is the replace text:
PROCEDURE FindReplaceResponsibilities (
iOldResponsibilities IN JP_JOB_FAMILIES.RESPONSIBILITIES%TYPE,
iNewResponsibilities IN JP_JOB_FAMILIES.RESPONSIBILITIES%TYPE,
oNumRowsUpdated OUT INTEGER
)
IS
BEGIN
oNumRowsUpdated := 0;
SAVEPOINT sp_jf_findrepresp;
-- If there is no old text to search for then,
-- append the new text to the end of every row.
-- Else replace all occurrences of the old text with the new text
IF iOldResponsibilities IS NULL THEN
UPDATE JP_JOB_FAMILIES
SET RESPONSIBILITIES = RESPONSIBILITIES || iNewResponsibilities;
oNumRowsUpdated := SQL%ROWCOUNT;
ELSE
UPDATE JP_JOB_FAMILIES
SET RESPONSIBILITIES = REPLACE(RESPONSIBILITIES, iOldResponsibilities, iNewResponsibilities)
WHERE RESPONSIBILITIES like '%' || iOldResponsibilities || '%';
-- I have also tried this:
--WHERE dbms_lob.instr(RESPONSIBILITIES, TO_CLOB(iOldResponsibilities)) > 0; -- This is a LIKE for CLOBS
oNumRowsUpdated := SQL%ROWCOUNT;
END IF;
RETURN;
EXCEPTION
WHEN OTHERS THEN
BEGIN
oNumRowsUpdated := -1;
ROLLBACK TO sp_jf_findrepresp;
dbms_output.put_line('error: ' || sqlerrm);
RETURN;
END;
END FindReplaceResponsibilities;
The Text is coming from an asp.net application (c#) as String values:
public int FindReplaceJobFamilyResponsibilities(String oldResponsibilities, String newResponsibilities, IDbTransaction transaction = null)
{
using (IDbCommand cmd = this._dataHelper.GetStoredProcedure(_connectionString,
"JP_JOBFAM_PKG.FindReplaceResponsibilities", true))
{
_dataHelper.SetParameterValue(cmd, "iOldResponsibilities", oldResponsibilities);
_dataHelper.SetParameterValue(cmd, "iNewResponsibilities", newResponsibilities);
DataHelperBase.VerifyParameters(cmd.Parameters, false);
base.SetExecuteConnection(cmd, transaction);
_dataHelper.ExecuteNonQuery(cmd);
return Convert.ToInt32(_dataHelper.GetParameterValue(cmd, "oNumRowsUpdated"));
}
}
Turns out to be a case of bad data. The data in my Test DB was corrupted and only had LFs instead of CRLFs.
GIGO :-)
Thanks for all your help
Oh and by the way In my code example I went with the INSTR function instead of the like function. If the user entered a % in the text to search through that might have messed up the like statement. (Can't filter those out because % might be a valid character in my data)
Here is the Final Code:
PROCEDURE FindReplaceResponsibilities (
iOldResponsibilities IN JP_JOB_FAMILIES.RESPONSIBILITIES%TYPE,
iNewResponsibilities IN JP_JOB_FAMILIES.RESPONSIBILITIES%TYPE,
oNumRowsUpdated OUT INTEGER
)
IS
BEGIN
oNumRowsUpdated := 0;
SAVEPOINT sp_jf_findrepresp;
-- If there is no old text to search for then,
-- append the new text to the end of every row.
-- Else replace all occurrences of the old text with the new text
IF iOldResponsibilities IS NULL THEN
UPDATE JP_JOB_FAMILIES
SET RESPONSIBILITIES = RESPONSIBILITIES || iNewResponsibilities;
oNumRowsUpdated := SQL%ROWCOUNT;
ELSE
UPDATE JP_JOB_FAMILIES
SET RESPONSIBILITIES = REPLACE(RESPONSIBILITIES, iOldResponsibilities, iNewResponsibilities)
WHERE dbms_lob.instr(RESPONSIBILITIES, iOldResponsibilities) > 0;
oNumRowsUpdated := SQL%ROWCOUNT;
END IF;
RETURN;
EXCEPTION
WHEN OTHERS THEN
BEGIN
oNumRowsUpdated := -1;
ROLLBACK TO sp_jf_findrepresp;
dbms_output.put_line('error: ' || sqlerrm);
RETURN;
END;
END FindReplaceResponsibilities;
The code from my application was fine:
public int FindReplaceJobFamilyResponsibilities(String oldResponsibilities, String newResponsibilities, IDbTransaction transaction = null)
{
using (IDbCommand cmd = this._dataHelper.GetStoredProcedure(_connectionString,
"JP_JOBFAM_PKG.FindReplaceResponsibilities", true))
{
_dataHelper.SetParameterValue(cmd, "iOldResponsibilities", oldResponsibilities);
_dataHelper.SetParameterValue(cmd, "iNewResponsibilities", newResponsibilities);
DataHelperBase.VerifyParameters(cmd.Parameters, false);
base.SetExecuteConnection(cmd, transaction);
_dataHelper.ExecuteNonQuery(cmd);
return Convert.ToInt32(_dataHelper.GetParameterValue(cmd, "oNumRowsUpdated"));
}
}