[Solved]How to decode Json lazarus / 21-General ---------------------------------------------------------------------- arneolav · 2022-02-06 21:51 Json should be simple, but after a all Sunday hours I've so far given up, (I'm a newbie with Json). Any idea how to get this to work? And I believe this can be done in a more clever way. Thank's in advance for your time and attention. ----------- Look down at the solution by avk . ---------------------------------------------------------------------- zoltanleo · 2022-02-07 10:27 Hi arneolav Try to use 3d party jsontool https://forum.lazarus.freepascal.org/index.php/topic,46533.0.html ---------------------------------------------------------------------- arneolav · 2022-02-07 11:27 Hi! Thank's. I'v tried but run into some problem. I will take a closer look and try it again. ---------------------------------------------------------------------- avk · 2022-02-07 13:10 Maybe try something like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);type  TData = record    total,  energy,    tax: double;    startsAt: string;  end;var  s: string;  d, o: TJsonData;  ja: TJsonArray;  jo: TJsonObject;  a: array of TData;  I: Integer;begin  s := '{"data":{"viewer":{"homes":[{"currentSubscript ion":{"priceInfo":{"current":{"total":1.4904,"energy":1.1843,"tax":0.3 061,"startsAt":"2022-02- 05T11:00:00.000+01:00"}}}},{"currentSubscription":{"priceInfo":{"curre nt":{"total":1.4904,"energy":1.1843,"tax":0.3061,"startsAt":"2022-02- 05T11:00:00.000+01:00"}}}}]}}}';  d := GetJSON(s);  o := d.FindPath('data.viewer.homes');  if (o <> nil) and (o.JSONType = jtArray) then begin    ja := TJsonArray(o);    SetLength(a, ja.Count);    for I := 0 to ja.Count - 1 do begin      o := ja.Objects[I].FindPath('currentSubscription.priceInfo.current');      if (o <> nil) and (o.JSONType = jtObject) then begin        jo := TJsonObject(o);        with a[I] do begin          total := jo.Floats['total'];          energy := jo.Floats['energy'];          tax := jo.Floats['tax'];          startsAt := jo.Strings['startsAt'];        end;      end;    end;  end;  d.Free;   for I := 0 to High(a) do begin    Memo1.Append('Item ' + I.ToString);    Memo1.Append('  total: ' + a[I].total.ToString);    Memo1.Append('  energy: ' + a[I].energy.ToString);    Memo1.Append('  tax: ' + a[I].tax.ToString);    Memo1.Append('  startsAt: ' + a[I].startsAt);  end;end;   Error handling is at your discretion, it prints Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Item 0  total: 1,4904  energy: 1,1843  tax: 0,3061  startsAt: 2022-02-05T11:00:00.000+01:00Item 1  total: 1,4904  energy: 1,1843  tax: 0,3061  startsAt: 2022-02-05T11:00:00.000+01:00]  BTW, you can see that in my locale the decimal separator is a comma. Because JsonTools has some issues with floating point, it will print in this case: Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Item 0  total: 0  energy: 0  tax: 0  startsAt: 2022-02-05T11:00:00.000+01:00Item 1  total: 0  energy: 0  tax: 0  startsAt: 2022-02-05T11:00:00.000+01:00 ---------------------------------------------------------------------- balazsszekely · 2022-02-07 13:20 @arneolav 1. Go to Lazarus Menu->Tools->Example Projects 2. Check "Include all subdirectories" 3. Type "json" in the filter 4. Open project jsonviewer.lpi 5. Compile, build then run the project 6. Go to Options, check "Strict JSON" and "Compact display" 7. Save your json to a file, the open via File->Open PS: It can parse almost any json you throw at it. ---------------------------------------------------------------------- arneolav · 2022-02-07 13:39 Thanks a lot avk! This is close to my last try, but I was (among other errors) confused in the find: Did this fatal error: FindPath('data/viewer/homes'), this does work in function Exists! Then we have mostly all we need to get values from Tibber; And thank's to GetMem,  I did try jsonviewer but spent more time JsonTools. And was totally lost for some hours...