Recent

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

yahoo000

  • New Member
  • *
  • Posts: 21
extract data from json ? pls help
« on: December 13, 2021, 05:10:15 pm »
Can someone help me extract data from this json example

below is the json generated from google translate, I would like to extract polish translated content, without english content

so I want to extract only these :

"jak się masz\r\n"
"jak masz na imię"

Code: Pascal  [Select][+][-]
  1. [[["jak się masz\r\n","how are you\r\n",null,null,10,null,null,null,[[null,true]]],["jak masz na imię","what is your name",null,null,1]],null,"en",null,null,null,null,[]]
  2.  

I tried to write code by myself, unfortunately nothing works, pls help

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: extract data from json ? pls help
« Reply #1 on: December 13, 2021, 06:02:10 pm »
http://www.getlazarus.org/json will help

Also would help to give the properly formatted json string
« Last Edit: December 13, 2021, 06:15:18 pm by pcurtis »
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: extract data from json ? pls help
« Reply #2 on: December 13, 2021, 06:10:32 pm »
Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   SysUtils;
  7.  
  8. const
  9.  
  10.   raw: String = '[[["jak się masz\r\n","how are you\r\n",null,null,10,null,null,null,[[null,true]]],["jak masz na imię","what is your name",null,null,1]],null,"en",null,null,null,null,[]]';
  11.  
  12.  
  13. function GetQuotedsWithJak(const aText: String): TStringArray;
  14. var
  15.   p, quoteStart, quoteEnd: Integer;
  16.   withinQuote: Boolean = False;
  17.   extract: String;
  18. begin
  19.   p := 0;
  20.   SetLength(Result, 0);
  21.   while p < Length(aText) do
  22.     begin
  23.       Inc(p);
  24.       if aText[p] = '"' then
  25.         begin
  26.           withinQuote := not withinQuote;
  27.           case withinQuote of
  28.             True:  quoteStart := p;
  29.             False: quoteEnd := p;
  30.           end;
  31.           if not withinQuote then
  32.             begin
  33.               extract := Copy(aText, Succ(quoteStart), Pred(quoteEnd - quoteStart));
  34.               if Pos('jak', extract) > 0 then
  35.                 begin
  36.                   SetLength(Result, Succ(Length(Result)));
  37.                   Result[High(Result)] := extract;
  38.                 end;
  39.             end;
  40.         end;
  41.     end;
  42. end;
  43.  
  44. var
  45.   s: String;
  46.  
  47. begin
  48.   for s in GetQuotedsWithJak(raw) do
  49.     WriteLn(s);
  50.   ReadLn;
  51. end.

yahoo000

  • New Member
  • *
  • Posts: 21
Re: extract data from json ? pls help
« Reply #3 on: December 13, 2021, 06:55:23 pm »
thank you for help, but it cannot be based on the word "Jak" .. the same two words "Jak" are just coincidence

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: extract data from json ? pls help
« Reply #4 on: December 13, 2021, 07:05:40 pm »
Code: Pascal  [Select][+][-]
  1. uses
  2. ...,jsontools,...
  3.  
  4. procedure TForm1.Button1Click(Sender: TObject);
  5. var
  6.   MyJSON : TJsonNode;
  7. begin
  8.   MyJSON := TJsonNode.Create;
  9.   MyJSON.LoadFromFile('test.json');
  10.  
  11.         MyJSON := MyJSON.Child(0);
  12.         MyJSON := MyJSON.Child(0);
  13.         MyJSON := MyJSON.Child(0);
  14.         ShowMessage(MyJSON.AsString);
  15.         MyJSON := MyJSON.Root;
  16.         MyJSON := MyJSON.Child(0);
  17.         MyJSON := MyJSON.Child(1);
  18.         MyJSON := MyJSON.Child(0);
  19.         ShowMessage(MyJSON.AsString);
  20.  
  21.   MyJSON.Free;
  22. end;
  23.  
  24.  
« Last Edit: December 13, 2021, 07:13:56 pm by pcurtis »
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

GhostlyMan

  • New Member
  • *
  • Posts: 14
Re: extract data from json ? pls help
« Reply #5 on: December 13, 2021, 07:28:13 pm »
The string you provide is not valid JSON.

JSON is formed by name-value pairs, such as:
Code: Javascript  [Select][+][-]
  1. {
  2.   "firstName": "John",
  3.   "lastName": "Smith",
  4.   "isAlive": true,
  5.   "age": 27,
  6.   "address": {
  7.     "streetAddress": "21 2nd Street",
  8.     "city": "New York",
  9.     "state": "NY",
  10.     "postalCode": "10021-3100"
  11.   },
  12.   "phoneNumbers": [
  13.     {
  14.       "type": "home",
  15.       "number": "212 555-1234"
  16.     },
  17.     {
  18.       "type": "office",
  19.       "number": "646 555-4567"
  20.     }
  21.   ],
  22.   "children": [],
  23.   "spouse": null
  24. }

