Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you reference the root key using JSON Path?

I have data like so:

{"key": {"name":"hi", "size":10}}

"key" is a dynamic value. It isn't fixed. I can access name and size using this JSON Path:

*.name
*.size

How can I get the value of "key" itself with JSON Path? * gives me the entire row of data, and both $ and @ give me "no element found" in my parser.

I'm trying to do this in Pentaho with a JSON Input step.

like image 320
gdm Avatar asked Oct 02 '12 15:10

gdm


People also ask

What is a root element in JSON?

According to the modified Backus-Naur-Form on the right side pane of http://json.org/ the root element of a JSON data structure can be any of these seven types/values: Object Array String Number true false null.

What is JSON path expression?

JsonPath expressions always refer to a JSON structure in the same way as XPath expression are used in combination with an XML document. The "root member object" in JsonPath is always referred to as $ regardless if it is an object or array. JsonPath expressions can use the dot–notation. $.store.book[0].title.

How does JSON path work?

You use a JSONPath expression to traverse the path to an element in the JSON structure. You start at the root node or element, represented by $, and reach the required element in the JSON structure to extract data from it. You can use either the dot-notation or the bracket-notation to form the expressions.


2 Answers

$.* will give you all elements of parent object, in your example you will get something like

[
   {
      "name":"hi",
      "size":10
   }
]

according to: http://jsonpath.curiousconcept.com/

like image 67
zpon Avatar answered Sep 29 '22 23:09

zpon


This is not possible to do with the Pentaho JSON Input step, as this uses JSONPath as you say. You need to do it another way. For example with the Modified Java Script Value:

var obj = JSON.parse(json);
var keys = Object.keys(obj);
like image 31
bolav Avatar answered Sep 30 '22 01:09

bolav