Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select attribute name with JsonPath

Tags:

jsonpath

I'm consuming a JSON feed that returns a structure with an array of irregular objects - I can't predict what's in the array, and whatever is there will change over time. A simple example is

{
 "content":
   [
     {
       "person"  : "john"
     },
     {
       "car"  : "Audi"
     }
   ]
 }

The JsonPath query

$.content.[0]

returns the first object in that array. But, I need a jsonpath query that returns the literal NAME of the first property of that object, in this case I need the query to return "person". Is this possible?

like image 778
Shukri Adams Avatar asked Aug 29 '14 15:08

Shukri Adams


People also ask

How do I specify JsonPath?

A JsonPath expression begins with the dollar sign ( $ ) character, which refers to the root element of a query. The dollar sign is followed by a sequence of child elements, which are separated via dot (code) notation or via the square brackets (code).

What is JsonPath query?

JSONPath is a query language for JSON, similar to XPath for XML. It allows you to select and extract data from a JSON document. You use a JSONPath expression to traverse the path to an element in the JSON structure.

What is JsonPath in Rest assured?

JsonPath is an alternative to using XPath for easily getting values from a Object document. It follows the Groovy dot notation syntax when getting an object from the document. You can regard it as an alternative to XPath for XML.

What is a JPath?

JPath is a language for querying and manipulating JSON elements. You can use JPath to compute values, such as strings, numbers, and boolean values, from JSON elements. Basic selection. Select elements by using a forward slash (/). Select array items by using square brackets ( [ ] ).


1 Answers

$.content.[0].*~

Tested on JSON path

like image 132
Bhaskar13 Avatar answered Sep 23 '22 10:09

Bhaskar13