If you want a JSON response from Google Translate it seems that you have to adjust some of your query parameters.
See:
https://stackoverflow.com/questions/26714426/what-is-the-meaning-of-google-translate-query-params
https://stackoverflow.com/questions/10334358/how-to-get-and-parse-json-answer-from-google-translate

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: extract data from json ? pls help
« Reply #6 on: December 13, 2021, 07:30:36 pm »
Your correct but JSON tools handled it.
« Last Edit: December 13, 2021, 07:32:25 pm by pcurtis »
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

GhostlyMan

  • New Member
  • *
  • Posts: 14
Re: extract data from json ? pls help
« Reply #7 on: December 13, 2021, 07:39:04 pm »
@pcurtis

What is the result of jsontools loading and parsing the test string?

Can you pretty print it - I don't have jsontools

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: extract data from json ? pls help
« Reply #8 on: December 13, 2021, 07:42:51 pm »
jak się masz
"jak masz na imię"
with
 ShowMessage(MyJSON.AsString);


"jak się masz\r\n"
"jak masz na imię"
with
 ShowMessage(MyJSON.AsJson);
« Last Edit: December 13, 2021, 07:47:15 pm by pcurtis »
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

GhostlyMan

  • New Member
  • *
  • Posts: 14
Re: extract data from json ? pls help
« Reply #9 on: December 13, 2021, 07:46:57 pm »
I meant the full JSON output from MyJSON.LoadFromFile('test.json');

GhostlyMan

  • New Member
  • *
  • Posts: 14
Re: extract data from json ? pls help
« Reply #10 on: December 13, 2021, 08:05:59 pm »
Code: Pascal  [Select][+][-]
  1. ...
  2.   MyJSON.LoadFromFile('test1.json');
  3.   WriteLn(MyJSON.Value);  
  4. ...

Outputs:
Code: Text  [Select][+][-]
  1. [
  2.         [
  3.                 [
  4.                         "jak się masz\r\n",
  5.                         "how are you\r\n",
  6.                         null,
  7.                         null,
  8.                         10,
  9.                         null,
  10.                         null,
  11.                         null,
  12.                         [
  13.                                 [
  14.                                         null,
  15.                                         true
  16.                                 ]
  17.                         ]
  18.                 ],
  19.                 [
  20.                         "jak masz na imię",
  21.                         "what is your name",
  22.                         null,
  23.                         null,
  24.                         1
  25.                 ]
  26.         ],
  27.         null,
  28.         "en",
  29.         null,
  30.         null,
  31.         null,
  32.         null,
  33.         [ ]
  34. ]
  35.  

So, jsontools reads the file but ignores the name part of the name-data pair.

Interesting.

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1353
  • Professional amateur ;-P
Re: extract data from json ? pls help
« Reply #11 on: December 14, 2021, 03:12:03 am »
Hey yahoo000,

The result returned from the translate API is valid JSON. Just because it's a 3-4 level deep of arrays it doesn't mean it's not valid JSON.
Now, the problem is that being only arrays, you don't really have a sense of what the data is, but for what you ask, it doesn't really matter.

You want the 2 strings that are translated into Polish.

Ok, let's then follow the multiple layers of the arrays:

Code: Pascal  [Select][+][-]
  1. uses
  2.   ..., fpjson, ...
  3. const
  4.   cjResponse = '[[["jak się masz\r\n","how are you\r\n",null,null,10,null,null,null,[[null,true]]],["jak masz na imię","what is your name",null,null,1]],null,"en",null,null,null,null,[]]';
  5. var
  6.   InputJSON: TJSONData;
  7.   strTranslation0, strTranslation1: String;
  8. begin
  9.   InputJSON:= GetJSON(cjResponse);
  10.   strTranslation0:= InputJSON.FindPath('[0][0][0]').AsString; // this should get >jak się masz\r\n<
  11.   strTranslation1:= InputJSON.FindPath('[0][1][0]').AsString; // this should get >jak masz na imię<
  12.   InputJSON.Free;
  13. end;

I would try and have a look at what @GhostlyMan mentions about having a different output from the API.
Maybe having objects in those arrays with descriptive members would be better to grok.

Hope this helps.

