JSON deserialize a series of objects inside brakets {} - java

I am writing a Java Library Parser for Alpha Vantage. Here is the endpoint to download some data:
https://www.alphavantage.co/query?function=DIGITAL_CURRENCY_INTRADAY&symbol=BTC&market=CNY&apikey=demo
As you can see the historical data are enclosed into { } and not into []. Do you have any idea to deserialize this? I am using Gson but I cannot find any way

First of all, your JSON structure is probably not what you want it to be. The content of the "Time Series (Digital Currency Intraday)" is an object with properties that seem to represent date. This is annoying from the start, since they are dynamically generated. It would be really simple, if the content would be a list of objects(that could also contain the date value):
{
"Time Series (Digital Currency Intraday)": [
{
"date": "2017-10-24 22:30:00",
"1a. price (CNY)": "34889.57919003",
"1b. price (USD)": "5259.23870358",
"2. volume": "5708.68994668",
"3. market cap (USD)": "30023363.11434300"
}
]
}
This would be a better structure, imo. Then all you need to do is change the property names to be more Java friendly and create some POJOs, then you could deserialize with GSON easily. See: http://www.baeldung.com/gson-deserialization-guide
As for a solution with the current structure, you could maybe make use of the entitySet() method of the JSONObject.
It returns a Set> object. Assuming you would use the method on an object representing the root Json object, the String values that you would get in the Map objects would be "Meta Data" and "Time Series (Digital Currency Intraday)". These are the properties of the root object in your JSON. And the JsonElement they are associated with, would be the value of the property, in this case another JSON object.
So you can use this and your knowledge of how the JSON is structured, to deserialize the data to some objects of a class. WIth a couple of loops, of course.

Related

How to traverse, navigate and access objects from a JSON-LD files with JSONLD-JAVA?

