Parse a JSON string in Java - java

I would like to know how I can access the data from the below JSON String.
{
"posters": {
"thumbnail": "http://google.com/images/11420914_ori.jpg",
"profile": "http://google.com/images/11420914_ori.jpg"
}
}
I want this to be done in Java Lists (NOT by using JSON Parsers)
Expected output:
thumbnail: "http://google.com/images/11420914_ori.jpg"
profile: "http://google.com/images/11420914_ori.jpg"

You need a Json Mapper, I recommend you to read this : Jackson in 5 minutes

Related

How do I remove all control characters in a string in Java?

My REST API, which is build with Spring in Java, produces an invalid JSON object, because it contains multiple breaks in a string, which lead to the problem, that the string has an unexpected end and the rest doesn't count as part of the string anymore, example:
{
"status": "Success",
"message": "Lorem ipsum",
"data": {
"correct": [
{
"record": "ULTRA LONG
XML STRING
WITH BREAKS",
"code": 0,
"errors": []
}
]
}
}
The error arises in the data -> correct -> record string field, because it contains breaks which splits the original string.
My API endpoint serializes the above JSON like this:
#PostMapping(value="/check-records",
consumes=MediaType.APPLICATION_JSON_VALUE,
produces=MediaType.APPLICATION_JSON_VALUE)
public Response checkRecords(#RequestBody(required=true) Records records) {
// Check records
return new Response("Success", "Lorem ipsum", data);
}
Response is a class, which automatically gets serialized into a JSON object after returning. data is a map in order to create the above JSON structure.
I couldn't find any suitable solution for my problem yet. Does anybody has an idea how I could remove all breaks, spaces or control characters before I serialize the JSON object?
I appreciate any kind of help, sheers! :)
Thanks to #pringi. He suggested to use a regex to remove all control characters in Java before I serialize the JSON object.
String record = orginalRecord.replaceAll("[\\\\p{Cntrl}^\\r\\n\\t]+", "")
You can find more informations about regex in the original question: How to remove control characters from java string?

How to expand JSON structure in JAVA

I have a JSON structure, which is small.
My requirement is to expand the JSON structure.
Example JSON structure :
'{
"CallLog":{
"Three":{
"age":100,
"name":"Sample",
"Other":100,
"Add":"Sample"
},
"One":{
"CallLogEntry":[
{
"ContryCode":{
"CountryCode":123
}
},
{
"Phone Number":{
"PhoneNumber":456
}
},
{
"Name":{
"name":456
}
}
]
},
"Two":{
"age":100,
"name":"Sample",
"Other":100,
"Add":"Sample"
}
}
}
So, i want to expand his JSON.
Lets say i want repeat "One" node 10 times, and "two" node 5 times, and then write this expanded JSON in the new JSON.
How can i do this?
If you want to do it through java, i would recommend converting this JSON to java code first.Once you get the corresponding classes e.g. CallLog.java,CallLogEntry.java etc , you can define a new Class CallLog1.java and declare 10 'One' , 5 'Two' etc , as per your requirement and then create the JSON object. You can copy paste your json to http://www.jsonschema2pojo.org/ to get POJO , and then create you own POJO , and convert it into JSON , using library like GSon.

Parsing a subset of JSON in Java using Jackson

