I'm using Json Path library to parse JSON. I've following json which has key with space:
{
"attributes": {
"First Name": "Jim",
"Last Name": "Rohn"
}
}
In order to get value of First Name
, I wrote code like (where json
is object which holds above json) -
String firstName = JsonPath.from(json).getString("attributes.First Name");
But it results into following error -
java.lang.IllegalArgumentException: Invalid JSON expression:
Script1.groovy: 1: expecting EOF, found 'Attributes' @ line 1, column 67.
.First Name
Could you please suggest how to get values of key having spaces using json-path
library?
Try using bracket notation for First Name
as follows:
String firstName = JsonPath.from(json).getString("attributes.['First Name']");
UPDATE
Sorry for mixing different JsonPath
libraries up.
If you are using com.jayway.jsonpath
, try following way for escaping:
DocumentContext jsonContext = JsonPath.parse(json);
String firstName = jsonContext.read("$.attributes.['First Name']");
But if you are using ***.restassured.json-path
, please use this one:
String firstName = JsonPath.from(json).getString("attributes.'First Name'");