I need to take a large JSON-LD file as input for an algorithm that I am writing in Java. Therefore, I intend to use JSONLD-JAVA for that.
The JSONLD-JAVA page shows an example for reading a JSON-LD file, but not for navigating or traversing it, or accessing individual objects in it. Instead, it refers to the JSON-LD and JSON-LD API specifications for details on specific possible operations.
However, the JSON-LD specification simply defines the syntax and semantics of a JSON-LD, and does not say anything about how to access them, and of course neither should it, being just a specification of the format. I was expecting that kind of operation to be described in the JSON-LD API specification, but it only describes operations that convert the entire JSON-LD file into different forms (compact, expanded, flattening, and conversion to RDF). It does not seem to include operations for accessing the objects (for example, accessing the key-value pairs of an object).
So I am guessing we are supposed to read the JSON-LD file and expand or flatten it, and then access it as pure JSON. But JSONLD-JAVA methods only return instances of Object, so it's not clear to me how I can use these object to obtain the JSON key-value pairs. The only exception seems to be the method frame, which returns a Map, but it is not very clear to me what a frame is. The JSON-LD specification does not include the word "frame", and the JSON-LD API specification has a very terse explanation which does not seem to help in understanding how to access an object's key-value pairs.
The fact that I only have Object instances from JSONLD-JAVA methods also makes it look like it would be hard to use some JSON library to use them, unless I use some JSON library that knows about the internal format of these objects as formed by JSONLD-JAVA, but the page of JSONLD-Java does not mention any such library.
I was expecting to be able to read a JSON-LD file and then programmatically accessing or manipulating it within Java, and to have Java classes that correspond to the main concepts, something like a JSONLDObject with methods for providing its key-value pairs.
As I read the above pages, I get the feeling that they are meant for people that already know something that I don't. So perhaps I am missing something. Otherwise, is there a tutorial on using JSONLD-JAVA or even just the JSONLD API in order to traverse the objects?
If you read the documentation on the JSONLD-JAVA page you linked to, it starts with a commented example:
// Open a valid json(-ld) input file
InputStream inputStream = new FileInputStream("input.json");
// Read the file into an Object (The type of this object will be a List, Map, String, Boolean,
// Number or null depending on the root object in the file).
Object jsonObject = JsonUtils.fromInputStream(inputStream);
// Create a context JSON map containing prefixes and definitions
Map context = new HashMap();
// Customise context...
// Create an instance of JsonLdOptions with the standard JSON-LD options
JsonLdOptions options = new JsonLdOptions();
// Customise options...
// Call whichever JSONLD function you want! (e.g. compact)
Object compact = JsonLdProcessor.compact(jsonObject, context, options);
// Print out the result (or don't, it's your call!)
System.out.println(JsonUtils.toPrettyString(compact));
The second comment is interesting, so let me highlight it for you:
Read the file into an Object (The type of this object will be a List, Map, String, Boolean, Number or null depending on the root object in the file).
Object jsonObject = JsonUtils.fromInputStream(inputStream);
The key point is that JSONLD is JSON, and that when you've loaded it into memory like above, you can navigate that JSON structure, by casting the Object as appropriate.
Lets have a look at Example #3 from the JSON-LD specification:
{
"#context":
{
"name": "http://schema.org/name", // ← This means that 'name' is shorthand for 'http://schema.org/name'
"image": {
"#id": "http://schema.org/image", // ← This means that 'image' is shorthand for 'http://schema.org/image'
"#type": "#id" // ← This means that a string value associated with 'image' should be interpreted as an identifier that is an IRI
},
"homepage": {
"#id": "http://schema.org/url", // ← This means that 'homepage' is shorthand for 'http://schema.org/url'
"#type": "#id" // ← This means that a string value associated with 'homepage' should be interpreted as an identifier that is an IRI
}
}
}
So if you want the #id value of the image, you'd do this:
Map<String, Object> root = (Map) jsonObject;
Map<String, Object> context = (Map) root.get("#context");
Map<String, Object> image = (Map) root.get("image");
String imageId = (String) image.get("#id");
1. Convert JSON-LD to a nice nested map. Use the framing algorithm. Example: JSON-LD to normal JSON
and How to convert RDF to pretty nested JSON using java rdf4j
2. Accessing JSON-LD. I would use JsonNode together with JPointer.
On small and simple documents operating directly on the Map<String,Object> is also ok. For JsonPointer you can use Jackson JsonNode.at().
ObjectMapper mapper = new ObjectMapper();
JsonNode json = mapper.readValue(in, JsonNode.class);
String id = json.at("/#id").getText();
3. Preprocessing. In some cases it can be handy to preprocess the JSON Input. This answer lists some command line tools: XSLT equivalent for JSON

Gson: Convert json string formatted with jsonapi.org ID based format to a java POJO

There is an example of how JSON responses should be formatted here at jsonapi.org. I want to use this convention but I need to check if this data can be serialized easily by Gson. Searching the web didn't come to any related solution. This Q from SO Is the most related but the language is neither Java nor Kotlin.
The main problem is that this convention says you should place your attributes like this:
"attributes": {
"title": "JSON API paints my bikeshed!"
"city": "Paris"
...
},
But I do not know how to tell Gson to fetch attribute names and their corresponding content in this format. I do not want to make a collection of nested classes to represent that whole json string field by field.

Spring/Jackson Mapping Inner JSON Objects

I have a RESTful web service that provides JSON that I am consuming. I am using Spring 3.2 and Spring's MappingJacksonHttpMessageConverter. My JSON looks like this:
{
"Daives": {
"Daive": {},
"Daive": {},
"Daive": {},
"Daive": {}
}
}
Now everything I have read seems to indicate that this JSON should be refactored to an array of JSON Daives. However, this is valid JSON so I want to make sure that I am thinking correctly before going back to the service provider to ask for changes. In the format above, I would have to know ahead of time how many Daives there are going to be such that my DTO accounted for them. The handy dandy Jackson mapper isn't going work with this kind of JSON setup. If the JSON was altered to provide and Array of JSON Daives, I could use a List to dynamically map them using Spring/Jackson.
Am I correct? Thanks :)
According to this thread, the JSON spec itself does not forbid multiple fields with the same name (in your case, multiple fields named "Daive" in the object "Daives").
However, most parsers will either return an error or ignore any value but the last one. As you said, putting these values into an array seems much more sensible; and indeed, you'll be able to map this array to a List with Jackson.

