Recent

Author Topic: extract data from json ? pls help  (Read 23981 times)

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1248
  • Professional amateur ;-P
Re: extract data from json ? pls help
« Reply #15 on: December 15, 2021, 12:52:05 am »
Hey Y'all,

It's rather intriguing that the Wiki example contains the unit fpjson and then parses the JSON with the most insecure method ever: A pointer to the string.

Well, continuing my collection of TestSomething applications, I've done one that uses both the return of a JSON Array and the return of a JSON Object from the Google Translate service.

I'm attaching a screenshot of the app after it translates.
I'm also attaching the project itself.

If you want to see if I make any changes then feel free to have a look at the GitHub repo: https://github.com/gcarreno/TestGoogleTranslate

This is the, slightly changed, HTTP get function:
Code: Pascal  [Select][+][-]
  1. function TfrmMain.CallGoogleTranslate(AURL: String): TJSONStringType;
  2. var
  3.   client: TFPHTTPClient;
  4.   doc: TStringList;
  5. begin
  6.   Result:= EmptyStr;
  7.   doc:=TStringList.Create;
  8.   client:=TFPHTTPClient.Create(nil);
  9.   try
  10.     client.Get(AURL,doc);
  11.     Result:=doc.Text;
  12.   finally
  13.     doc.Free;
  14.     client.Free;
  15.   end;
  16. end;

This is the call to the service and parsing the JSON Array return:
Code: Pascal  [Select][+][-]
  1. procedure TfrmMain.actArraysTranslateExecute(Sender: TObject);
  2. var
  3.   URL: String;
  4.   Index: integer;
  5.   strResponse: TJSONStringType;
  6.   jdResponse, jdTranslation, jdTranslationArray: TJSONData;
  7.   jaTranslation, jaTranslationArray: TJSONArray;
  8. begin
  9.   actArraysTranslate.Enabled:= False;
  10.   memArraysTo.Clear;
  11.   panArrayDetectedLanguage.Caption:= EmptyStr;
  12.   Application.ProcessMessages;
  13.   try
  14.  
  15.     if Length(memArraysFrom.Text) = 0 then
  16.     begin
  17.       ShowMessage('Need something to translate');
  18.       exit;
  19.     end;
  20.  
  21.     if (cobArrayFrom.ItemIndex <= 0) and (cobArrayTo.ItemIndex <= 0) then
  22.     begin
  23.       ShowMessage('Cannot have language detection on both sides');
  24.       exit;
  25.     end;
  26.  
  27.     URL:='https://translate.googleapis.com/translate_a/single?client=gtx'
  28.       +'&q='+HTTPEncode(memArraysFrom.Text)
  29.       +'&sl='+cArrayShortLanguages[cobArrayFrom.ItemIndex]
  30.       +'&tl='+cArrayShortLanguages[cobArrayTo.ItemIndex]
  31.       +'&dt=t'
  32.       +'&ie=UTF-8&oe=UTF-8'
  33.       ;
  34.  
  35.     //ShowMessage(URL);
  36.  
  37.     strResponse:= CallGoogleTranslate(URL);
  38.     try
  39.       jdResponse:= GetJSON(strResponse);
  40.  
  41.       //memArraysTo.Append(jdResponse.FormatJSON); exit;
  42.  
  43.       jdTranslation:= jdResponse.FindPath('[0]');
  44.       if (jdTranslation <> nil) and (jdTranslation.JSONType = jtArray) then
  45.       begin
  46.         jaTranslation:= TJSONArray(jdTranslation);
  47.         for index:= 0 to Pred(jaTranslation.Count) do
  48.         begin
  49.           jdTranslationArray:= jaTranslation[Index];
  50.           if (jdTranslationArray <> nil) and (jdTranslationArray.JSONType = jtArray) then
  51.           begin
  52.             jaTranslationArray:= TJSONArray(jdTranslationArray);
  53.             memArraysTo.Append(Trim(jaTranslationArray[0].AsString));
  54.           end;
  55.         end;
  56.         if cobArrayFrom.ItemIndex = 0 then
  57.         begin
  58.            panArrayDetectedLanguage.Caption:= Format(cSourceLanguage, [
  59.            ShortCodetoLongCode(jdResponse.FindPath('[2]').AsString)
  60.           ]);
  61.         end;
  62.       end;
  63.     finally
  64.       jdResponse.Free;
  65.     end;
  66.  
  67.   finally
  68.     Application.ProcessMessages;
  69.     actArraysTranslate.Enabled:= True;
  70.   end;
  71. end;

