How do I access triple nested values in JSON using Java? - java

I am trying to access the "text" values in the below JSON and create an ArrayList with those values. How would I approach that? I am trying to use com.fasterxml.jackson.
{
"searchApiFormatVersion": "1.0",
"searchName": "SalaryAccessRole",
"description": "",
"totalRowCount": "2",
"returnedRowCount": "2",
"startingReturnedRowNumber": "1",
"basetype": "Person",
"columnCount": "1",
"columnHeaders": [
{
"text": "EMPLID",
"dataType": "string"
}
],
"resultSet": {
"rows": [
{
"values": [
{
"text": "2270127",
"dataType": "string",
"columnHeader": "EMPLID"
}
]
},
{
"values": [
{
"text": "1050518",
"dataType": "string",
"columnHeader": "EMPLID"
}
]
}
]
}
}
I've used this to success for the searchName but am trying to get the text under resultSet->rows->values->text.
JsonNode salaryExcludePersonNode = new ObjectMapper().readTree(sourceJson);
String person = salaryExcludePersonNode.get("searchName").textValue();//this works
String person2 = salaryExcludePersonNode.get("resultSet")
.get("rows")
.get("values")
.get("text")
.textValue());//this says that com.fasterxml.jackson.databind.JsonNode.get(String) is null

I know you tagged this question as a Jackson question, but I think JSONPath may be a good fit here.
The JSONPath library is available as a Maven dependency:
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.7.0</version>
</dependency>
For the JSON in your question, all of the "text" values can be selected using a single JSONPath selector:
$.resultSet.rows..text
$ - start at the root of the JSON.
.resultSet.rows - navigate to the rows array.
..text - recursively find all text entries.
The results are automatically added to a Java List:
String jsonAsString = "{...}";
List<String> textList = JsonPath.read(jsonAsString, "$.resultSet.rows..text");
You can also read from a java.io.File directly:
File jsonFile = "/your/path/to/file.json";
List<String> textList = JsonPath.read(jsonFile, "$.resultSet.rows..text");
The list will be populated with 2 entries:
2270127
1050518
References:
JSONPath syntax overview
JavaDocs

Related

List all the paths which contains an attribute

