Jump to content
JIMSMITH

Reading a section of JSON

Recommended Posts

I am tring to read a section of json containing an array below for the section titled MAILCONFIGS.  There is an array that contains two pairs.   I need to read the two JSON PAIRS and breakout the datapair within.  Any help will be appreciated, and Thanks in advance.

{ "SPWORKDATA": { "DB_PARAMS": { "Pooled":"False", "DriverID":"Oracle", "Database":"iranums", "User_name":"rms", "Password":"trojan", "Server":"lilly.com", "Port":"5432", "Monitorby":"", "MetaDefSchema":"False" }, "MAILCONFIGS": [ {"mail1@gmail.com": { "client_id": "ClientID_Data1", "apptoken": "apptoken data1", "temptoken": "temporary token1" } }, {"mail2@gmail.com": { "client_id": "client_data2", "apptoken": "apptoken data2", "temptoken": "temporary token2" } } ], "MEDIASERVER": { "Server": "192.162.2.185" }, "MSGSERVER": { "Server": "127.0.0.1", "Port":"55555" } } }

Share this post


Link to post

What version of Delphi are you using? Delphi has had a built-in JSON framework for a long time, have you tried it yet? For example:

uses
  ..., System.JSON;

var JSON = '...';
var obj = TJSONObject.ParseJSONValue(JSON) as TJSONObject;
try
  for var element in (obj.GetValue('MAILCONFIGS') as TJSONArray) do
  begin
    for var pair in (element as TJSONObject) do
    begin
      var email := pair.JsonString.Value;
      var config := pair.JsonValue as TJSONObject;
      var clientId := config.GetValue('client_id').Value;
      var appToken := config.GetValue('apptoken').Value;
      var tempToken := config.GetValue('temptoken').Value;
      ...
    end;
  end;
finally
  obj.Free;
end;

If your version doesn't have it, there are plenty of 3rd party JSON libraries available.

Edited by Remy Lebeau

Share this post


Link to post
9 hours ago, Remy Lebeau said:

What version of Delphi are you using? Delphi has had a built-in JSON framework for a long time, have you tried it yet? For example:


var JSON = '...';
var obj = TJSONObject.ParseJSONValue(JSON) as TJSONObject;
try
  for var element in (obj.GetValue('MAILCONFIGS') as TJSONArray) do
  begin
    for var pair in (element as TJSONObject) do
    begin
      var email := pair.JsonString.Value;
      var config := pair.JsonValue as TJSONObject;
      var clientId := config.GetValue('client_id').Value;
      var appToken := config.GetValue('apptoken').Value;
      var tempToken := config.GetValue('temptoken').Value;
      ...
    end;
  end;
finally
  obj.Free;
end;

If your version doesn't have it, there are plenty of 3rd party JSON libraries available.

I am using Delphi 10.1 Update 2. 

Share this post


Link to post
13 hours ago, Remy Lebeau said:

What version of Delphi are you using? Delphi has had a built-in JSON framework for a long time, have you tried it yet? For example:


uses
  ..., System.JSON;

var JSON = '...';
var obj = TJSONObject.ParseJSONValue(JSON) as TJSONObject;
try
  for var element in (obj.GetValue('MAILCONFIGS') as TJSONArray) do
  begin
    for var pair in (element as TJSONObject) do
    begin
      var email := pair.JsonString.Value;
      var config := pair.JsonValue as TJSONObject;
      var clientId := config.GetValue('client_id').Value;
      var appToken := config.GetValue('apptoken').Value;
      var tempToken := config.GetValue('temptoken').Value;
      ...
    end;
  end;
finally
  obj.Free;
end;

If your version doesn't have it, there are plenty of 3rd party JSON libraries available.

Remy,

 

I am using Delphi 10.1 Update 2.  I was able to figure it out from the code that you provided plus prior work.  So I am good at this point and thanks for your help.

  • Like 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×