Recent

Author Topic: JSON count how many arrays are with an array  (Read 596 times)

alvas

  • New member
  • *
  • Posts: 9
JSON count how many arrays are with an array
« on: April 01, 2023, 10:54:02 am »
Hi,

I apologise in advance.  I have a rather complex JSON file and I am just sinking my teeth into JSON pretty much for the first time.  I feel this may be an easy question but I can't for the life of me find an example.  I just want to find out within a TJSONData instance whether there are any more arrays (and how many) within or if there are just objects.  I'm guessing I could probably do a search for '[' in the string but just wondering whether there is a cleaner way.  Thank you.

paweld

  • Hero Member
  • *****
  • Posts: 970
Re: JSON count how many arrays are with an array
« Reply #1 on: April 01, 2023, 01:22:12 pm »
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, fphttpclient, opensslsockets, fpjson, StrUtils;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     Memo1: TMemo;
  17.     Memo2: TMemo;
  18.     procedure Button1Click(Sender: TObject);
  19.   private
  20.  
  21.   public
  22.     procedure GetJsonInfo(jd: TJSONData; level: Integer);
  23.   end;
  24.  
  25. var
  26.   Form1: TForm1;
  27.  
  28. implementation
  29.  
  30. {$R *.lfm}
  31.  
  32. { TForm1 }
  33.  
  34. var
  35.   a_count, o_count, s_count, n_count, b_count, e_count, u_count: Integer;
  36.  
  37. procedure TForm1.Button1Click(Sender: TObject);
  38. var
  39.   hc: TFPHttpClient;
  40.   jd: TJSONData;
  41. begin
  42.   a_count := 0;
  43.   o_count := 0;
  44.   s_count := 0;
  45.   n_count := 0;
  46.   b_count := 0;
  47.   e_count := 0;
  48.   u_count := 0;
  49.   hc := TFPHttpClient.Create(nil);
  50.   hc.AllowRedirect := True;
  51.   Memo1.Lines.Text := hc.Get('https://docs.google.com/uc?export=download&id=1G_9faCq0UbTjFVB6UpMASq5rQrzrN9OU');
  52.   hc.Free;
  53.   jd := GetJSON(Memo1.Lines.Text);
  54.   Memo2.Lines.Clear;
  55.   Memo2.Lines.BeginUpdate;
  56.   GetJsonInfo(jd, 0);
  57.   Memo2.Lines.EndUpdate;
  58.   Memo2.Lines.Add(Format('Count: array - %d, object - %d, string - %d, number - %d, boolean - %d, null - %d, unknown - %d',
  59.     [a_count, o_count, s_count, n_count, b_count, e_count, u_count]));
  60.   jd.Free;
  61. end;
  62.  
  63. procedure TForm1.GetJsonInfo(jd: TJSONData; level: Integer);
  64. var
  65.   i: Integer;
  66.   s: String;
  67.   jo: TJSONObject;
  68. begin
  69.   jo := TJSONObject(jd);
  70.   for i := 0 to jo.Count - 1 do
  71.   begin
  72.     s := IntToStr(level) + ': ';
  73.     if level > 0 then
  74.       s := s + DupeString('   ', level);
  75.     case jo.Items[i].JSONType of
  76.       jtUnknown:
  77.         begin
  78.           Inc(u_count);
  79.           s := s + jo.Names[i] + ' > ' + 'UNKNOWN';
  80.         end;
  81.       jtNumber:
  82.         begin
  83.           Inc(n_count);
  84.           s := s + jo.Names[i] + ' > ' + 'NUMBER';
  85.         end;
  86.       jtString:
  87.         begin
  88.           Inc(s_count);
  89.           s := s + jo.Names[i] + ' > ' + 'STRING';
  90.         end;
  91.       jtBoolean:
  92.         begin
  93.           Inc(b_count);
  94.           s := s + jo.Names[i] + ' > ' + 'BOOLEAN';
  95.         end;
  96.       jtNull:
  97.         begin
  98.           Inc(e_count);
  99.           s := s + jo.Names[i] + ' > ' + 'NULL';
  100.         end;
  101.       jtArray:
  102.         begin
  103.           Inc(a_count);
  104.           s := s + jo.Names[i] + ' > ' + 'ARRAY count ' + IntToStr(jo.Items[i].Count);
  105.           GetJsonInfo(jo.Items[i], level + 1);
  106.         end;
  107.       jtObject:
  108.         begin
  109.           Inc(o_count);
  110.           s := s + ' > ' + 'OBJECT';
  111.           GetJsonInfo(jo.Items[i], level + 1);
  112.         end;
  113.       else
  114.       begin
  115.         Inc(u_count);
  116.         s := s + jo.Names[i] + ' > ' + 'UNKNOWN';
  117.       end;
  118.     end;
  119.     Memo2.Lines.Add(s);
  120.   end;
  121. end;
  122.  
  123. end.
Best regards / Pozdrawiam
paweld

alvas

  • New member
  • *
  • Posts: 9
Re: JSON count how many arrays are with an array
« Reply #2 on: April 02, 2023, 12:19:43 am »
Fantastic, thank you

 

TinyPortal © 2005-2018