I have a following object:
String jsonObject = "{\"cat\": \"nice cat\"}"
From this I want to get com.fasterxml.jackson.module.jsonSchema.JsonSchema class object.
But I can't find a way how to achieve this without having a Java class beforehand.
Any help would be much appreciated.
The goal I am trying to achieve is to generate Kafka Connect Schema from a JSON string. For this I need first to get JsonSchema from a JSON string.
After more research and more thought put into it, such method does not exist for obvious reasons. Imagine for example a valid JSON:
{
"a": [],
"b": null
}
Then there is no way to tell what are types of a or b.
Therefore I need to be more clever here in what I am trying to achieve.
Related
I'm working on a project at the moment that requires me to take in a various currency pairs and generate a response object from an API call response. I'm using jackson to map the JSON response to java objects then reading data from an ArrayList generated. The problem is the JSON response can have the different currency pairing strings as a key for the list of pairing data. Here's what a typical response looks like:
{"error":[],"result":{"XXBTZUSD":[[1647062100,"39091.2","39184.9","39088.9","39139.0","39150.9","59.22447291",161],}[1647063000,"39138.9","39188.4","39138.9","39151.2","39174.2","2.92905848",126]]}
The problem arises when I try to pull data from a different currency pair as my result object is hard coded to pull the data for the JSON key XXBTZUSD. Here's what my result object looks like:
public class Result{
#JsonProperty("XXBTZUSD")
public ArrayList<ArrayList<Object>> candles;
public int last;
}
I was thinking having the #JsonProperty be variable and pass in the key from the json response to correct pull the currnecy pair I set it to, but JsonProperty needs to be a constant. The only way around this I can see is to have a ton of different classes for each currency pair but that would be inefficient and take around 15 separate classes to do. I'm not too familiar with the jackson library. If anyone has any ideas of how to solve this I would be greatly appreciative, I've been trying to figure out a way around this for awhile now. Thank you!
If keys can be different, one option is to use a Map. Your Result class won't be needed, parse the result property into Map<String, Object>. Then extract like this:
Map<String, Object> result = deserialize();
ArrayList<ArrayList<Object>> arrays = (ArrayList<ArrayList<Object>>) result.get("XXBTZUSD");
Just change property depending on which currency pair you need.
Another option is writing custom deserializer, in which to always put value in your candles field, regardless of how the property is named in json.
I'm trying to parse the Wikidata JSON dump using the Gson streaming API, since the file is around 70GB of json. The overall structure of the file is as follows:
[
{"type":"item",... other fields ...},
{"type":"property",... other fields ...},
.....
]
It is an array of objects in which each object can be of type item or property and I would like to instantiate a different class (namely I have a corresponding Item and Property class in my Java code) according to the object that I encounter.
Basically, I'd like to look at the type field and then parse the following JSON accordingly. Since the JsonReader doesn't seem to provide a getNextJsonObject() or similar function, is there a way to do this besides preprocessing the whole file and splitting the entries into two separate ones? The file is so big that I'd like to avoid the extra preprocessing step when I could do everything on the fly.
I actually found a very easy solution after a bit of thinking. The Gson API provides the method:
Gson.fromJson(JsonReader reader, Class class)
This will read the next object from the reader and deserialize to the class you pass as parameter. Since in my case I don't know which class to serialize to I can do the following:
JsonObject asd = gson.fromJson(reader, JsonObject.class);
if (asd.get("type").getAsString().equals("item")) {
// Instantiate item
} else {
// Instantiate property
}
I'm parsing json with Gson but I'm struggling with the data I'm getting. This is part of an API out of my control (openFDA) so changing that might not be an option.
Here's the json I'm strugling with: https://api.fda.gov/device/event.json?search=device.generic_name:generator&limit=10
There are some fields that are not consistent, for example remedial_action. Sometimes it comes out like this:
"remedial_action": [
"Recall"
]
and in other results like this:
"remedial_action": ""
So it's either an array or a plain string. Is there a way to handle this? If not possible in Gson, any other json parsing library that can help?
I created my pojos here in case someone needs the code. There are a few files created from that and didn't want to spam them here. I can add them if needed.
Update: The bug has been confirmed and it's scheduled for a fix.
It is possible through GSON, by using a TypeAdapter.
Here are the initial steps I would use to do that:
Create a POJO that contains the array and the String. Let's call it RemedialAction.
In your original POJO, create an attribute of the new class.
Create a class that extends TypeAdapter<RemedialAction>.
Override the read() and write() methods and create the logic in them.
That should be a little hard to parse, though. Read this tutorial for more information.
Note: you can customize getRemedialAction() to give you only the valid return -- array or String.
I have a in-house api (that I can't edit) that parses json into a POJO.
The function looks like parse(String jsonString, Class jsonObj)
My problem is the the json string is dynamic. I don't always expect the same json object, so I want to avoid some ugly way of figuring out which json it is and pass in the correct class.
It would be nice to skip creating a bunch of classes to pass in.
How do I achieve this? I was thinking maybe reflection but not sure yet.
I'm very new to Java. I'm just parsing a string and getting the Json Response like this:
{
"customer_id": "user",
"merchantId": "xxxx",
"cards":
[
{
"card_token": "715fc10a-e7b3-48a1-b6e7-09e71ac050f8",
"card_number": "11111111",
"card_isin": "23232",
"card_exp_year": "2013",
"card_exp_month": "12"
}
]
}
For some reason I wish I could be able to represent this in a POJO. Note that the cards field can consist of more than 1 field.
I'm new to Java. I don't want the code for doing it, but I want to know what is the best way to represent these structure in POJO.
You can use GSON that can easily convert json to java object (generic) and vice versa which further you can use for your POJO.
- Manual parsing of JSON to object will be a pain.
- Its better to go with Jackson, which i use.
- Or you can also choose GSON (created by google for its internal use initially).
See this link for implementation example:
http://www.mkyong.com/java/json-simple-example-read-and-write-json/
The two dominate JSON processors for Java are GSON (as #Abu points out in his answer) and Jackson. These libraries will help you map a Java POJO to a JSON object, and vice-versa.
Here is a comparison on SO.
You can also use Jackson JSON. It's a bit faster and it works great with the Java API for RESTful Services (JAX-RS), the Java standarization for REST api's.
Here's a better comparison between the two: Jackson Vs. Gson
To read an object from String, checkout the ObjectMapper class.