0

Why is this not working with TJSONObject?

procedure TForm1.Button5Click(Sender: TObject);
var
  js : TJSONObject;
  isoDate1, isoDate2, data : string;
begin
  isoDate1 := '2018-01-02T10:00:00.000Z';
  isoDate2 := '2018-01-02T10:10:00.000Z';

  js := TJSONObject.Create;
  js.AddPair(TJsonPair.Create(isoDate1, 'TEST'));
  js.AddPair(TJsonPair.Create(isoDate2, 'TEST2'));

  outputdebugstring(pchar(js.ToString));

  if js.TryGetValue<string>(isoDate1, data) then begin
    ShowMessage(data);
  end else begin
    ShowMessage('data non trouvé pour ' + isoDate1);
  end;
end;

output : Sortie de débogage: {"2018-01-02T10:00:00.000Z":"TEST","2018-01-02T10:10:00.000Z":"TEST2"} Processus Project1.exe (6232)

Expected Outcome:
The TryGetValue should put a string in data
ShowMessage should give me 'TEST' in a message box.

Outcome:
The ShowMessage give me 'data non trouvé pour 2018-01-02T10:00:00.000Z'.

4
  • 1
    What are you expecting? What is not working? Commented Feb 2, 2018 at 15:06
  • The TryGetValue should put a string in data and ShowMessage should give me 'TEST' in a message box? Commented Feb 2, 2018 at 15:08
  • So what does happen? Commented Feb 2, 2018 at 15:13
  • The ShowMessage give me 'data non trouvé pour 2018-01-02T10:00:00.000Z' which I thought definitely should be 'TEST' Commented Feb 2, 2018 at 15:17

3 Answers 3

2

Problem is the dot in your JSON path that you query with the TryGetValue method call. Dot char path parser (TJSONPathParser) interprets as key separator of pairs, whose value is about to be obtained. So, for example call like this:

if JSONObject.TryGetValue<string>('Foo.Bar', S) then
  DoSomething;

is a query for The value of an object like this:

{"Foo": {"Bar": "The value"}}

not for The value with the key named this way:

{"Foo.Bar": "The value"}

So in your case you were trying to query the value of the 2018-01-02T10:00:00 object's key 000Z, which would require object like this:

{"2018-01-02T10:00:00": {"000Z": "TEST"}}

This JSON path dot notation is hardcoded at this time (without possibility of escaping dot chars in the queried path), so the only way is giving up on that method at this moment, or losing dots from key names.

Sign up to request clarification or add additional context in comments.

3 Comments

But why js.toString give me : {"2018-01-02T10:00:00.000Z":"TEST","2018-01-02T10:10:00.000Z":"TEST2"}, which is a validated JSON, also the dot is a part of ISO, I am not sure to remove it
Oddly, js.GetValue(isoDate1).toString worked but not js.GetValue<string>(isoDate1)
You have a valid JSON. The probem is that TryGetValue supports dot notation of paths and thinks that you're querying value of an object that I've shown in this post.
2

The problem is the dot in your key. It's used as a seperator. Delphi interprets '2018-01-02T10:00:00' as an object which has '000Z' as a property.

I would sugguest to get the value like this:

var
...
  LJsonValue: TJSONValue;
begin
...

  LJsonValue := js.GetValue(isoDate1);
  if Assigned(LJsonValue) then
    ShowMessage(LJsonValue.Value);

...
end;

Comments

0

You can use JSON path syntax in GetValue...

... MySt := JSONObject.GetValue<string>('["Foo"]["Bar"]') ...

Comments

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.