Given a Json, is it possible to use Jackson to only parse out a section of the message?
Say that the data I'm interested in is buried in a deep hierarchy of fields and I simply do not care about creating DTO classes for each and every class.
Given a very simplified scenario I'd like to model the Telephone class without knowing anything about the structure before it:
...{
"firstName": "John",
"lastName" : "doe",
"age" : 26,
"address" : {
"streetAddress": "naist street",
"city" : "Nara",
"postalCode" : "630-0192"
},
"phoneNumbers": [
{
"type" : "iPhone",
"number": "0123-4567-8888"
},
{
"type" : "home",
"number": "0123-4567-8910"
}
]
}....
I'm thinking something in the terms of using json-path together with deserializing just the parts I'm interested of. Some pseudo:
List<Telephone> phoneNrs = parse(".my.deep.structure.persons.phoneNumbers", List<Telephone.class>);
ObjectMapper mapper = new ObjectMapper();
JsonNode json = mapper.readTree("... your JSON ...");
Using the JsonNode object you can then call get("my").get("deep").get("structure") to get the node you want.
Once you got your hands on that node, a simple call to mapper.treeToValue(myDeepJsonNode, Telephone[].class) will get you your array ofTelephone. You can get a list using a TypeReference as well.
To get to your deep JsonNode you can also use the findValue and findPath methods.
The Javadoc:
https://fasterxml.github.io/jackson-databind/javadoc/2.2.0/com/fasterxml/jackson/databind/JsonNode.html
Yes, it is possible the way you have mentioned in the Pseudo code. "phoneNumbers" is a key and value returned can be passed on to Jackson deserialiying.
If the response is an array of maps then you can iterate through each one of them and use the yourResponseAsJSONObject.get('phoneNumbers') method to get the value and pass it on to Jackson
or use JsonPath as mentioned by #dimas
You can use JsonPath library. With this library you can map your JsonPath output directly into POJO's.
Pseudo:
List<Telephone> phoneNrs = JsonPath.parse(json).read("$.my.deep.structure.persons.phoneNumbers", List.class);
To do this efficiently with Jackson, use the Streaming API via the JsonParser class (http://fasterxml.github.io/jackson-core/javadoc/2.5/com/fasterxml/jackson/core/JsonParser.html).
This approach will allocate no additional memory and will not incur the cost of deserializing values for all of the skipped data. Since the code will be much longer and more difficult to read than using Jackson's ObjectMapper, only do this if profiling shows unacceptable GC activity or CPU usage during parsing.
You can skip all of the nodes that you are uninterested in until you hit the "phoneNumbers" key. Then you can call the readValueAs function to deserialize the array of phone number dictionaries like so readValueAs(new TypeReference<MyPhoneNumberType[]>()).
See also:
a tutorial on reading and writing with JsonParser: http://www.cowtowncoder.com/blog/archives/2009/01/entry_132.html
The main documentation: https://github.com/FasterXML/jackson-core

How to parse JSON data without key names into a Java class structure?

I successfully followed the example Simple Spring code to parse JSON into a Java class structure using Jackson.
Now i am looking for a hint how to do the same for JSON data without key names, e.g.
{
"10869918": {
"BRANCH": "Dienstleistungen",
"SECTOR": "Diverse"
},
"12254991": {
"BRANCH": "Luft- und Raumfahrtindustrie",
"SECTOR": "Logistik"
},
"12302743": {
"BRANCH": "Touristik und Freizeit",
"SECTOR": "Medien/Freizeit"
}
}
I doubt this is possible with a POJO-JSON mapper. You could use libraries like json-simple to parse the JSON string into Java objects (which basically are maps and lists) and access values like "10869918" by reading the keys of those maps.

Need help working with a json formatted string. (Java)

I have the contents of this page stored in a string variable. I've spent hours looking at different ways of working with json in java, and have learned a lot about the build path, dependencies, ssl certificates, and so on. I say that to emphasize the fact that I have been looking for an answer to this question independently. I've finally caved and I need help.
I'm looking for a way to turn this string into some kind of json array so that I could access the individual elements more easily. For example, something along the lines of:
int map = jsonArray[index].getInt("map_Id");
I hope that what I'm trying to do is clear enough. Any help would be appreciated.
edit: I'm currently attempting this with gson but am open to suggestions.
You can use the Jackson libraries used for JSON parsing as follows:
public static int getIntFromJson(String jsonString)
{
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree(jsonString);
int id = node.get(0).get("age").asInt();
return id;
}
Assuming that your jsonString will be something like this, the above code will return39.
[
{ "name": "Dagny Taggart", "age": 39 },
{ "name": "Francisco D'Anconia", "age": 40 },
{ "name": "Hank Rearden", "age": 46 }
]
You can pick a library (in this case, to decode JSON) from the Java section at the bottom of: http://json.org
YOu can have a look to the library below:
json-java
You can use the JSONObject to parse a String representing a JSON object and then extract the values you are interested in.

Categories

Resources