Json to Java Mapping - java

I want to map below Json data to java object of List<Map<String, String>> type.
Sample Json:
{
{
a:b,
c:d
},
{
e:f,
g:h,
i:j
},
{
h:k
}
}
Here a:b represents key-value pair. So a:b and c:d will be mapped to first map of the list and so on.
one way to do this is by building JSON tree and access each node and store the pair into the map.
Is there a better way to do this (cleaner approach)?

Here is the code to read a List<Map<String, String>> using the Jackson library, with your example data as input:
public class JsonTest {
public static void main(String[] args) throws Exception {
final String json
= "[\n"
+ " {\n"
+ " \"a\":\"b\",\n"
+ " \"c\":\"d\"\n"
+ " },\n"
+ " {\n"
+ " \"e\":\"f\",\n"
+ " \"g\":\"h\",\n"
+ " \"i\":\"j\"\n"
+ " },\n"
+ " {\n"
+ " \"h\":\"k\"\n"
+ " }\n"
+ "]"; // [{a:b,c:d},{e:f,g:h,i:j},{h:k}]
ObjectMapper mapper = new ObjectMapper();
TypeFactory factory = TypeFactory.defaultInstance();
List<Map<String, String>> list = mapper.readValue(json,factory
.constructCollectionType(List.class, factory
.constructMapType(Map.class, String.class, String.class)));
System.out.println(list.toString());
}
}
Note: I had to fix your outermost braces from {} to [], which is the correct JSON list syntax.