This is the same call code but parsing the JSON object result:
Code: Pascal  [Select][+][-]
  1. procedure TfrmMain.actObjectTranslateExecute(Sender: TObject);
  2. var
  3.   URL: String;
  4.   Index: integer;
  5.   strResponse: TJSONStringType;
  6.   jdResponse: TJSONData;
  7.   joTranslation, joSentence: TJSONObject;
  8.   jaSentencesArray: TJSONArray;
  9. begin
  10.   actObjectTranslate.Enabled:= False;
  11.   memObjectTo.Clear;
  12.   panObjectDetectedLanguage.Caption:= EmptyStr;
  13.   Application.ProcessMessages;
  14.   try
  15.  
  16.     if Length(memObjectFrom.Text) = 0 then
  17.     begin
  18.       ShowMessage('Need something to translate');
  19.       exit;
  20.     end;
  21.  
  22.     if (cobObjectFrom.ItemIndex <= 0) and (cobObjectTo.ItemIndex <= 0) then
  23.     begin
  24.       ShowMessage('Cannot have language detection on both sides');
  25.       exit;
  26.     end;
  27.  
  28.     URL:='https://translate.googleapis.com/translate_a/single?client=gtx'
  29.       +'&q='+HTTPEncode(memObjectFrom.Text)
  30.       +'&sl='+cArrayShortLanguages[cobObjectFrom.ItemIndex]
  31.       +'&tl='+cArrayShortLanguages[cobObjectTo.ItemIndex]
  32.       +'&dt=t&dj=1'
  33.       +'&ie=UTF-8&oe=UTF-8'
  34.       ;
  35.  
  36.     //ShowMessage(URL);
  37.  
  38.     strResponse:= CallGoogleTranslate(URL);
  39.     try
  40.       jdResponse:= GetJSON(strResponse);
  41.  
  42.       //memObjectTo.Append(jdResponse.FormatJSON); exit;
  43.  
  44.       if (jdResponse <> nil) and (jdResponse.JSONType = jtObject) then
  45.       begin
  46.         joTranslation:= TJSONObject(jdResponse);
  47.         jaSentencesArray:= TJSONArray(joTranslation.FindPath(cJSONSentences));
  48.         for Index:=0 to Pred(jaSentencesArray.Count) do
  49.         begin
  50.           joSentence:= TJSONObject(jaSentencesArray[Index]);
  51.           memObjectTo.Append(Trim(joSentence.Get(cJSONTranslation,'')));
  52.         end;
  53.         if cobObjectFrom.ItemIndex = 0 then
  54.         begin
  55.           panObjectDetectedLanguage.Caption:= Format(cSourceLanguage, [
  56.             ShortCodetoLongCode(joTranslation.Get(cJSONSource,''))
  57.           ]);
  58.         end;
  59.       end;
  60.     finally
  61.       jdResponse.Free;
  62.     end;
  63.  
  64.   finally
  65.     Application.ProcessMessages;
  66.     actObjectTranslate.Enabled:= True;
  67.   end;
  68. end;

I had fun doing it and I hope this can prove useful to someone.

Cheers,
Gus
« Last Edit: December 15, 2021, 12:58:38 am by Gustavo 'Gus' Carreno »
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2032
  • Former Delphi 1-7, 10.2 user
Re: extract data from json ? pls help
« Reply #16 on: December 15, 2021, 01:04:21 am »
Why not improve the Wiki for future users who may not recognise the example has an issue? It may even be worth pointing out the issue with the example and providing your improved example too.

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1248
  • Professional amateur ;-P
Re: extract data from json ? pls help
« Reply #17 on: December 15, 2021, 01:11:49 am »
Hey Trev,

Why not improve the Wiki for future users who may not recognise the example has an issue? It may even be worth pointing out the issue with the example and providing your improved example too.

Crap, I'm now kicking myself in the arse for not thinking about that  :-[

Thanks, Trev, for the suggestion. I'll have a go at it after I put some other fires out.

When you say point to my improved example, should I point to the GitHub repo?
I'm always thinking that my code is a bit stinky for such official medium....

Cheers,
Gus
« Last Edit: December 15, 2021, 01:50:19 am by Gustavo 'Gus' Carreno »
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1272
Re: extract data from json ? pls help
« Reply #18 on: December 15, 2021, 02:09:59 am »
hello,
good job Gus !
I have some troubles with your project when resizing the main form :
There are some pieces of the form that are not refreshed (see attachment).
It seems to be in the TPairSplitter component.
Lazarus 2.0.12 windows 10

Friendly, J.P

Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1248
  • Professional amateur ;-P
Re: extract data from json ? pls help
« Reply #19 on: December 15, 2021, 02:22:37 am »
Hey J.P.,

good job Gus !

Thank you so very much :D

I have some troubles with your project when resizing the main form :
There are some pieces of the form that are not refreshed (see attachment).
It seems to be in the TPairSplitter component.

Humm, that's rather troubling...
I did have someone mention that they didn't like the TPairSplitter, but they never elaborated.
If this is a common occurrence with TPaiSpltter, no wonder that person gave up on it, eheheh

In Linux there's no refresh issue.
Could you please mess about with the app to see if you can mitigate that issue?
I'll gladly accept a PR on the GitHub repo if you're able to fix it :)

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2032
  • Former Delphi 1-7, 10.2 user