Cheers,
Gus
« Last Edit: December 14, 2021, 03:48:32 am by Gustavo 'Gus' Carreno »

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1290
Re: extract data from json ? pls help
« Reply #12 on: December 14, 2021, 07:12:52 am »
hello,
you can also use the extract part of the code of the Using Google Translate  lazarus wiki page. The whole code doesn't work anymore.
Here is a code working with Lazarus 2.0.12 :
Code: Pascal  [Select][+][-]
  1. implementation
  2. uses
  3.   lazlogger, fpjson, fphttpclient, opensslsockets, HTTPDefs;
  4.  
  5. {$R *.lfm}
  6.  
  7.  
  8. function DownloadText(const URL: string): TStrings;
  9. var
  10.   client: TFPHTTPClient;
  11.   doc: TStringList;
  12. begin
  13.   Result:=nil;
  14.   doc:=TStringList.Create;
  15.   client:=TFPHTTPClient.Create(nil);
  16.   try
  17.     client.Get(URL,doc);
  18.     Result:=doc;
  19.     doc:=nil;
  20.   finally
  21.     doc.Free;
  22.     client.Free;
  23.   end;
  24. end;
  25.  
  26. function AskGoogleTranslate(OriginalText, SourceLang, TargetLang: string;
  27.   out TranslatedText, ErrorMessage: string): boolean;
  28. var
  29.   doc: TStrings;
  30.   URL: String;
  31.   JSON: String;
  32.   p: PChar;
  33.   i: Integer;
  34.   StartPos: PChar;
  35.   Line: TJSONStringType;
  36.   Level: Integer;
  37. begin
  38.   Result:=false;
  39.   ErrorMessage:='';
  40.   TranslatedText:='';
  41.   if (SourceLang='') or (TargetLang='') then begin
  42.     ErrorMessage:='missing language parameter';
  43.     exit;
  44.   end;
  45.   if OriginalText='' then exit;
  46.  
  47.   // download JSON
  48.   URL:='https://translate.googleapis.com/translate_a/single?client=gtx'
  49.     +'&q='+HTTPEncode(OriginalText)
  50.     +'&sl='+SourceLang
  51.     +'&tl='+TargetLang
  52.     +'&dt=t'
  53.     +'&ie=UTF-8&oe=UTF-8'
  54.     ;
  55.   try
  56.     doc:=DownloadText(URL);
  57.   except
  58.     on E: Exception do begin
  59.       Form1.Memo2.Append('AskGoogleTranslate failed URL=' + URL +' Error=' + E.Message);
  60.       ErrorMessage:='query failed: '+E.Message;
  61.       exit;
  62.     end;
  63.   end;
  64.   try
  65.     // parse JSON
  66.     // For example:
  67.     //  [[["Erster Satz . ","First sentence.","",""],["Zweiter Satz . ","Second sentence.","",""]],
  68.     JSON:=doc.Text;
  69.     p:=PChar(JSON);
  70.     Level:=0;
  71.     i:=0;
  72.     repeat
  73.       case p^ of
  74.       #0: break;
  75.       '[':
  76.         begin
  77.           inc(Level);
  78.           i:=0;
  79.         end;
  80.       ']':
  81.         begin
  82.           dec(Level);
  83.           if Level=1 then break;
  84.         end;
  85.       ',':
  86.         inc(i);
  87.       '"':
  88.         begin
  89.           inc(p);
  90.           StartPos:=p;
  91.           repeat
  92.             case p^ of
  93.             #0,'"': break;
  94.             '\':
  95.               begin
  96.                 inc(p);
  97.                 if p^=#0 then break;
  98.               end;
  99.             end;
  100.             inc(p);
  101.           until false;
  102.           if i=0 then begin
  103.             Line:=JSONStringToString(copy(JSON,StartPos-PChar(JSON)+1,p-StartPos));
  104.             TranslatedText:=TranslatedText+Line;
  105.           end;
  106.           if p^=#0 then break;
  107.         end;
  108.       end;
  109.       inc(p);
  110.     until false;
  111.   finally
  112.     doc.Free;
  113.   end;
  114.   Result:=true;
  115. end;
  116.  
  117. { TForm1 }
  118.  
  119. procedure TForm1.Button1Click(Sender: TObject);
  120. var
  121.   ErrorMessage: string;
  122.   TranslatedText: string;
  123. begin
  124.   if not AskGoogleTranslate(Memo1.Lines.Text,'en','pl',TranslatedText,ErrorMessage)
  125.   then begin
  126.     Memo2.Append('AskGoogleTranslate failed: ' + ErrorMessage);
  127.   end else begin
  128.     Memo2.Append(TranslatedText);
  129.   end;
  130.  
  131. end;

Friendly, J.P

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

Roland57

  • Hero Member
  • *****
  • Posts: 605
    • msegui.net
Re: extract data from json ? pls help
« Reply #13 on: December 14, 2021, 12:02:40 pm »
Here is a code working with Lazarus 2.0.12 :

Great!
My projects are on Codeberg.

ftplazarus

  • Newbie
  • Posts: 4