You can use Jackson to achieve this task through below example code
public class JacksonExample {
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
try {
// Convert JSON string from file to Object
User user = mapper.readValue(new File("C:\\user.json"), User.class);
System.out.println(user);
// Convert JSON string to Object
String jsonInString = "{\"age\":33,\"messages\":[\"msg 1\",\"msg 2\"],\"name\":\"xyz\"}";
User user1 = mapper.readValue(jsonInString, User.class);
System.out.println(user1);
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

First I made few changes to your json to make it valid
{
"key":
[{
"a": "b",
"c": "d"
},
{
"e": "f",
"g": "h",
"i": "j"
},
{
"h": "k"
}]
}
Please find the below code that I have tried out :
ObjectMapper mapper = new ObjectMapper();
ObjectNode objectNode1 = mapper.createObjectNode();
Map<String, Object> i = mapper.readValue(new File("J:/temp/sample.json"), Map.class);
System.out.println(i.get("key"));
System.out.println(i.values());
Output :
//For System.out.println(i.get("key"));
[{a=b, c=d}, {e=f, g=h, i=j}, {h=k}]
//For System.out.println(i.values());
[[{a=b, c=d}, {e=f, g=h, i=j}, {h=k}]]
If the above approach helps you, Make a right decision based on your needs either i.get("key") or i.values()

Just use gson library to Json to object and object to Json
import java.lang.reflect.Type;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
Object to Json
Gson gson = new Gson();
Student obj=new Student();
String jsonInString = gson.toJson(obj);
Json To Object
Student obj = gson.fromJson(jsonInString, Student.class);

Related

How to generated the csv file from json data with Java?

I try to generate csv file from json type data. These are my json test data.
{
"realtime_start":"2020-09-25",
"realtime_end":"2020-09-25",,
"units": "Percent",
"seriess": [
{
"name": "James",
"age": 29,
"house": "CA"
},
{
"name": "Jina",
"age": 39,
"house": "MA",
"notes": "Million tonne punch"
},
}
The problem is json array type "seriess" does not contain "notes" node in all every nodes.
I made the below java codes to change this json data to csv file with header columns
JSONObject json = getJsonFileFromURL(...)
JSONArray docsArray = json.getJSONArray("seriess");
docsArray.put(json.get("realtime_start"));
docsArray.put(json.get("realtime_end"));
docsArray.put(json.get("units"));
JsonNode jsonTree = new ObjectMapper().readTree(docsArray.toString());
Builder csvSchemaBuilder = CsvSchema.builder();
for(JsonNode node : jsonTree) {
node.fieldNames().forEachRemaining(fieldName -> {csvSchemaBuilder.addColumn(fieldName);} );
}
CsvSchema csvSchema = csvSchemaBuilder.build().withHeader();
CsvMapper csvMapper = new CsvMapper();
csvMapper.writerFor(JsonNode.class).with(csvSchema).writeValue(new File("test.csv"), jsonTree);
But the incorrect results are shown like below,
realtime_start,realtime_end,units,names,age,house,realtime_start,realtime_end,units,names,age,house,notes, realtime_start,.....
The generated header columns does not contain distinct values. The header columns are added in duplicate. How can I generate the distinct header like below
realtime_start,realtime_end,units,names,age,house, notes
Any idea?
Update Part
I try to extract data from the FRED (FEDERAL RESERVE BANK OF ST. LOUIS). FRED provide simple and convenient Python api like below,
from fredapi import Fred
import pandas as pd
fred = Fred(api_key='abcdefghijklmnopqrstuvwxyz0123456789')
data_unemploy = fred.search('Unemployment Rate in California')
data_unemploy.to_csv("test_unemploy.csv")
But the java apis are deprecated, so I have to develop simple Java api which convert json values to csv file. I found the below Java codes with googling
JSONObject json = getJsonFileFromURL("https://api.stlouisfed.org/fred/series/search?search_text=Unemployment+Rate+in+California&api_key=abcdefghijklmnopqrstuvwxyz0123456789&file_type=json");
JSONArray docsArray = json.getJSONArray("seriess");
docsArray.put(json.get("realtime_start"));
docsArray.put(json.get("realtime_end"));
JsonNode jsonTree = new ObjectMapper().readTree(docsArray.toString());
JsonNode firstObject = jsonTree.elements().next(); // I am struggling with this line
firstObject.fieldNames().forEachRemaining(fieldName -> {csvSchemaBuilder.addColumn(fieldName);} );
CsvSchema csvSchema = csvSchemaBuilder.build().withHeader();
CsvMapper csvMapper = new CsvMapper();
csvMapper.writerFor(JsonNode.class).with(csvSchema).writeValue(new File("test.csv"), jsonTree);
To extract columns from json data JsonNode firstObject = jsonTree.elements().next(); return the first json node. But this line does not return notes column. because the first line does not contain the notes key value.
So I change this code line to following lines
for(JsonNode node : jsonTree) {
node.fieldNames().forEachRemaining(fieldName -> {
csvSchemaBuilder.addColumn(fieldName);
} );
}
But these lines throws the results which I do not expect. The repeated duplicated columns like below
realtime_start,realtime_end,units,names,age,house,realtime_start,realtime_end,units,names,age,house,notes, realtime_start,.....
I am totally stuck with this part.
Most probably it is easiest to write a bin type class like below :
public class CsvVo {
private String realtime_start;
private String realtime_end;
private String units;
private String name;
private String age;
private String house;
private String notes;
public void setRealtime_start(String realtime_start) {
this.realtime_start = realtime_start;
}
//Other getters and Setters
Then you can Write :
public class ConvertJsonToCSVTest {
public static void main(String[] args) throws JSONException {
String jsonArrayString = "{\n" +
"\t\"realtime_start\": \"2020-09-25\",\n" +
"\t\"realtime_end\": \"2020-09-25\",\n" +
"\t\"units\": \"Percent\",\n" +
"\t\"seriess\": [{\n" +
"\t\t\t\"name\": \"James\",\n" +
"\t\t\t\"age\": 29,\n" +
"\t\t\t\"house\": \"CA\"\n" +
"\t\t},\n" +
"\t\t{\n" +
"\t\t\t\"name\": \"Jina\",\n" +
"\t\t\t\"age\": 39,\n" +
"\t\t\t\"house\": \"MA\",\n" +
"\t\t\t\"notes\": \"Million tonne punch\"\n" +
"\t\t}\n" +
"\t]\n" +
"}";
JSONObject inJson;
List<CsvVo> list = new ArrayList<>();
inJson = new JSONObject(jsonArrayString);
JSONArray inJsonSeries = inJson.getJSONArray("seriess");
for (int i = 0, size = inJsonSeries.length(); i < size; i++){
CsvVo line = new CsvVo();
line.setRealtime_start(inJson.get("realtime_start").toString());
line.setRealtime_end(inJson.get("realtime_end").toString());
line.setUnits(inJson.get("units").toString());
JSONObject o = (JSONObject)inJsonSeries.get(i);
line.setName(o.get("name").toString());
line.setAge(o.get("age").toString());
line.setHouse(o.get("house").toString());
try {
line.setNotes(o.get("notes").toString());
}catch (JSONException e){
line.setNotes("");
}
list.add(line);
}
String[] cols = {"realtime_start", "realtime_end", "units", "name", "age", "house", "notes"};
CsvUtils.csvWriterUtil(CsvVo.class, list, "in/EmpDetails.csv", cols);
}
}
csvWriterUtil is like below :
public static <T> void csvWriterUtil(Class<T> beanClass, List<T> data, String outputFile, String[] columnMapping){
try{
Writer writer = new BufferedWriter(new FileWriter(outputFile));
ColumnPositionMappingStrategy<T> strategy = new ColumnPositionMappingStrategy<>();
strategy.setType(beanClass);
strategy.setColumnMapping(columnMapping);
StatefulBeanToCsv<T> statefulBeanToCsv =new StatefulBeanToCsvBuilder<T>(writer)
.withMappingStrategy(strategy)
.build();
writer.write(String.join(",",columnMapping)+"\n");
statefulBeanToCsv.write(data);
writer.close();
} catch (IOException e) {
e.printStackTrace();
} catch (CsvRequiredFieldEmptyException e) {
e.printStackTrace();
} catch (CsvDataTypeMismatchException e) {
e.printStackTrace();
}
}
Full example is available in GitRepo
You can do it with a library Apache Commons IO
pom.xml
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
ConvertJsonToCSVTest.java
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.json.*;
public class ConvertJsonToCSVTest {
public static void main(String[] args) throws JSONException {
String jsonArrayString = "{\"fileName\": [{\"first name\": \"Adam\",\"last name\": \"Smith\",\"location\": \"London\"}]}";
JSONObject output;
try {
output = new JSONObject(jsonArrayString);
JSONArray docs = output.getJSONArray("fileName");
File file = new File("EmpDetails.csv");
String csv = CDL.toString(docs);
FileUtils.writeStringToFile(file, csv);
System.out.println("Data has been Sucessfully Writeen to "+ file);
System.out.println(csv);
}
catch(Exception e) {
e.printStackTrace();
}
}
}
Output
Data has been Sucessfully Writeen to EmpDetails.csv
last name,first name,location
Smith,Adam,London

Getting null pointer while reading the fileds from JSON to POJO

I have my json as given below which is coming as string. I want to map only two fields in SEGMENT object ex:(TYPE and UN_NUM) to a pojo. I used the following code which is returning null values.
test.json
{
"TEST": {
"NAME": "PART_TRAN",
"VERSION": "9.0",
"ID": "----",
"SEGMENT": {
"TYPE": "R",
"CLIENT_ID": "----",
"UN_NUM": "UN"
}
}
}
test.java
process(new Processor() {
#Override
public void process(Exchange exchange) throws Exception {
String data = exchange.getIn().getBody(String.class);
try{
XmlMapper xmlMapper = new XmlMapper();
JsonNode jsonNode = xmlMapper.readTree(data.toString());
ObjectMapper objectMapper = new ObjectMapper();
String value = objectMapper.writeValueAsString(jsonNode);
logger.info("Converting XML to JSON {}" , value);
SEGMENT seg = objectMapper.readValue(value, SEGMENT.class);
Test test = new Test(seg);
logger.info("Test Object {}" , test);
}catch (JsonParseException e){
e.printStackTrace();
}catch (JsonMappingException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
}
}).
SEGMENT.java
#Data
#JsonIgnoreProperties
public class SEGMENT {
#JsonIgnore
private String TYPE;
#JsonIgnore
private String CLIENT_ID;
#JsonIgnore
private String UN_NUM;
}
Test.java
#Data
public class Test {
private String NAME;
private String VERSION;
private String ID;
private SEGMENT segment;
}
Logs:
: Test Object Test(SEGMENT=SEGMENT(TYPE=null, CLIENT_ID =null,UN_NUM =null))
I just added the SEGMENT class which I'm using to map the json.
There is underscore-java library with methods U.fromJsonMap(json) and U.get(map, path). I am the maintainer of the project.
String jsonData = "{\n"
+ " \"TEST\": {\n"
+ " \"NAME\": \"PART_TRAN\",\n"
+ " \"VERSION\": \"9.0\",\n"
+ " \"ID\": \"----\",\n"
+ " \"SEGMENT\": {\n"
+ " \"TYPE\": \"R\",\n"
+ " \"CLIENT_ID\": \"----\",\n"
+ " \"UN_NUM\": \"UN\"\n"
+ " }"
+ " }"
+ "}";
Map<String, Object> jsonObject = U.fromJsonMap(jsonData);
String type = U.<String>get(jsonObject, "TEST.SEGMENT.TYPE");
String unNum = U.<String>get(jsonObject, "TEST.SEGMENT.UN_NUM");
System.out.println(type);
System.out.println(unNum);
Output:
R
UN

How to de-serialize Json to Map of Map?

I want to deserialize a json into
Map<String, Map<String, String>
This is the json structure that i want to convert.
{
"filetype": {
"cell": "value"
},
"fileType2": {
"cell2": "value2"}
}
I tried creating a class with Map<String, Map<String, String> type and tried with ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) in jackson:
class MapOfMap {
Map<String, Map<String, String>> mapOfMap;
//getter and setter
}
objectMapper.readValue(
new File(this.getClass()
.getClassLoader()
.getResource("configs/sample.json").getFile()),
MapOfMap.class
)
I am getting this error:
Unrecognized field "fileType".
Is there any way to do this?
You don't have to write a wrapping class around your Map. Just tell the Mapper you want a Map:
String jsonString = "{\n" +
" \"filetype\": {\n" +
" \"cell\": \"value\"\n" +
" },\n" +
" \"fileType2\": {\n" +
" \"cell2\": \"value2\"}\n" +
" }";
Map<String, Map<String, String>> map;
ObjectMapper mapper = new ObjectMapper();
try {
map = mapper.readValue(
jsonString,
new TypeReference<Map<String, Map<String, String>>>(){}
);
System.out.println(map);
} catch (Exception e) {
e.printStackTrace();
}
It prints: {filetype={cell=value}, fileType2={cell2=value2}}, which looks like what you want.
For reference: this
jackson
to Map
public static HashMap<String,Object> jsonToMap(String json) throws IOException {
JsonFactory factory = new JsonFactory();
ObjectMapper mapper = new ObjectMapper(factory);
TypeReference<HashMap<String,Object>> typeRef = new TypeReference<HashMap<String,Object>>() {};
return mapper.readValue(json, typeRef);
}
to Object:
public static <T> T jsonToObject(String json, Class<T> clazz) throws IOException {
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(json, clazz);
}
using gson:
String json = "{\n" +
" \"filetype\": {\n" +
" \"cell\": \"value\"\n" +
" },\n" +
" \"fileType2\": {\n" +
" \"cell2\": \"value2\"\n" +
" }\n" +
" }";
Gson gson = new GsonBuilder().create();
Map map = gson.fromJson(json, Map.class); //will be a map of maps
note that gson defaults to LinkedTreeMaps (so the order of iteration of elements is preserved)

How to map JSON (custom)

I'm a beginner and I need to sort from a JSON to be analyzed later.
I need to know the JSON fields and if it has arrays or subcategories.
I have to map JSON input, for example:
{
"car":"Audi",
"model":"2010",
"price":"30000",
"colors":[
"Grey",
"White",
"Black"
],
"otro":{
"a":1,
"b":2,
"c":[
{
"c11":"c11",
"c12":"c12"
},
{
"c21":"c21",
"c22":"c22"
}
]
}
}
Waiting as output mapping:
car
model
price
colors[]
otro.a
otro.b
otro.c[].c11
otro.c[].c12
otro.c[].c21
otro.c[].c22
This is my code:
public static void main(String[] args) {
try {
ObjectMapper mapper = new ObjectMapper();
String json = "{\"car\":\"Audi\",\"model\":\"2010\",\"price\":\"30000\",\"colors\":[\"Grey\",\"White\",\"Black\"],\"otro\":{\"a\":1,\"b\":2,\"c\":[{\"c11\":\"c11\", \"c12\":\"c12\"},{\"c21\":\"c21\", \"c22\":\"c22\"}]}}";
Map<String, Object> map = new HashMap<String, Object>();
// convert JSON string to Map
map = mapper.readValue(json, new TypeReference<Map<String, Object>>() {
});
for (Map.Entry<String, Object> entry : map.entrySet()) {
System.out.println(entry.getKey() + " - " + entry.getValue().getClass());
if (entry.getValue() instanceof List) {
for (Object object : ((List)entry.getValue())) {
System.out.println("\t-- " + object.getClass());
}
}
}
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
You can make a POJO and map the JSON to the POJO and do whatever you would like with it. Another option that is pretty powerful is using the JsonNode objects. They have lots of helper methods for figuring out the types of each node. Here are some quick examples https://www.stubbornjava.com/posts/practical-jackson-objectmapper-configuration#jsonnodes-and-nested-objects

Map to JSON in Java

i have a map like this
MAP:
{
facility-1={
facility-kind1={param1=XPath-1, param2=XPath-2},
facility-kind2={param1=XPath-1, param2=XPath-2},
facility-kind3={param1=XPath-1, param2=XPath-2}
},
facility-2={
facility-kind1={param1=XPath-1, param2=XPath-2},
facility-kind2={param1=XPath-1, param2=XPath-2},
facility-kind3={param1=XPath-1, param2=XPath-2}
}
}
I want to convert it into JSON formated like this
[
{"title": "Item 1"},
{"title": "Folder 2",
"children": [
{"title": "Sub-item 2.1"},
{"title": "Sub-item 2.2"}
]
},
{"title": "Folder 3",
"children": [
{"title": "Sub-item 3.1"},
{"title": "Sub-item 3.2"}
]
},
{"title": "Item 5"}
]
I tried to use GSON But the resulting output was not what I wanted:
{
"facility-1": {
"facility-kind1":
{"param1":"XPath-1","param2":"XPath-2"},
"facility-kind2":
{"param1":"XPath-1","param2":"XPath-2"},
"facility-kind3":
{"param1":"XPath-1","param2":"XPath-2"}
},
"facility-2": {
"facility-kind1":
{"param1":"XPath-1","param2":"XPath-2"},
"facility-kind2":
{"param1":"XPath-1","param2":"XPath-2","param3":"XPath-3"},
"facility-kind3":
{"param1":"XPath-1","param2":"XPath-2"}
}
}
how can a get a json formated as i want??
You need to transform your JSON to the new format you have provided.
Data to convert:
static String json =
"{\n" +
" facility-1={\n" +
" facility-kind1={param1=XPath-1, param2=XPath-2},\n" +
" facility-kind2={param1=XPath-1, param2=XPath-2},\n" +
" facility-kind3={param1=XPath-1, param2=XPath-2}\n" +
" },\n" +
" facility-2={\n" +
" facility-kind1={param1=XPath-1, param2=XPath-2},\n" +
" facility-kind2={param1=XPath-1, param2=XPath-2},\n" +
" facility-kind3={param1=XPath-1, param2=XPath-2}\n" +
" }\n" +
"}\n";
Using GSON
Create some classes that you want to handle the data, they can look something like:
static class Facility {
List<Kind> children = new LinkedList<Kind>();
}
static class Kind {
String title;
Map<String, String> params;
public Kind(String title, Map<String, String> params) {
this.title = title;
this.params = params;
}
}
The next step is too look at the source and create a represenatation of it. I would use:
Map<String, Map<String, Map<String, String>>>
since the input data is layed out like it. To convert it using Gson now is quite easy:
public static void main(String... args) throws Exception {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
Type type = new TypeToken<
Map<String, Map<String, Map<String, String>>>>() {}.getType();
Map<String, Map<String, Map<String, String>>> source =
gson.fromJson(json, type);
Map<String, Facility> dest = new HashMap<String, Facility>();
for (String facilityName : source.keySet()) {
Map<String, Map<String, String>> facility = source.get(facilityName);
Facility f = new Facility();
for (String kindName : facility.keySet())
f.children.add(new Kind(kindName, facility.get(kindName)));
dest.put(facilityName, f);
}
System.out.println(gson.toJson(dest));
}
Using JSONObject/JSONArray
public static void main(String... args) throws Exception {
JSONObject source = new JSONObject(json);
JSONArray destination = new JSONArray();
for (Iterator<?> keys = source.keys(); keys.hasNext(); ) {
String facilityName = (String) keys.next();
JSONObject kinds = source.getJSONObject(facilityName);
JSONArray children = new JSONArray();
for (Iterator<?> kit = kinds.keys(); kit.hasNext(); ) {
String kind = (String) kit.next();
JSONObject params = kinds.getJSONObject(kind);
JSONObject kindObject = new JSONObject();
kindObject.put("title", kind);
for (Iterator<?> pit = params.keys(); pit.hasNext(); ) {
String param = (String) pit.next();
kindObject.put(param, params.get(param));
}
children.put(kindObject);
}
JSONObject facility = new JSONObject();
facility.put("title", facilityName);
facility.put("children", children);
destination.put(facility);
}
System.out.println(destination.toString(2));
}
What you want is pretty printing.
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String json = gson.toJson(yourMap);

Categories

Resources