Re: extract data from json ? pls help
« Reply #20 on: December 15, 2021, 04:08:12 am »
Why not improve the Wiki for future users who may not recognise the example has an issue? It may even be worth pointing out the issue with the example and providing your improved example too.

When you say point to my improved example, should I point to the GitHub repo?

If you can provide a simple standalone unit as was done for the original example, I would do so. There's no reason why you can't also point to your GitHub repo for a full project.

Quote
I'm always thinking that my code is a bit stinky for such official medium....

If it's in the Wiki, someone else may come along in future and deodorise it for you :)

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1248
  • Professional amateur ;-P
Re: extract data from json ? pls help
« Reply #21 on: December 15, 2021, 05:47:20 am »
Hey Jamie,

The problem is that Delphi hacked the Onresize event because windows does not send that event with mouse move, it gets sent when mouse is released or when sizing is completed.
Otherwise a lot of unwanted effects take place..

This is good to know, many thanks Jamie!!!

Below is an alteration of that project that helps with that.

Thanks for the change. It has to be surrounded with appropriate {$IFDEF}, but it works fine on both a Linux build and a Windows build via wine

I'll make the changes and update the GitHub repo and will give you credit.
The best I can do since you didn't go the PR path :)

Again, many thanks for the tweak, really appreciate it !!

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1248
  • Professional amateur ;-P
Re: extract data from json ? pls help
« Reply #22 on: December 15, 2021, 05:52:45 am »
Hey Trev,

If you can provide a simple standalone unit as was done for the original example, I would do so. There's no reason why you can't also point to your GitHub repo for a full project.

Thanks for the tips. I was already thinking about a bit of an edit on the article.
And let's see how my rusty French performs in also changing the FR version.
And while I'm at it, and I'm Portuguese, let's also drop a PT version :)

And yeah, I'll probably will point towards the GH repo, maybe at the end in the attachments or footer section...

If it's in the Wiki, someone else may come along in future and deodorise it for you :)

That's actually quite a nice way of putting it, thanks, LOL!!!  :D

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1248
  • Professional amateur ;-P
Re: extract data from json ? pls help
« Reply #23 on: December 15, 2021, 06:05:59 am »
Hey J.P.,

I've updated the repo with Jamie's code suggestions and released version v0.4.
Could you be so kind as to have a go at it and see if the weirdness is gone?

I'm also attaching the 0.4 version here.

Many thanks!!

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1272
Re: extract data from json ? pls help
« Reply #24 on: December 15, 2021, 09:45:26 am »
hello,
the unit Lmessages is missing in the uses.
With this unit and the Jamie's code it'is OK !  No weirdness !  :)
Friendly, J.P
« Last Edit: December 15, 2021, 09:47:55 am by Jurassic Pork »
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1248
  • Professional amateur ;-P
Re: extract data from json ? pls help
« Reply #25 on: December 15, 2021, 12:04:51 pm »
Hey J.P.,

the unit Lmessages is missing in the uses.

Sheeeshh, I'm dropping the ball all over the place today :(
Thanks for the correction!!

I've corrected it on the GitHub repo and I'm attaching v0.5.

With this unit and the Jamie's code it'is OK !  No weirdness !  :)

Good to know!!
I really appreciate your time and effort in the testing, thanks so very much!!

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1248
  • Professional amateur ;-P
Re: extract data from json ? pls help
« Reply #26 on: December 16, 2021, 06:17:54 am »
Hey Y'all,

I've updated the English version of the page: https://wiki.lazarus.freepascal.org/Using_Google_Translate

I'll also be updating the French version in order to maintain both in synch.

I'll also provide with a Portuguese version.

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1248
  • Professional amateur ;-P
Re: extract data from json ? pls help
« Reply #27 on: December 16, 2021, 06:47:01 am »
Hey Y'all,

I've updated the French version of the page: https://wiki.lazarus.freepascal.org/Using_Google_Translate/fr

I would really appreciate that a native speaker would give it a good gloss over to see if my lazy Google Translate with a side ways view with my rusty French isn't that bad.

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1248
  • Professional amateur ;-P
Re: extract data from json ? pls help
« Reply #28 on: December 16, 2021, 07:15:59 am »
Hey Y'all,

Portuguese version of the page is online: https://wiki.lazarus.freepascal.org/Using_Google_Translate/pt

Funny enough, the template it self has the link to the Portuguese language, but none of the translations will show it.
Bummer...

Oh well, it's done.

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1272
Re: extract data from json ? pls help
« Reply #29 on: December 16, 2021, 08:59:16 am »
hello Gus,
I've updated the French version of the page: https://wiki.lazarus.freepascal.org/Using_Google_Translate/fr
I would really appreciate that a native speaker would give it a good gloss over to see if my lazy Google Translate with a side ways view with my rusty French isn't that bad.
Thanks for this. I am a french guy and i will have a look on you wiki french page.
Don't forget one thing :
Quote
translate.googleapis.com site use is very limited. It only allows about 100 requests per one hour period and there after returns a 429 error (Too many requests)

Friendly, J.P
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

 

TinyPortal © 2005-2018