www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - std.json / nested key/value access?

reply =?iso-8859-1?Q?Robert_M._M=FCnch?= <robert.muench saphirion.com> writes:
I have:

  JSONValue j = parseJSON(json_string).object;

  writeln(j); 							// works
  writeln(j["a"]); 					// works
  writeln(j["a"].object["b"]); 	// bombs

Runtime error: std.json.JSONException std/json.d(276): JSONValue is not 
an object

How can I access a nested JSON key/value using something that comes 
close to a path notation?

-- 
Robert M. Münch
http://www.saphirion.com
smarter | better | faster
Nov 15 2019
parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 11/15/19 12:05 PM, Robert M. Münch wrote:
 JSONValue j = parseJSON(json_string).object;
 
   writeln(j);                             // works
   writeln(j["a"]);                     // works
   writeln(j["a"].object["b"]);     // bombs
auto json_string = `{"a": {"b": 1}}`; Results with your code as: {"a":{"b":1}} {"b":1} 1 Maybe your "a" value is not an object? -Steve
Nov 15 2019
parent =?iso-8859-1?Q?Robert_M._M=FCnch?= <robert.muench saphirion.com> writes:
On 2019-11-15 17:23:38 +0000, Steven Schveighoffer said:

 On 11/15/19 12:05 PM, Robert M. Münch wrote:
 JSONValue j = parseJSON(json_string).object;
 
  writeln(j);                             // works
  writeln(j["a"]);                     // works
  writeln(j["a"].object["b"]);     // bombs
Maybe your "a" value is not an object?
Thanks, it's an array... why ever... I need to use: writeln(j["a"][0]["b"]); Wasn't that obvious, because it's a big JSON structure. -- Robert M. Münch http://www.saphirion.com smarter | better | faster
Nov 15 2019