Skip to content
Advertisement

Tag: json

How to properly convert HashMap<String,List> to Json with array of json object

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()) Construct a JSONObject from a Map using jsonObject = new JSONObject(responses); Return Map<String, Object> using jsonObject.toMap();

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 refs. I want to populate each of, refReqItem.resourceId, in the

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 I

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 this? Thank you in advance! Answer One

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 From documentation https://fasterxml.github.io/jackson-annotations/javadoc/2.5/com/fasterxml/jackson/annotation/JsonRawValue.html it states

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 easily done by adding a custom key serializer to SimpleModule, then

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.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 39

Advertisement