How to parse a JSON string in Inno Setup?

You can use JsonParser library instead. It can parse JSON strings.

It’s not as easy to use as JSONConfig.dll – but that’s the reason why it is more flexible. Also it’s a native Pascal Script code. So, it not only saves you from a temporary .json file, but also from a temporary .dll.

The code can be like:

[Code]

#include "JsonParser.pas"

function GetJsonRoot(Output: TJsonParserOutput): TJsonObject;
begin
  Result := Output.Objects[0];
end;

function FindJsonValue(
  Output: TJsonParserOutput; Parent: TJsonObject; Key: TJsonString;
  var Value: TJsonValue): Boolean;
var
  I: Integer;
begin
  for I := 0 to Length(Parent) - 1 do
  begin
    if Parent[I].Key = Key then
    begin
      Value := Parent[I].Value;
      Result := True;
      Exit;
    end;
  end;

  Result := False;
end;

function FindJsonObject(
  Output: TJsonParserOutput; Parent: TJsonObject; Key: TJsonString;
  var Object: TJsonObject): Boolean;
var
  JsonValue: TJsonValue;
begin
  Result :=
    FindJsonValue(Output, Parent, Key, JsonValue) and
    (JsonValue.Kind = JVKObject);

  if Result then
  begin
    Object := Output.Objects[JsonValue.Index];
  end;
end;

function FindJsonNumber(
  Output: TJsonParserOutput; Parent: TJsonObject; Key: TJsonString;
  var Number: TJsonNumber): Boolean;
var
  JsonValue: TJsonValue;
begin
  Result :=
    FindJsonValue(Output, Parent, Key, JsonValue) and
    (JsonValue.Kind = JVKNumber);

  if Result then
  begin
    Number := Output.Numbers[JsonValue.Index];
  end;
end;

function FindJsonString(
  Output: TJsonParserOutput; Parent: TJsonObject; Key: TJsonString;
  var Str: TJsonString): Boolean;
var
  JsonValue: TJsonValue;
begin
  Result :=
    FindJsonValue(Output, Parent, Key, JsonValue) and
    (JsonValue.Kind = JVKString);
  if Result then
  begin
    Str := Output.Strings[JsonValue.Index];
  end;
end;

function ParseJsonAndLogErrors(
  var JsonParser: TJsonParser; const Source: WideString): Boolean;
var
  I: Integer;
begin
  ParseJson(JsonParser, Source);

  Result := (Length(JsonParser.Output.Errors) = 0);
  if not Result then
  begin
    Log('Error parsing JSON');
    for I := 0 to Length(JsonParser.Output.Errors) - 1 do
    begin
      Log(JsonParser.Output.Errors[I]);
    end;
  end;
end;

procedure ParseJsonString;
var
  Json: string;
  JsonParser: TJsonParser;
  I: Integer;
  JsonRoot, InfoObject: TJsonObject;
  UserNumber: TJsonNumber; { = Double }
  UserString: TJsonString; { = WideString = string }
begin
  Json := '{"Info":{"User":2,"String":"abc"}}';

  if ParseJsonAndLogErrors(JsonParser, Json) then
  begin
    JsonRoot := GetJsonRoot(JsonParser.Output);
    if FindJsonObject(JsonParser.Output, JsonRoot, 'Info', InfoObject) and
       FindJsonNumber(JsonParser.Output, InfoObject, 'User', UserNumber) and
       FindJsonString(JsonParser.Output, InfoObject, 'String', UserString) then
    begin
      Log(Format('Info:User:%d', [Round(UserNumber)]));
      Log(Format('Info:String:%s', [UserString]));
    end;
  end;

  ClearJsonParser(JsonParser);
end;

Another option is to fork the Inno JSON Config library and add support for parsing strings.

Leave a Comment