Recent

Author Topic: Json unnamed array access  (Read 624 times)

zariq

  • Full Member
  • ***
  • Posts: 109
Json unnamed array access
« on: February 14, 2020, 05:29:19 pm »
Hello.

I need some help with parsing a Json file I'm having trouble with. It's not very structured. Apart from the first four fields, the other's are not always present. Even when they are present, they can vary between records. This is a sample of the file.

Code: Text  [Select][+][-]
  1. {
  2.     "word": "elephant",
  3.     "lang": "English",
  4.     "heads": [{
  5.             "1": "~",
  6.             "template_name": "en-noun"
  7.         }
  8.     ],
  9.     "senses": [{
  10.             "glosses": ["A mammal of the order Proboscidea, having a trunk, and two large ivory tusks jutting from the upper jaw."]
  11.         }, {
  12.             "glosses": ["synonym of elephant paper"],
  13.             "tags": ["paper", "printing"],
  14.             "taxon": [["Euploea", "genus"]],
  15.             "alt_of": ["elephant paper"]
  16.         }, {
  17.             "glosses": ["A dark grayish-blue colour, like that of an elephant (also called elephant gray)."]
  18.         }
  19.     ],
  20.     "synonyms": [{
  21.             "word": "word meaning"
  22.         }, {
  23.             "word": "Elephas maximus",
  24.             "sense": "animal"
  25.         }, {
  26.             "word": "see Appendix:Words used as placeholders to count seconds",
  27.             "sense": "counting term"
  28.         }
  29.     ],
  30.     "hyponyms": [{
  31.             "word": "African forest elephant",
  32.             "sense": "animal"
  33.         }, {
  34.             "word": "Indian elephant",
  35.             "sense": "animal"
  36.         }
  37.     ],
  38.     "derived": [{
  39.             "word": "African bush elephant"
  40.         }, {
  41.             "word": "white elephant"
  42.         }
  43.     ],
  44.     "related": [{
  45.             "word": "chryselephantine"
  46.         }, {
  47.             "word": "Elephas"
  48.         }
  49.     ],
  50.     "categories": ["English basic words", "Elephants", "Paper sizes"],
  51.     "pos": "noun"
  52. }
  53.  


I'm up to "senses" bit at the moment. I'm not able to access the array inside the "taxon" array. If the "taxon" field is not present then it works, otherwise understandably it fails. This is what I have so far. Thank you.


Code: Pascal  [Select][+][-]
  1. procedure Tform1.dojson(jsonstr: string);
  2. var
  3.     keyname: string;
  4.     jdata: TJSONData;
  5.     i, j, x, mainloop: integer;
  6.     tempjobj, dataarrayitem: tjsonobject;
  7.     en_word, language, Value: string;
  8.     sensesarray, headsarray, synonymsarray: TJSONArray;
  9. begin
  10.     jdata := GetJSON(jsonstr);
  11.     tempjobj := Tjsonobject(jdata);
  12.     en_word := tempjobj.FindPath('word').AsString;
  13.     language := tempjobj.FindPath('lang').AsString;
  14.  
  15.     for mainloop := 2 to jdata.Count - 1 do
  16.     begin
  17.         keyname := tjsonobject(jdata).Names[mainloop];
  18.  
  19.         if keyname = 'heads' then
  20.         begin
  21.             headsarray := TJSONArray(jdata.items[mainloop]);
  22.             DataArrayItem := headsarray.Objects[0];
  23.  
  24.             for I := 0 to dataarrayitem.Count - 1 do
  25.             begin
  26.                 keyname := dataarrayitem.Names[i];
  27.                 Value := DataArrayItem[keyname].AsString;
  28.             end;
  29.         end
  30.         else if keyname = 'senses' then
  31.         begin
  32.             sensesarray := TJSONArray(jdata.items[mainloop]);
  33.             for i := 0 to sensesarray.Count - 1 do
  34.             begin
  35.                 DataArrayItem := sensesarray.Objects[i];
  36.                 for j := 0 to dataarrayitem.Count - 1 do
  37.                 begin
  38.                     keyname := dataarrayitem.Names[j];
  39.                     Value := dataarrayitem.FindPath(keyname + '[0]').AsString;
  40.                 end;
  41.             end;
  42.         end;
  43.     end;
  44.  
  45.     jData.Free;
  46. end;
  47.  

d-_-b

  • New Member
  • *
  • Posts: 32
Re: Json unnamed array access
« Reply #1 on: February 14, 2020, 10:39:46 pm »
Where is your taxon if statement?

Quote
I'm not able to access the array inside the "taxon" array

If I look at your data it seems taxon is wrapped as a array in a array:

Code: Text  [Select][+][-]
  1. "taxon": [["Euploea", "genus"]],
  2.  

So you are going to need to specify the zero index array eg:

Code: Pascal  [Select][+][-]
  1.  TJSONArray(jdata.items[mainloop][0]);
  2.  
Code: Pascal  [Select][+][-]
  1. mov     ax,0013h
  2. int     10h
Denthor thanks for the vga programming tutorials | Download all tutorials

GetMem

  • Hero Member
  • *****
  • Posts: 3757
Re: Json unnamed array access
« Reply #2 on: February 15, 2020, 08:01:25 am »
@zariq
1. Go to Lazarus menu Tools-->Example Projects...
2. Check "Include all subdirectories"
3. Type "jsonviewer" in the searchbox
4. Open project jsonviewer.lpi
5. Parse your json
6. Study the code to see how it's done

zariq

  • Full Member
  • ***
  • Posts: 109
Re: Json unnamed array access
« Reply #3 on: February 15, 2020, 10:24:26 pm »
Thank you both for your input. Seems to be working now.

Code: Text  [Select][+][-]
  1. {
  2.     "word": "crow",
  3.     "lang": "English",
  4.     "senses": [{
  5.             "taxon": [["Euploea", "genus"]],
  6.             "tags": ["organism"]
  7.         }
  8.     ]
  9. }
  10.  

Code: Pascal  [Select][+][-]
  1. procedure Tform1.dojson(jsonstr: string);
  2. var
  3.     keyname, value : string;  i, j, x, mainloop: integer;
  4.     jdata: TJSONData;
  5.     dataarrayitem: tjsonobject;
  6.     sensesarray,  taxonarray, unnamedarray: TJSONArray;
  7. begin
  8.     jdata := GetJSON(jsonstr);
  9.     mainloop := 2;
  10.     keyname := tjsonobject(jdata).Names[mainloop];
  11.     sensesarray := TJSONArray(jdata.items[mainloop]);
  12.     i := 0;
  13.         DataArrayItem := sensesarray.Objects[i];
  14.         for j := 0 to dataarrayitem.Count - 1 do
  15.         begin
  16.             keyname := dataarrayitem.Names[j];
  17.             if keyname = 'taxon' then
  18.             begin
  19.                 taxonarray := TJSONArray(dataarrayitem.items[j]);
  20.                 unnamedarray := TJSONArray(taxonarray.items[0]);
  21.                 for x := 0 to unnamedarray.Count - 1 do
  22.                 begin
  23.                     value := unnamedarray.Items[x].AsString;
  24.                 end;
  25.             end;
  26.         end;
  27.     jData.Free;
  28. end;
  29.  

 

TinyPortal © 2005-2018