read large json file

I have a json file with complex structure.
{"Objects":{"items":{"item":[
{
"field1": "value1",
"field2": "value2",
"field3":[
{
"label1":"1",
"label2":"2"
},
{
"label1":"3",
"label2":"4"
}]
}
,
{
//same structure as above object
}
]}}}
The file size is a little more than 1GB. I need to read an object and see what the value of a particular label is and if it matches the list I have, I need to write that object in another file else not.
I know normal JSON parser like JSONSimple won't work as it hold the data into the memory. I am trying to use Jackson, but finding hard to go over all objects as it takes one token at a time. What is an efficient way to use streaming and tree structure of Jackson for this JSON format.
Or in what way can I use script to get the data and use it?
Probably you could advance the JsonParser several times calling nextToken() until you get Token ID_START_ARRAY, call nextToken() to move to the start of the first item object and then feed the parser and POJO class representing "item" into ObjectMapper.readValue() (https://github.com/FasterXML/jackson-databind/blob/master/src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java), repeat until no more objects are found. POJO can be hand-written or generated using something like https://github.com/astav/JsonToJava/wiki/JsonToJava
Or skip Jackson entirely - write a little tokenizer yourself that will extract individual "item" json elements and feed them into JSONSimple. This way you'll maybe have a bit of wheel reinvention, but will avoid getting a lot of dependencies.

Converting JSON Object(s) from file using Java

I have a JSON file with no clue on how data will be in it nor the structure of data.
The only thing known is that it will have either an array of JSON objects or a single JSON object.
I need to get each object from the file and store it as a separate item. In case of array of objects in the file, I should get an array of JSON strings which I can store in DB.
Basically, I need to read this file and separate out each JSON object from it and store it in DB as a string.
One of the ways to do it was to use JACKSON ObjectMapper and assign these items to a Hashmap as key value pairs, but I am not sure though how it can be done If there are list of JSON Objects in the file.
Sample JSON File:
[
{
"name":"Bob",
"type":"Email",
"from":"a#a.com",
"to":"b#B.com",
"attachments":[...],
.
.
.
}
]
Do you know the Object structure that the JSON has(let it be Array or a single one) ? If Yes,
First load the json string form the file into an in memory string.
check the string for Array existence, by searching for '[',']' in the outer structure of multiple occurrences of '{' or '}'
once you know whether you have an array or a single object, you can pass it as object reference to either Jackson or GSON parsers
create in memory Array of JsonObject.class say List. It is actually better to enclose this List inside another class. say myJsonObjects and have a List inside it.
Let us see GSON parsers (by google), though Jackson can also be used in the similar implementation
Gson gson = new Gson();
if(isArray){
myJsonObjects jsonArray = gson.fromJson(jsonStringFromFile,myJsonObjects );
}
else{
gson.fromJson(jsonStringFromFile,JsonObject);
}
http://google-gson.googlecode.com/svn-history/trunk/gson/docs/javadocs/com/google/gson/Gson.html
Jackson is my favorite JSON-to-POJO library. It doesn't really matter where you're loading the JSON from (a URL or from the filesystem), there are handlers for several input sources.
Here's an example:
Map<String,Object> userData = mapper.readValue(new File("user.json"), Map.class);
As far as having an unknown number of JSON structures that you're about to parse, the first thing that comes to mind is to have a mapper for each type you're expecting. You could then wrap the parsing code in try/catch blocks so that if the first fails with whatever exception Jackson gives you when encountering an unexpected format, you can then try the next format and so on.
If you're just trying to generically parse JSON that you don't know the structure of beforehand, you can try something like this:
mapper.readValue(jsonString, new TypeReference<List<EntryType>>() {});
The documentation for Jackson is pretty good-- giving it a solid read-through should definitely help. Here's a good five minute tutorial: http://wiki.fasterxml.com/JacksonInFiveMinutes
I prefer use Gson:
Gson gson;
Map<String, Object>parameters=gson.fromJson(myString);
the rest is iterate the map, i hope help you

Categories

Resources