I have a stream of Kafka messages and wanted to build a HashMap<String,List<Object>> to be used as API response in Json format. expected response: actual response: Answer It is now working using these changes: Convert Kafka message string to JSONObject new JSONObject(consumerRecord.value()) Constr…
Tag: json
Custom Data Binding in Spring
I have a situation here. Suppose, I’m receiving this JSON in the @RequestBody, Which I’m binding to a POJO, like so. Now, ResourceRefRequest, is as follows, and, RefReqItem, is as follows, Things are pretty well, as I expect them to be. The only problem is, refReqItem.resourceId is null, for all r…
Jackson subtypes – how to specify a default
I want to do something like this with subtypes. I have 3 different types of objects: value can either be a string or an object. The corresponding Java classes are I don’t mind having a discriminator inside Obj1 and Obj2, but there is no place for one when the value is just a string. Is there a way that …
Adding default values to array in Jolt
I am trying to transform a json using jolt, it’s just adding some default values to an array. the input looks like this: The output should be: My spec looks right now: I just can’t pass the valC to the listWithItems and haven’t found anything in the documentation. Can someone help me with th…
How to read value from JSON with Jackson Java
I have a JSON file: I want to get the items separately. I try to do, but the result did null: This is class for Items: What I do wrong? how correctly read value from items? Answer You didn’t handle the JSON array – items – properly, so as I commented under OP, all what you need to do is
Convert Java object to json string containing a json string property already
Consider a Java object like following: so when we do, I was expecting something like: instead I got it as a json value wrapped into a double quotes and skipped by backslashes as it was double jsonized – if this is actually a verb – Answer I managed to overcome this problem by using @JsonRawValue F…
Convert nested map fields to snake case
how to convert all the nested fields of the following object rendered as json to snake_case? Given it’s read as a org.bson.Document, jackson object mapper won’t work as it’s designed for POJOs and it’s not possible to have POJOs here as the records are schema-less Answer It can be easi…
Mockito:: Unit test case for JsonProcessingException when passing object
I have a class as shown below: Now I am trying to write a unit test case for this where I can throw exception and unit test the catch clause using Mockito but I am not able to mock it. I have noticed that object mapper class internally converting everything to string even if I set name and age to
Processing a string formatted as a JSONArray
We have some code where we read a JSON file into a string and then use Json-lib to process it as follows: We now have a situation where the file is actually a JSONArray (i.e. starts with [ and ends with ]). The file passes all JSON validation tests but our code raises the following exception: We are able to
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 39 path $.message
i am making login function in android using retrofit. I have created an endpoint for login validation, then I have tested it using Postman using raw (json) and it worked. But when I enter the endpoint into android using retrofit I get an error message like this: com.google.gson.JsonSyntaxException: java.lang.…