Re: extract data from json ? pls help
« Reply #14 on: December 14, 2021, 03:13:23 pm »
hello,
you can also use the extract part of the code of the Using Google Translate  lazarus wiki page. The whole code doesn't work anymore.
Here is a code working with Lazarus 2.0.12 :
Code: Pascal  [Select][+][-]
  1. implementation
  2. uses
  3.   lazlogger, fpjson, fphttpclient, opensslsockets, HTTPDefs;
  4.  
  5. {$R *.lfm}
  6.  
  7.  
  8. function DownloadText(const URL: string): TStrings;
  9. var
  10.   client: TFPHTTPClient;
  11.   doc: TStringList;
  12. begin
  13.   Result:=nil;
  14.   doc:=TStringList.Create;
  15.   client:=TFPHTTPClient.Create(nil);
  16.   try
  17.     client.Get(URL,doc);
  18.     Result:=doc;
  19.     doc:=nil;
  20.   finally
  21.     doc.Free;
  22.     client.Free;
  23.   end;
  24. end;
  25.  
  26. function AskGoogleTranslate(OriginalText, SourceLang, TargetLang: string;
  27.   out TranslatedText, ErrorMessage: string): boolean;
  28. var
  29.   doc: TStrings;
  30.   URL: String;
  31.   JSON: String;
  32.   p: PChar;
  33.   i: Integer;
  34.   StartPos: PChar;
  35.   Line: TJSONStringType;
  36.   Level: Integer;
  37. begin
  38.   Result:=false;
  39.   ErrorMessage:='';
  40.   TranslatedText:='';
  41.   if (SourceLang='') or (TargetLang='') then begin
  42.     ErrorMessage:='missing language parameter';
  43.     exit;
  44.   end;
  45.   if OriginalText='' then exit;
  46.  
  47.   // download JSON
  48.   URL:='https://translate.googleapis.com/translate_a/single?client=gtx'
  49.     +'&q='+HTTPEncode(OriginalText)
  50.     +'&sl='+SourceLang
  51.     +'&tl='+TargetLang
  52.     +'&dt=t'
  53.     +'&ie=UTF-8&oe=UTF-8'
  54.     ;
  55.   try
  56.     doc:=DownloadText(URL);
  57.   except
  58.     on E: Exception do begin
  59.       Form1.Memo2.Append('AskGoogleTranslate failed URL=' + URL +' Error=' + E.Message);
  60.       ErrorMessage:='query failed: '+E.Message;
  61.       exit;
  62.     end;
  63.   end;
  64.   try
  65.     // parse JSON
  66.     // For example:
  67.     //  [[["Erster Satz . ","First sentence.","",""],["Zweiter Satz . ","Second sentence.","",""]],
  68.     JSON:=doc.Text;
  69.     p:=PChar(JSON);
  70.     Level:=0;
  71.     i:=0;
  72.     repeat
  73.       case p^ of
  74.       #0: break;
  75.       '[':
  76.         begin
  77.           inc(Level);
  78.           i:=0;
  79.         end;
  80.       ']':
  81.         begin
  82.           dec(Level);
  83.           if Level=1 then break;
  84.         end;
  85.       ',':
  86.         inc(i);
  87.       '"':
  88.         begin
  89.           inc(p);
  90.           StartPos:=p;
  91.           repeat
  92.             case p^ of
  93.             #0,'"': break;
  94.             '\':
  95.               begin
  96.                 inc(p);
  97.                 if p^=#0 then break;
  98.               end;
  99.             end;
  100.             inc(p);
  101.           until false;
  102.           if i=0 then begin
  103.             Line:=JSONStringToString(copy(JSON,StartPos-PChar(JSON)+1,p-StartPos));
  104.             TranslatedText:=TranslatedText+Line;
  105.           end;
  106.           if p^=#0 then break;
  107.         end;
  108.       end;
  109.       inc(p);
  110.     until false;
  111.   finally
  112.     doc.Free;
  113.   end;
  114.   Result:=true;
  115. end;
  116.  
  117. { TForm1 }
  118.  
  119. procedure TForm1.Button1Click(Sender: TObject);
  120. var
  121.   ErrorMessage: string;
  122.   TranslatedText: string;
  123. begin
  124.   if not AskGoogleTranslate(Memo1.Lines.Text,'en','pl',TranslatedText,ErrorMessage)
  125.   then begin
  126.     Memo2.Append('AskGoogleTranslate failed: ' + ErrorMessage);
  127.   end else begin
  128.     Memo2.Append(TranslatedText);
  129.   end;
  130.  
  131. end;

Friendly, J.P

Hello,
It may happen that the Json string is more complicated that in your exemple, especially if there are many lines to translate.
So I suggest to put, line 102 of your code :
 instead of  if i=0 then begin, put
if ((i=0)AND(Level=3)) then begin
In that case, only the relevant strings are copied.

Regards,


 

TinyPortal © 2005-2018