I have following JSON, in which I want to list all parent node paths where path attribute is present.
{
"ServiceId": {
"type": "string",
"admin": "false"
},
"NormalizedEvents": {
"type": "list",
"path": "/data/../../nEvents"
},
"Events": {
"type" : "list",
"path": "/data/../../Events"
}
}
In this case, I need output like, $.NormalizedEvents and $.Events.
use JSONPath options to output path instead of value.
JSONPath
$.[*][?(#.path)]
Output
[
"$['NormalizedEvents']",
"$['Events']"
]
JsonPath expressions can use the dot–notation
$.NormalizedEvents
or
the bracket–notation
$['NormalizedEvents']
Online Tool : Jayway JsonPath Evaluator

Get json object in Array based on key and value in Java

I have a Json body like the example below. I need to extract the value from a key that has another key with a specific value in an array. I am passing in a JsonNode with everything in the detail component of the message, I can easily extract from each level, however, I'm struggling with the array.
In this case, I need to extract the value of "value" (Police/Fire/Accident Report) from the object in the array which has a key/value pair of "name":"documentTitle". I understand this is a JSONArray, but I can't find a good example that shows me how to extract the values for an object in the array that contains a certain key/value pair, I don't think I can rely on getting the object in position [2] in the array as the same objects may not always be present in the additionalMetadata array.
Sample Json:
"sourceVersion": "1.0",
"eventId": "8d74b892-810a-47c3-882b-6e641fd509eb",
"clientRequestId": "b84f3a7b-03cc-4848-a1e8-3519106c6fcb",
"detail": {
"stack": "corona",
"visibilityIndicator": null,
"documentUid": "b84f3a7b-03cc-4848-a1e8-3519106c6fcb",
"additionalMetadata": [
{
"name": "lastModifiedDate",
"value": "2021-05-21T04:53:53Z"
},
{
"name": "documentName",
"value": "Police/Fire Report, 23850413, 2021-05-20 14:51:23"
},
{
"name": "documentTitle",
"value": "Police/Fire/Accident Report"
},
{
"name": "documentAuthor",
"value": "System Generated"
},
{
"name": "lastModifiedBy",
"value": "System Updated"
},
{
"name": "createdBy",
"value": "System Generated"
},
{
"name": "documentDescription",
"value": "Police/Fire Report received"
},
{
"name": "organizationCode",
"value": "Claims"
}
]
}
}```
Loop through the json array and extract the json object with name documentTitile. From that json object you can get the value
Well, either the JSON framework you're using supports this out of the box (check the documentation) or you could convert it manually to a map:
List<AdditionalMetadataEntry> additionalMetadata;
[...]
Map<String, String> additionalMetadataMap = additionalMetadata.stream().collect(Collectors.toMap(AdditionalMetadataEntry::getName, AdditionalMetadataEntry::getValue));
I was able to figure it out. I created a new node off the existing notificationBody JsonNode, then parsed through the metadata key/value pairs:
String docTitle = "";
JsonNode additionalMetadata = notificationBody.get("detail").get("additionalMetadata");
for (JsonNode node: additionalMetadata) {
String name = node.get("name").asText();
String value = node.get("value").asText();
if(name.equals("documentTitle")){
docTitle = value;
}
}

How can I get data from the api graphQL?

I make a request on api graphQL and have the responce of body:
{
"dataRequests": [
{
"status": "success",
"title": "token",
"values": {
"limit": 1,
"offset": 0,
"count": 1,
"total": 1,
"elements": [
{
"type": "DOMAIN",
"permission": "default",
"properties": [
{
"name": "property:id",
"value": 390
},
{
"name": "setting:crawler:token",
"value": "token(here's real token)"
}
],
"filters": []
}
]
}
}
]
}
I want to get value of the field "value" with token. But i have a problem to desirialize it.
My code can get the list of maps of 'dataRequests' field(using RestAssured):
GraphQLSteps graphQLSteps = new GraphQLSteps();
Response response = graphQLSteps.postProjectToken(id);
List<Map<String, String>> dataRequest = response.jsonPath().getList("dataRequests");
but, if i try to get the list of maps of 'value' field:
List<Map<String, String>> dataRequest = response.jsonPath().getList("value");
i get the "null" value. I think it's because i have to wrap it through the whole tree: values - elements - properties and only then value. But it's very complicated and i tried to get it in this way, but obtain only the same result "null".
I noticed that if i print an entrySet of an existing map from list:
List<Map<String, String>> dataRequest = response.jsonPath().getList("dataRequests");
for (Map<String, String> map : dataRequest) {
System.out.println(map.entrySet());
}
I get the result:
[status=success, title=token, values={limit=1, offset=0, count=1, total=1, elements=[{type=DOMAIN, permission=default, properties=[{name=property:id, value=390}, {name=setting:crawler:token, value=someToken}], filters=[]}]}]
And can see value with token there.
Can you prompt me, how can i get the "value" with token either from this List<Map<String, String>> or by other way with deserialitation api?
Your json is kind of complicated with many nested level. I would recommend to use lib jsonpath, instead of using jsonpath provided by rest-assured.
Example:
List<String> values = JsonPath.read(response.toString(), "$..value");
System.out.println(values);
//[390,"token(here's real token)"]
add jsonpath to pom.xml
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.6.0</version>
</dependency>

JSONPath expression to concate two or more JSONPath values

I have below JSON data. I have JPath to read FirstName and LastName but to read both values using a single JSONPath expression is not getting. Can someone help me here to read the values for both the elements?
I want to read like Name=Rob|Long using JSONPath expression. I tried a few combinations but not working
{
"attributes": {
"type": "Contacts",
"url": "/services/data/v36.0/sobjects/Contact/abc123"
},
"Id": "abc123",
"Salutation": "Mr.",
"FirstName": "Rob",
"LastName": "Long"
}
Thanks in advance
Try with JayWay JsonPath:
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.4.0</version>
</dependency>
This should do the trick: concat($.FirstName,"|",$.LastName)
However, it doesn't seem to work with other JsonPath implementations.
using Bazaarvoice/ jolt this can be easily achieved
--------JSON Input
{
"attributes": {
"type": "Contacts",
"url": "/services/data/v36.0/sobjects/Contact/abc123"
},
"Id": "abc123",
"Salutation": "Mr.",
"FirstName": "Rob",
"LastName": "Long"
}
--------------spec
[
{
"operation": "modify-default-beta",
"spec": {
// String join the values in the array x, with a comma and a space
"fullName": "=concat(#(1,FirstName),'|',#(1,LastName))"
}
}
]
--------------------output
{
"attributes" : {
"type" : "Contacts",
"url" : "/services/data/v36.0/sobjects/Contact/abc123"
},
"Id" : "abc123",
"Salutation" : "Mr.",
"FirstName" : "Rob",
"LastName" : "Long",
"fullName" : "Rob|Long"
}
try this out at - https://jolt-demo.appspot.com/

How to select fields in different levels of a jsonfile with jsonPath?

I want to convert jsonobjcts into csv files. Wy (working) attempt so far is to load the json file as a JSONObject (from the googlecode.josn-simple library), then converting them with jsonPath into a string array which is then used to build the csv rows. However I am facing a problem with jsonPath. From the given example json...
{
"issues": [
{
"key": "abc",
"fields": {
"issuetype": {
"name": "Bug",
"id": "1",
"subtask": false
},
"priority": {
"name": "Major",
"id": "3"
},
"created": "2020-5-11",
"status": {
"name": "OPEN"
}
}
},
{
"key": "def",
"fields": {
"issuetype": {
"name": "Info",
"id": "5",
"subtask": false
},
"priority": {
"name": "Minor",
"id": "2"
},
"created": "2020-5-8",
"status": {
"name": "DONE"
}
}
}
]}
I want to select the following:
[
"abc",
"Bug",
"Major",
"2020-5-11",
"OPEN",
"def",
"Info",
"Minor",
"2020-5-8",
"DONE"
]
The csv should look like that:
abc,Bug,Major,2020-5-11,OPEN
def,Info,Minor,2020-5-8,DONE
I tried $.issues.[*].[key,fields] and I get
"abc",
{
"issuetype": {
"name": "Bug",
"id": "1",
"subtask": false
},
"priority": {
"name": "Major",
"id": "3"
},
"created": "2020-5-11",
"status": {
"name": "OPEN"
}
},
"def",
{
"issuetype": {
"name": "Info",
"id": "5",
"subtask": false
},
"priority": {
"name": "Minor",
"id": "2"
},
"created": "2020-5-8",
"status": {
"name": "DONE"
}
}
]
But when I want to select e.g. only "created" $.issues.[*].[key,fields.[created]
[
"2020-5-11",
"2020-5-8"
]
This is the result.
But I just do not get how to select "key" and e.g. "name" in the field issuetype.
How do I do that with jsonPath or is there a better way to filter a jsonfile and then convert it into a csv?
I recommend what I believe is a better way - which is to create a set of Java classes which represent the structure of your JSON data. When you read the JSON into these classes, you can manipulate the data using standard Java.
I also recommend a different JSON parser - in this case Jackson, but there are others. Why? Mainly, familiarity - see later on for more notes on that.
Starting with the end result: Assuming I have a class called Container which contains all the issues listed in the JSON file, I can then populate it with the following:
//import com.fasterxml.jackson.databind.ObjectMapper;
String jsonString = "{...}" // your JSON data as a string, for this demo.
ObjectMapper objectMapper = new ObjectMapper();
Container container = objectMapper.readValue(jsonString, Container.class);
Now I can print out all the issues in the CSV format you want as follows:
container.getIssues().forEach((issue) -> {
printCsvRow(issue);
});
Here, the printCsvRow() method looks like this:
private void printCsvRow(Issue issue) {
String key = issue.getKey();
Fields fields = issue.getFields();
String type = fields.getIssuetype().getName();
String priority = fields.getPriority().getName();
String created = fields.getCreated();
String status = fields.getStatus().getName();
System.out.println(String.join(",", key, type, priority, created, status));
}
In reality, I would use a CSV library to ensure records are formatted correctly - the above is just for illustration, to show how the JSON data can be accessed.
The following is printed:
abc,Bug,Major,2020-5-11,OPEN
def,Info,Minor,2020-5-8,DONE
And to filter only OPEN records, I can do something like this:
container.getIssues()
.stream()
.filter(issue -> issue.getFields().getStatus().getName().equals("OPEN"))
.forEach((issue) -> {
printCsvRow(issue);
});
The following is printed:
abc,Bug,Major,2020-5-11,OPEN
To enable Jackson, I use Maven with the following dependency:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.10.3</version>
</dependency>
In case you don't use Maven, this gives me 3 JARs: jackson-databind, jackson-annotations, and jackson-core.
To create the nested Java classes I need (to mirror the structure of the JSON), I use a tool which generates them for me using your sample JSON.
In my case, I used this tool, but there are others.
I chose "Container" as the name of the root Java class; a source type of JSON; and selected Jackson 2.x annotations. I also requested getters and setters.
I added the generated classes (Fields, Issue, Issuetype, Priority, Status, and Container) to my project.
WARNING: The completeness of these Java classes is only as good as the sample JSON. But you can, of course, enhance these classes to more accurately reflect the actual JSON you need to handle.
The Jackson ObjectMapper takes care of loading the JSON into the class structure.
I chose to use Jackson instead of JsonPath, simply because of familiarity. JsonPath appears to have very similar object mapping capabilities - but I have never used those features of JsonPath.
Final note: You can use xpath style predicates in JsonPath to access individual data items and groups of items - as you describe in your question. But (in my experience) it is almost always worth the extra effort to create Java classes, if you want to process all your data in more flexible ways - especially if that involves transforming the JSON input into different output structures.

Categories