Jump to content
Registration disabled at the moment Read more... ×
Sign in to follow this  
omnibrain

Trouble getting JSON FindValue (JSONPath) to work

Recommended Posts

I'm trying to use JSONPath via TJSONObject.FindValue in my program. I'm using Delphi 11.

 

I broke it down to the easiest example and I still can't get it to work:

{
  "name": "Chris",
  "value": 10000
}

Path: 

$.name

My Code after stripping away everything else:

  var myjson:=TJSONObject.ParseJSONValue('{"name": "Chris","value": 10000}');
  var myval:=myjson.FindValue('$.name');
  ergebnis.Text:=myval.Value;

The JSON get's parsed, but the FindValue returns 'nul'.

Am I doing something completely wrong?

Share this post


Link to post

Take at the look at the documentation again, it has some examples. Get Rid of the $ and the dot.

program Project1;

uses System.SysUtils, System.JSON;

begin
  var myjson:=TJSONObject.ParseJSONValue('{"name": "Chris","value": 10000}');
  var myval:=myjson.FindValue('name');
  Assert(myVal.Value() = 'Chris');
end.

Or here, with arrays:

 

program Project1;

uses System.SysUtils, System.JSON;

const
  json =
    '{'+
    '	"name": "Chris",'+
    '	"pets": ['+
    '		{'+
    '			"name": "Rufus",'+
    '			"type": "dog",'+
    '			"age": 10'+
    '		},'+
    '		{'+
    '			"name": "Wraabargl",'+
    '			"type": "Dinosaur",'+
    '			"age": 113'+
    '		}'+
    '	]'+
    '}';
begin
  var myjson:=TJSONObject.ParseJSONValue(json);
  var myval:=myjson.FindValue('pets[1].type');
  Assert(myVal.Value() = 'Dinosaur');
  readln;
end.

 

  • Thanks 1

Share this post


Link to post

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×