I am trying to deserialize a JSON file basis the environment selected by the user . My requirement is to select data fro JSON basis the Environment selected & convert them into Java Object for running my test script . Below is the research I could comup with so far .
JSON file //Select the JSON array basis the environment(QA,STAGE)
{
"QA":{
"customerName":"Customer QA",
"customerAddr":"UK",
"currency": "GBP"
},
"STAGE" : {
"customerName":"Customer STAGE",
"customerAddr":"FRANCE",
"currency": "EUR"
}
}
Java Class //Class to transform JSON to Object . Call the constructor of the class basis the environment (QA,STAGE)
public class P {
String customerName;
String customerAddr;
String currency;
P(String env,String customerName,String customerAddr,String currency)
{
if(env.equals("qa"){}
if(env.equals("stage"){}
}
Deserialization Class // Need to decide how to call the above class constructor basis the environment for deserializing
public static <T> T deserializeJson(String fileName, Class<T> T) throws IOException {
InputStream is = JacksonUtils.class.getClassLoader().getResourceAsStream(fileName);
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.readValue(is, T);
}```
There seem to be multiple possible solutions for this. However, without knowing what you want to with it afterwards, its not really possible to tell which is best.
Personally I would make an additional class which holds the environments, as there should be a finite number of environments (currently we know about 2).
#Data
class Environment {
#JsonProperty(value = "QA")
UserData qa;
#JsonProperty(value = "STAGE")
UserData stage;
}
And UserData is your P:
#Data
class UserData {
String customerName;
String customerAddr;
String currency;
}
Then you can parse it like this:
String json = "{\n" + "\"QA\":{\n" + " \"customerName\":\"Customer QA\",\n"
+ " \"customerAddr\":\"UK\",\n" + " \"currency\": \"GBP\"\n" + " },\n"
+ "\"STAGE\" : {\n" + " \"customerName\":\"Customer STAGE\",\n"
+ " \"customerAddr\":\"FRANCE\",\n" + " \"currency\": \"EUR\"\n" + "}\n"
+ "}";
ObjectMapper objectMapper = new ObjectMapper();
Environment environment = objectMapper.readValue(json, Environment.class);
System.out.println(environment);
Which prints:
Environment(qa=UserData(customerName=Customer QA, customerAddr=UK, currency=GBP), stage=UserData(customerName=Customer STAGE, customerAddr=FRANCE, currency=EUR))
and lets you access the UserData by calling environment.getQa(); or environment.getStage();.
Another possible solution utilizes the TypeReference that can be provided for objectMapper.readValue() method. Here we parse the JSON into a Map<String, UserData>, skipping the Environment class, which makes it more dynamic, allowing you to add new environments just through the JSON:
String json = "{\n" + "\"QA\":{\n" + " \"customerName\":\"Customer QA\",\n"
+ " \"customerAddr\":\"UK\",\n" + " \"currency\": \"GBP\"\n" + " },\n"
+ "\"STAGE\" : {\n" + " \"customerName\":\"Customer STAGE\",\n"
+ " \"customerAddr\":\"FRANCE\",\n" + " \"currency\": \"EUR\"\n" + "}\n"
+ "}";
TypeReference<Map<String, UserData>> type = new TypeReference<Map<String, UserData>>() {
};
ObjectMapper objectMapper = new ObjectMapper();
Map<String, UserData> readValue = objectMapper.readValue(json, type);
System.out.println(readValue.get("STAGE"));
This prints:
UserData(customerName=Customer STAGE, customerAddr=FRANCE, currency=EUR)
Related
I have JSON-object which has a dynamic key inside it. I need to get a specific value mapped to this dynamic Key.
For example: value "10.00" will be returned for the key "value" and value REFUND_COMPLETED will be obtained as a result for the key "refundState"
Code:
public static void main(String[] args) {
String json2 = "{\n"
+ " \"refundStatusDetails\": {\n"
+ " \"txn_f2a7802c-ef84-43c3-8615-5f706b995c23\": {\n"
+ " \"refundTransactionId\": \"txn_f2a7802c-ef84-43c3-8615-5f706b995c23\",\n"
+ " \"requestId\": \"refund-request-id-1\",\n"
+ " \"refundState\": \"REFUND_COMPLETED\",\n"
+ " \"amount\": {\n"
+ " \"currency\": \"INR\",\n"
+ " \"value\": \"10.00\"\n"
+ " },\n"
+ " \"refundRequestedTime\": 1513788119505,\n"
+ "}";
System.out.println("JSON: " + json2);
JsonParser p = new JsonParser();
Map<String,String> res =check("refundState", p.parse(json2));
System.out.println("JSON: " + res.get("refundState"));
}
private static Map<String,String> check(String key, JsonElement jsonElement) {
Map<String,String> res = new HashMap<>();
if (jsonElement.isJsonObject()) {
Set<Map.Entry<String, JsonElement>> entrySet = jsonElement.getAsJsonObject().entrySet();
entrySet.stream().forEach((x) ->{
if (x.getKey().equals(key)) {
res.put(x.getKey(),x.getValue().toString());
}
});
}
return res;
}
If you are interested to access any field in a JSON object structure then you can use a method like the following one that I used to access the fields that I needed from an JSON Array structure using "package org.json;"
public static final String COLLECTION_OBJECT = "collectionObject";
public static final String FIELD = "field";
private ArrayList<Object> getSearchFilterCriteriaAsString() {
String jsonString = "{" +
"\n\"collectionObject\": " +
"[\n" +
"{" +
"\n\"field\": \"productId\"," +
"\n\"value\": \"3\"," +
"\n\"operator\": \"EQUALS\"\n" +
"},\n" +
"{" +
"\n\"field\": \"productPrice\"," +
"\n\"value\": \"15\"," +
"\n\"operator\": \"MORE_THAN\"\n" +
"},\n" +
"{" +
"\n\"field\": \"productQuantity\"," +
"\n\"value\": \"25\"," +
"\n\"operator\": \"LESS_THAN\"\n" +
"}\n" +
"]\n" +
"}";
JSONObject jsonObject = new JSONObject(jsonString);
JSONArray jsonArray = jsonObject.getJSONArray(COLLECTION_OBJECT);
ArrayList<Object> filteredObjectsList = new ArrayList<>();
if (jsonArray != null) {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject filteredObj = (JSONObject) jsonArray.get(i);
filteredObjectsList.add(filteredObj.getString(FIELD));
}
}
return filteredObjectsList;
}
As long as you know your key values you can parse any JSON as deep you want, without to care about how big it is, how many attributes it has.
I have JSON Object which has a dynamic key inside the object I need a
specific key value
The recursive method listed below is capable of fetching the value mapped to the provided key from a nested JSON-object.
The method return an optional result.
If provided JsonElement is not a JsonObject an empty optional will be returned.
Otherwise, if the given JSON-object contains the given key the corresponding value wrapped by an optional will be returned. Or if it's not the case the entry set obtained from an object will be processed with stream. And for every JSON-object in the stream method getValue() will be called recursively.
If the given key is present in one of the nested objects, the first encountered non-empty optional will be returned. Or empty optional if the key was not found.
private static Optional<JsonElement> getValue(String key, JsonElement jsonElement) {
if (!jsonElement.isJsonObject()) {
return Optional.empty();
}
JsonObject source = jsonElement.getAsJsonObject();
return source.has(key) ? Optional.of(source.get(key)) :
source.entrySet().stream()
.map(Map.Entry::getValue)
.filter(JsonElement::isJsonObject)
.map(element -> getValue(key, element))
.filter(Optional::isPresent)
.findFirst()
.orElse(Optional.empty());
}
main() - demo
public static void main(String[] args) {
String json2 =
"""
{
"refundStatusDetails": {
"txn_f2a7802c-ef84-43c3-8615-5f706b995c23": {
"refundTransactionId": "txn_f2a7802c-ef84-43c3-8615-5f706b995c23",
"requestId": "refund-request-id-1",
"refundState": "REFUND_COMPLETED",
"amount": {
"currency": "INR",
"value": "10.00"
},
"refundRequestedTime": "1513788119505"
}
}
}""";
JsonElement element = JsonParser.parseString(json2);
System.out.println(getValue("refundState", element));
System.out.println(getValue("value", element));
}
Output
Optional["REFUND_COMPLETED"]
Optional["10.00"]
Note:
If you are using Java 17 you can utilize text blocks by plasing the text between the triple quotation marks """ JSON """.
Constructor of the JsonParser is deprecated. Instead of instantiating this class, we have to use its static methods.
I have an ObjectNode, that looks as follows
{
"Header":{
"sub-header1":{
"#field":"value",
"#field":"value",
},
"sub-header2":{
"field":"",
"field":"",
"field":"",
"panel_field":{
"value":"",
"value":""
}
}
}
Now, what I want to do is to get all the fields from sub-headers in a list. This is the method that I'm employing
public static List<String> getDocumentFields(ObjectNode jsonDocumentNode) {
List<String> documentFields = new ArrayList<>();
Iterator<JsonNode> fields = jsonDocumentNode.elements();
while (fields.hasNext()) {
JsonNode jsonNode = fields.next();
Iterator<Map.Entry<String, JsonNode>> jsonFields = jsonNode.fields();
while (jsonFields.hasNext()) {
Map.Entry<String, JsonNode> jsonNodeEntry = jsonFields.next();
documentFields.add(jsonNodeEntry.getKey());
}
}
return documentFields;
}
But, I'm only getting headers in the list like {sub-header1, sub-header2}, instead of fields. How can I fix this? I'd really appreciate any kind of help.
EDIT:
While #sfiss's answer helped a great deal, I still wanted to find a way to do so without hardcoding the loop-logic and this answer turned out to be the exact thing I was looking for.
Well, it's simple, you are not iterating deep enough (field list is on third level). If you know the structure of your JSON, just iterate until you find the desired fields:
public class MyTest {
#Test
public void testJson() throws IOException {
final String json = getJson();
final JsonNode jsonDocumentNode = new ObjectMapper().readTree(json);
final List<String> fields = getDocumentFields((ObjectNode) jsonDocumentNode);
assertThat(fields, Matchers.contains("#field1", "#field2", "field1", "field2", "field3", "panel_field"));
}
public static String getJson() {
return "{\r\n" +
" \"Header\":{\r\n" +
" \"sub-header1\":{\r\n" +
" \"#field1\":\"value\",\r\n" +
" \"#field2\":\"value\"\r\n" +
" },\r\n" +
" \"sub-header2\":{\r\n" +
" \"field1\":\"\",\r\n" +
" \"field2\":\"\",\r\n" +
" \"field3\":\"\",\r\n" +
" \"panel_field\":{\r\n" +
" \"value1\":\"\",\r\n" +
" \"value2\":\"\"\r\n" +
" }\r\n" +
" } \r\n" +
" }\r\n" +
"}";
}
public static List<String> getDocumentFields(final ObjectNode jsonDocumentNode) {
final List<String> documentFields = new ArrayList<>();
for (final JsonNode header : (Iterable<JsonNode>) jsonDocumentNode::elements) {
for (final JsonNode subheader : (Iterable<JsonNode>) header::elements) {
for (final Map.Entry<String, JsonNode> field : (Iterable<Map.Entry<String, JsonNode>>) subheader::fields) {
documentFields.add(field.getKey());
}
}
}
return documentFields;
}
}
However, I would argue that it is simpler to let jackson serialize the JSON into a convenient data structure, and you just use the POJO's getters to obtain your values. That would also make it more clear than handling the JsonNode.
Just FYI, I edited your JSON slightly and used Java 8 SAM conversions to create iterables for the foreach-loops from the iterators, but you can still use your code and iterate one more level using while and iterators.
I have the following json file
[{
"en": {
"key1": "Ap",
"key2": "ap2"
}
},
{
"ar": {
"key1": "Ap",
"key2": "ap2"
}
}
]
I would like to create a Map in Java such as the key is the language (like en or ar) and the value is a object. Something like this.
public class Category {
private String key1;
private String key2;
}
Type type = new TypeToken<Map<String, Category>>() {}.getType();
Gson gson = new Gson();
InputStream in = MyClass.class.getResourceAsStream("/categories.json");
String text = IOUtils.toString(in, StandardCharsets.UTF_8);
Map<String, Category> map = gson.fromJson(text, type);
But when I run this code, I get errors:
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 3 path $[0]
Is my Json structure wrong or is there an easier way to map this?
try to read this json file
{
"ar": {
"key1": "Ap",
"key2": "ap2"
},
"en": {
"key1": "Ap",
"key2": "ap2"
}
}
The above json is collection of JsonObject like list or array, so just parse it to List of Map objects
Type type = new TypeToken<List<Map<String, Category>>>() {}.getType();
Your json is a list of maps, not only maps. So you have to add it to the type declared.
Try this:
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;
public class Category {
private String key1;
private String key2;
#Override
public String toString() {
return "Category{" +
"key1='" + key1 + '\'' +
", key2='" + key2 + '\'' +
'}';
}
public static void main(String[] args) {
String json = "[{\n" +
" \"en\": {\n" +
" \"key1\": \"Ap\",\n" +
" \"key2\": \"ap2\"\n" +
" }\n" +
" },\n" +
"\n" +
" {\n" +
" \"ar\": {\n" +
" \"key1\": \"Ap\",\n" +
" \"key2\": \"ap2\"\n" +
" }\n" +
" }\n" +
"]";
Type type = new TypeToken<List<Map<String, Category>>>() {}.getType();
Gson gson = new Gson();
List<Map<String, Category>> maps = gson.fromJson(json, type);
System.out.println(maps);
}
}
Your input is an json array with two objects. However your target variable 'type' is a of type Object and not an 'Array of Objects'. In simpler terms, Map cannot store an
Array.
Lets take a simpler approach to this problem(not a recommended approach). If we convert the map manually to an array of maps, that would look like this:
yourJson -> [Map1(en,category1(Ap,ap2)),Map2(en,category2(Ap,ap2))]
i.e. An array of Maps
So in java equivalent this becomes:
Type typeOfT2 = new TypeToken<ArrayList<HashMap<String, Category>>>() {}.getType();
List<HashMap<String, Category>> list = gson.fromJson(text, typeOfT2);
We get to what we want, but there are better ways of doing this. We need Jackson instead of Gson for this.(Some one may add a Gson based solution, pretty sure a cleaner one than above exists). Here we will use ObjectMapper from com.fasterxml.jackson.databind.ObjectMapper
ObjectMapper om = new ObjectMapper();
List<Map.Entry<String, Category>> listx = om.readValue(text, ArrayList.class);
If you print listx. You can see this(overridden toString() of Category class):
[{en={key1=Ap, key2=ap2}}, {ar={key1=Ap, key2=ap2}}]
listx is the most accurate representation of your json and not a Map.
Now if you need a map, I will leave that as an exercise for you about how to convert your List of Map.Entry to a Map implementation.
PS.: First long answer here. Apologies for any mistakes.
I'm using Jackson 2.8.9 in my application to generate some JSON. I have some unit tests in which I compare generated JSON with some files content.
When I compare my generated JSON with my file content, it does not match due to the properties order.
For the tests to be repeatable I need to have the properties sorted alphabetically. But with Jackson, it does not seems to work.
I wrote some tests for illustation. Only should_indent_properties pass.
public class FormatJsonWithJacksonTest {
private static final String INDENTED_UNSORTED = "{\r\n" +
" \"firstChild\" : {\r\n" +
" \"subChild\" : {\r\n" +
" \"alphaItem\" : \"1234567891234567\",\r\n" +
" \"otherProperty\" : \"2017-06-21\",\r\n" +
" \"someOtherProperty\" : \"NONE\",\r\n" +
" \"alphaType\" : \"KIND_OF_TYPE\"\r\n" +
" }\r\n" +
" }\r\n" +
"}";
private static final String INDENTED_SORTED = "{\r\n" +
" \"firstChild\" : {\r\n" +
" \"subChild\" : {\r\n" +
" \"alphaItem\" : \"1234567891234567\",\r\n" +
" \"alphaType\" : \"KIND_OF_TYPE\",\r\n" +
" \"otherProperty\" : \"2017-06-21\",\r\n" +
" \"someOtherProperty\" : \"NONE\"\r\n" +
" }\r\n" +
" }\r\n" +
"}";
private static final String UNINDENTED_UNSORTED = "{" +
"\"firstChild\":{" +
"\"subChild\":{" +
"\"alphaItem\":\"1234567891234567\"," +
"\"otherProperty\":\"2017-06-21\"," +
"\"someOtherProperty\":\"NONE\"," +
"\"alphaType\":\"KIND_OF_TYPE\"" +
"}" +
"}" +
"}";
private static final String UNINDENTED_SORTED = "{" +
"\"firstChild\":{" +
"\"subChild\":{" +
"\"alphaItem\":\"1234567891234567\"," +
"\"alphaType\":\"KIND_OF_TYPE\"," +
"\"otherProperty\":\"2017-06-21\"," +
"\"someOtherProperty\":\"NONE\"" +
"}" +
"}" +
"}";
#Test
public void should_sort_properties() throws Exception {
// Given
ObjectMapper objectMapper = new ObjectMapper();
objectMapper = objectMapper
.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
// When
String formattedJson = tryingToFormatJson(objectMapper, INDENTED_UNSORTED);
// Then
assertEquals(UNINDENTED_SORTED, formattedJson);
}
#Test
public void should_indent_properties() throws Exception {
// Given
ObjectMapper objectMapper = new ObjectMapper();
objectMapper = objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
// When
String formattedJson = tryingToFormatJson(objectMapper, UNINDENTED_UNSORTED);
// Then
assertEquals(INDENTED_UNSORTED, formattedJson);
}
#Test
public void should_sort_and_indent_properties() throws Exception {
// Given
ObjectMapper objectMapper = new ObjectMapper();
objectMapper = objectMapper
.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true)
.enable(SerializationFeature.INDENT_OUTPUT);
// When
String formattedJson = tryingToFormatJson(objectMapper, INDENTED_UNSORTED);
// Then
assertEquals(INDENTED_SORTED, formattedJson);
}
//
// Utils
//
private String tryingToFormatJson(ObjectMapper objectMapper, String unformattedJson)
throws IOException, JsonProcessingException {
JsonNode unsortedTree = objectMapper.readTree(unformattedJson);
Object treeToValue = objectMapper.treeToValue(unsortedTree, Object.class);
return objectMapper.writeValueAsString(treeToValue);
}
}
How could I sort my JSON with Jackson?
Do you have any solution for implementing my method tryingToFormatJson?
Is Jackson the right tool I want for doing this?
When you call objectMapper.treeToValue(unsortedTree, Object.class), the Object's type is a subclass of Map - just put a System.out.println(treeToValue.getClass()) to check.
And according to javadoc, SORT_PROPERTIES_ALPHABETICALLY doesn't sort map keys:
Feature that defines default property serialization order used for POJO fields (note: does not apply to Map serialization!)
If you want to sort the fields, you have to create a custom class for you structure, like this:
public class Tree {
private Map<String, Child> firstChild;
// getters and setters
}
public class Child {
private String alphaItem;
private String otherProperty;
private String someOtherProperty;
private String alphaType;
// getters and setters
}
And change the treeToValue call to:
Tree treeToValue = objectMapper.treeToValue(unsortedTree, Tree.class);
With this, the fields will be sorted and the test will work.
I have a JSON document that describe list of objects, it looks something like this:
[
{
"txId": "ffff",
"sender" : "0xwwwwwww",
"recepient" : "0xeferfef"
},
{
"txId": "ffff",
"sender" : "0xwwwwwww",
"recepient" : "0xeferfef"
}
...
...
]
How can I get List<String> that contains txId values from each object using only Jackson API (without converting this JSON to a list of pojo-objects then proceed this list by foreach and create new list of strings)?
You can always read a JSON document as JsonNode object with Jackson API (no need of creating POJO). Next, there are several ways of reading and manipulating the data represented as JsonNode object. One of the most convenient ways available from Java 8+ is to create a java.util.Stream<JsonNode> and collect the final list as a result of a mapping from JsonNode to String, where String is represents a value of node.txId field.
You can create java.util.Stream<JsonNode> with:
java.util.stream.StreamSupport.stream(jsonNode.spliterator(), false)
and then you can call map(node -> node.get("txId").textValue() and finally call collect() to terminate the stream and get your expected result.
Consider following code as an example (requires Java 8+):
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
final class JacksonReadExample {
public static void main(String[] args) throws IOException {
final String json = " [\n" +
" {\n" +
" \"txId\": \"ffff-001\",\n" +
" \"sender\" : \"0xwwwwwww\",\n" +
" \"recepient\" : \"0xeferfef\"\n" +
" },\n" +
" {\n" +
" \"txId\": \"ffff-002\",\n" +
" \"sender\" : \"0xwwwwwww\",\n" +
" \"recepient\" : \"0xeferfef\"\n" +
" }\n" +
"]";
final ObjectMapper mapper = new ObjectMapper();
final JsonNode jsonNode = mapper.readTree(json);
final List<String> ids = StreamSupport.stream(jsonNode.spliterator(), false)
.map(node -> node.get("txId").textValue())
.collect(Collectors.toList());
System.out.println(ids);
}
}
Output:
[ffff-001, ffff-002]