Home > Back-end >  parse json and extract all id value with ULKJson library7
parse json and extract all id value with ULKJson library7

Time:09-13

I want to parse the JSON and extract all ID values and assign, show them in a combo box. I am using ULKJson library and Delphi 7. Here is my JSON

{
"ID": null,
"ResCode": 100,
"ResMessage": "هیچ آیتم جدیدی در پایگاه داده ذخیره نشد",
"ResPos": null,
"ResData": {
    "data": [
        {
            "data": {
                "id": "2994LUZUWL",
                "type": "SHEBAD_ID"
            },
            "sendDate": "2021-11-02T20:00:16.827"
        },
        {
            "data": {
                "id": "2992CQVAE1",
                "type": "SHEBAD_ID"
            },
            "sendDate": "2021-11-02T18:43:12.857"
        },
        {
            "data": {
                "id": "Z2005KUU6ZB",
                "type": "SHEBAD_ID"
            },
            "sendDate": "2021-11-02T18:51:36.107"
        },
        {
            "data": {
                "id": "Z29914MM2WL",
                "type": "SHEBAD_ID"
            },
            "sendDate": "2021-11-02T19:21:08.607"
        }
    ],
    "message": "",
    "succeeded": true
 }
}

How can I access to ID values inside this JSON?

CodePudding user response:

Delphi 6 user here, but same thing really..

You need to have uLKJson installed, which I assume you have.

You need to parse each element as one of the uLKJSON types: jsBase, jsNumber, jsString, jsBoolean, jsNull, jsList, jsObject

You have

{    <<< root = object
"ResData": {    <<< ResData = object
    "data": [   <<< data = list
        {       <<< element[0] = object
            "data": {    <<< data = object
                "id": "2994LUZUWL",   <<< id = string. This is what you want.
                ...

In jq, this would be selection string: ".ResData.data[].data.id".

The program to parse the data could look something like this:

program testparse;
{$APPTYPE CONSOLE}
uses
  SysUtils,     // for writing on console
  ulkjson;      // to parse JSON

var
  vJsonObj: TlkJsonObject;
  jo : TlkJSONstreamed;
  listofelements: TlkJSONlist;
  i : integer;

//        {
//            "data": {
//                "id": "2994LUZUWL",
//                "type": "SHEBAD_ID"
//            },
//            "sendDate": "2021-11-02T20:00:16.827"
//        },


  // function for simplicity
  //
  function get_key( j : TlkJSONobject) : string;
    begin
      try
         result := j.Field['data'].Field['id'].Value;
      except
        result := 'N/A';   // if the id key is not there..
      end;
    end;

begin
  jo := TlkJSONstreamed.Create;
  try
  vJsonObj := jo.LoadFromFile('jsontext.txt') as TlkJsonObject;
  listofelements := vJsonObj.Field['ResData'].Field['data'] as TlkJSONlist;
  for i := 0 to listofelements.Count-1 do
    begin
        writeln('Key no '   inttostr(i)   '='  
                get_key(listofelements.Child[i] as TlkJSONobject)
                );
    end;

  finally

    jo.free;
  end;

end.

Result:

Key no 0=2994LUZUWL
Key no 1=2992CQVAE1
Key no 2=Z2005KUU6ZB
Key no 3=Z29914MM2WL

Alternative library

Alternatively JSONTools, originally from Lazarus can be used. I have modified and uploaded a version for Delphi 6/7.

Using this unit:

    writeln('-- doing it with JSONTools');
    N := TJsonNode.Create;
    n.LoadFromFile('jsontext.txt');    // will automatically do a parse
    n:= n.Find('/ResData/data');       // get the array
    for i := 0 to n.Count-1 do
      begin
           writeln(
             'Key no '   inttostr(i)   '='  
             n.Child(i).Find('data/id').AsString);
      end;
    n.Free;

Adding to a combobox

As the question was for how to add to a combobox: this is simple. First clear the combobox, then for each item do an "additem".

  • Related