4

I'm trying to create JSON that looks like this:

{ "guestlist": ["alice","bob","charlie"] }

Typical examples I see for populating a JSON array look like this:

var 
  jsobj: TJsonObject;      
  jso : TJsonObject;      
  jsa : TJsonArray;
  jsp : TJsonPair;

begin
    jsObj := TJsonObject.Create();
    jsa := TJsonArray.Create();
    jsp := TJSONPair.Create('guestlist', jsa);
    jsObj.AddPair(jsp);

    jso := TJsonObject.Create();
    jso.AddPair(TJSONPair.Create('person', 'alice'));
    jsa.AddElement(jso);

    jso := TJsonObject.Create();
    jso.AddPair(TJSONPair.Create('person', 'bob'));
    jsa.AddElement(jso);

    jso := TJsonObject.Create();
    jso.AddPair(TJSONPair.Create('person', 'charlie'));
    jsa.AddElement(jso);
end;

But that would result in something like this:

{ "guestlist": [{"person":"alice"},{"person":"bob"},{"person":"charlie"}] }

How can I add a single value to the array instead of a pair? I see nothing in the documentation for TJsonObject on how to do this,

2
  • I think you'd do well to go to json.org and read the very simple JSON spec. Learn about objects, arrays and values. Most JSON libraries use those same terms. So you would appear to have a misunderstanding since you want an object to hold an array. Once the terms object, array and value are well understood, all of these libraries will become obvious. Commented Jan 30, 2016 at 8:24
  • @DavidHeffernan read and updated the question with better terminology Commented Feb 1, 2016 at 15:57

2 Answers 2

10

This is actually a lot simpler than you're making it out to be. A TJSONArray can happily contain any TJSONValue as elements so the solution is really quite straightforward.

program Project1;
{$APPTYPE CONSOLE}

uses
  JSON;

var
  LJObj : TJSONObject;
  LGuestList : TJSONArray;
begin

  LGuestlist := TJSONArray.Create();
  LGuestList.Add('alice');
  LGuestList.Add('bob');
  LGuestList.Add('charlie');

  LJObj := TJSONObject.Create;
  LJObj.AddPair(TJSONPair.Create('guestlist', LGuestList));

  WriteLn(LJObj.ToString);
  ReadLn;
end.

Produces output :

{"guestlist":["alice","bob","charlie"]}
Sign up to request clarification or add additional context in comments.

2 Comments

You can also do LJObj.AddPair('guestlist', LGuestList); so you don't have to call TJSONPair.Create() explicitly.
@RemyLebeau Good point - was just being explicit but that's nice and clean too.
4

Just in case you'd be interested in looking at an alternative: I created jsonDoc, primarily because I like COM interfaces and OleVariants, and dislike long lists of overloads. Then the above code could like like this:

JSON(['guestlist',
        VarArrayOf([JSON(['person','alice']),
                    JSON(['person','bob']),
                    JSON(['person','charlie'])
                    ])
    ])

2 Comments

Thanks, I'll consider that.
Nice! I will try this one next time I'm doing JSON.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.