populate combobox with part of values from json - java

I'm trying to fill a combobox with a specific field (name) from a json file.
parser file to extract information from json:
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;
public class ReferencesParser {
private final File jsonFile;
public ReferencesParser(File jsonFile) {
this.jsonFile = jsonFile;
}
public Map<ReferencesEnum, Reference> parseReferenceFile() {
Map<ReferencesEnum, Reference> referencesMap = new HashMap<>();
try {
String content = new String(Files.readAllBytes(this.jsonFile.toPath()));
JSONObject json = new JSONObject(content);
for (Object object : (JSONArray) json.get("genes")) {
JSONObject geneObject = (JSONObject) object;
Long id = geneObject.getLong("id");
String name = geneObject.getString("name");
String sequence = geneObject.getString("sequence");
Reference reference = new Reference(id, name, sequence);
referencesMap.put(ReferencesEnum.valueOf(name), reference);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return referencesMap;
}
}
genes.json file containing data to implement:
{
"genes": [
{ "id":"1","name":"gene1", "sequence": "gcattgtgggcgctatattgt" },
{ "id":"2","name":"gene2", "sequence": "gcattgtgggcgctatattcc" },
{ "id":"3","name":"gene3", "sequence": "gcatcgtgggcgctatatcat" }
]
}
and I'm trying to populate a combobox with 'name' value from json using a controller file:
...
#FXML
private ComboBox<String> choosegene;
#FXML
public void initialize(){
try {
populateGene_reference();
// System.out.println(geneFile);
System.out.println(referencesMap);
} catch (IOException e) {
e.printStackTrace();
} catch (CompoundNotFoundException e) {
e.printStackTrace();
}
};
private void populateGene_reference() throws IOException, CompoundNotFoundException {
URL url = getClass().getResource("/genes.json");
if (url != null) {
File geneFile = new File(url.getPath());
// String _Path = url.getPath();
// System.out.println("URL = " + url);
ReferencesParser parser = new ReferencesParser(geneFile);
// System.out.println("genefile = " + geneFile);
Map<ReferencesEnum, Reference> referencesMap = parser.parseReferenceFile();
// Map<ReferencesEnum, Reference> test parser.parseReferenceFile();
System.out.println("refmap = " + referencesMap);
choosegene.getItems().add(String.valueOf(referencesMap));
I have tried different ways to get my gene names but 'system.out.println' give me this:
refmap = {gene2=gene2 gcatcgtgggcgctatatcat}
refmap2 = {}
What did I miss?
Thank you for your help

ok referencesMap was ok but not choosegene, this works for me:
private void populateGene_reference() throws IOException, CompoundNotFoundException {
URL url = getClass().getResource("/main/genes.json");
if (url != null) {
File geneFile = new File(url.getPath());
ReferencesParser parser = new ReferencesParser(geneFile);
Map<ReferencesEnum, Reference> referencesMap = parser.parseReferenceFile();
for (ReferencesEnum key : referencesMap.keySet()) {
choosegene.getItems().add(referencesMap.get(key).getName());
}
Hope it will help those having the same issue!

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

Grouping ArrayList into JSON Arrays/Objects

I have a file structure in a database, with path and filename as columns and looking to group them together, but not sure how to do that. Here is a stripped down version of what I am using currently:
//in this example, list is the returned data
JSONArray jList1 = new JSONArray();
for (int a = 0; a < list.size(); a++){
Object[] fileObj = (Object[])list.get(a);
String folder = (String)fileObj[0];
String filename = (String)fileObj[1];
JSONObject jObj1 = new JSONObject();
jObj1.put("path",folder);
jObj1.put("filename",filename);
jList1.add(jObj1);
}
response.getWriter().write(jList1.toJSONString());
This puts everything into the same JSONArray. Here is an example of the output:
[
{
path: "/a",
filename: ""
},
{
path: "/a",
filename: "file1.png"
},
{
path: "/a/b",
filename: ""
},
{
path: "/a/b",
filename: "file2.png"
},
{
path: "/a/b",
filename: "file3.png"
},
{
path: "/a/c",
filename: ""
},
{
path: "/a/c",
filename: "file4.jpg",
},
{
path: "/",
filename: "file5.jpg",
},
{
path: "/",
filename: "file6.jpg",
}
]
I'd like to have it like:
[
{
path: "/a",
filename: ""
files: [
{
path: "/a/b",
filename: ""
files: [
{
path: "/a/b",
filename: "file2.png"
},
{
path: "/a/b",
filename: "file3.png"
}
]
},
{
path: "/a/c",
filename: ""
files: [
{
path: "/a/c",
filename: "file4.jpg",
}
]
},
{
path: "/a",
filename: "file1.png"
}
]
},
{
path: "/",
filename: "file5.jpg",
},
{
path: "/",
filename: "file6.jpg",
}
]
Now I could do this knowing the exact number of levels, but not sure how to do with an unknown number of levels of the tree. There are other properties in each object that I have taken out for the example, which is why I am not just putting the filename itself in the array.
I left out the database structure as don't think its really needed. Only thing might be how its ordered, which I have:
order by (CASE WHEN path='/' THEN 1 ELSE 0 END), path, filename
Basically have folders at the top and files at the bottom in each level.
Can someone put me on the right track to structuring the JSON the way I am looking for? I know it will have to be some sort of storing records comparing to previous ones, but just can't seem to figure out how to structure it. I had thought about having different queries for each directory, but that will cause a lot of hits to the database, resulting in performance issues.
I have a suggestion to try to solve your problem.
First create a file to store the values of each file / directory. Here's the example:
import java.util.ArrayList;
import java.util.List;
public class Archive implements Comparable<Archive> {
private String path;
private String filename;
List<Archive> files;
public Archive(String path, String filename) {
this.path = path;
this.filename = filename;
this.files = new ArrayList<Archive>();
}
public String getPath() {
return path;
}
public String getFilename() {
return filename;
}
public List<Archive> getFiles() {
return files;
}
#Override
public String toString() {
return "Path = "+ path + ", Filename = "+ filename;
}
#Override
public int compareTo(Archive archive) {
return getPath().compareTo(archive.getPath());
}
}
Now let's create the method that will read your folder and create the Archive class list:
public static void listFilesFromFolder(final File folder, final List<Archive> listArchive) {
for (File file : folder.listFiles()) {
if (file.isFile()) {
String path = file.getPath().replace(mainFolderPath, "").replace(file.getName(), "");
Archive archive = new Archive(path, file.getName());
listArchive.add(archive);
}
}
for (File file : folder.listFiles()) {
if (file.isDirectory()) {
String path = file.getPath().replace(mainFolderPath, "");
Archive archive = new Archive(path, "");
listArchive.add(archive);
listFilesFromFolder(file, archive.getFiles());
}
}
}
Next we will create the method that converts a List to JSON.
public static String listToJSON(List<Archive> listArchive) {
ObjectMapper mapper = new ObjectMapper();
try {
return mapper.writeValueAsString(listArchive);
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
And finally we will create the Main method to run our application:
public class Test {
final static String mainFolderPath = "c:\\MyFolder";
public static void main(String[] args) {
File folder = new File(mainFolderPath);
List<Archive> listArchive = new ArrayList<Archive>();
listFilesFromFolder(folder, listArchive);
Collections.sort(listArchive);
System.out.println( listToJSON(listArchive) );
}
}
For information only, follow the imports that were used:
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
I hope I've helped.
I ended up doing this by creating a ResourceTree class:
public class ResourceTree {
private String parentPath;
private List<ResourceTree> childrenItems;
private JSONObject jObj1;
public ResourceTree() {
this.parentPath = "";
this.childrenItems = new ArrayList<ResourceTree>();
}
public String getParentPath() {
return parentPath;
}
public void setParentPath(String parentPath) {
this.parentPath = parentPath;
}
public JSONObject getResourceObj() {
return jObj1;
}
public void setResourceObj(JSONObject jObj1) {
this.jObj1 = jObj1;
}
public List<ResourceTree> getChildrenItems() {
return childrenItems;
}
public void setChildrenItems(List<ResourceTree> childrenItems) {
this.childrenItems = childrenItems;
}
public void addChildrenItem(ResourceTree childrenItem){
if(!this.childrenItems.contains(childrenItem))
this.childrenItems.add(childrenItem);
}
public JSONArray getFileList() {
JSONArray jList1 = new JSONArray();
JSONObject jObj2 = new JSONObject();
List rtChilds = getChildrenItems();
for (int i = 0; i < rtChilds.size(); i++) {
ResourceTree rt = (ResourceTree)rtChilds.get(i);
jObj2 = rt.getResourceObj();
if (rt.getChildrenItems().size() > 0) {
jObj2.put("files",rt.getFileList());
}
jList1.add(jObj2);
}
return jList1;
}
}
From there, I create a ResourceTree for every item and added to the child items where applicable.
//list is an ArrayList of objects
if (list != null && list.size() > 0) {
Map<String, ResourceTree> hm = new LinkedHashMap();
for (int b = 0; b < list.size(); b++){
Object[] fileObj = (Object[])list.get(b);
if (fileObj != null) {
String folder = (String)fileObj[0];
String parentFolder = folder;
String fn = (String)fileObj[1];
String type = "";
if (fn.equals("")) {
parentFolder = folder.substring(0,folder.lastIndexOf("/"));
}
jObj1 = new JSONObject();
jObj1.put("path",folder);
jObj1.put("filename",fn);
ResourceTree rt = new ResourceTree();
rt.setResourceObj(jObj1);
rt.setParentPath(parentFolder);
if (fn.equals("")) {
hm.put(folder,rt);
}
ResourceTree rtParent;
if (hm.containsKey(parentFolder)){
rtParent = hm.get(parentFolder);
} else {
rtParent = new ResourceTree();
rtParent.setParentPath(parentFolder.substring(0,parentFolder.lastIndexOf("/")));
hm.put(parentFolder,rtParent);
}
rtParent.addChildrenItem(rt);
}
}
List<ResourceTree> DX = new ArrayList<ResourceTree>();
for (ResourceTree rt : hm.values()) {
//path is the top level folder specified
if (rt.getParentPath().equals(path.substring(0,path.lastIndexOf("/")))) {
jList1.addAll(rt.getFileList());
}
}
}
}

itertaing over inputjsonarray

import org.json.JSONObject;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* #author rajshree.some
*/
public class sample {
public static void main(String[] args) {
// JSONArray arrayinputip = new JSONArray();
// arrayinputip.put("10.253.140.116");
// arrayinputip.put("10.253.140.111");
// arrayinputip.put("10.253.140.118");
// arrayinputip.put("10.253.140.110");
// JSONArray arrayinputusername = new JSONArray();
// arrayinputusername.put("10.253.140.116");
// arrayinputusername.put("10.253.140.111");
// arrayinputusername.put("10.253.140.118");
// arrayinputusername.put("10.253.140.110");
// JSONArray arrayinput = new JSONArray();
// arrayinput.put("10.253.140.116");
// arrayinput.put("10.253.140.111");
// arrayinput.put("10.253.140.118");
// arrayinput.put("10.253.140.110");
JSONObject inputJsonObj1 = new JSONObject();
JSONObject inputJsonObj2 = new JSONObject();
JSONObject inputJsonObj3 = new JSONObject();
JSONArray array= new JSONArray();
try {
inputJsonObj1.put("ipaddress","10.253.140.116");
inputJsonObj1.put("username","bwadmin");
inputJsonObj1.put("password","c0mcast!");
inputJsonObj2.put("ipaddress","10.253.140.117");
inputJsonObj2.put("username","bwadmin");
inputJsonObj2.put("password","bwadmin!");
array.put(inputJsonObj1);
array.put(inputJsonObj2);
// inputJsonObj3.put("server",array);
System.out.println(array);
Client client = Client.create();
WebResource webResource = client.
resource("http://10.85.249.29:8080/checkRest/CheckxARC/getVersion");
ClientResponse response = webResource.type("application/json").
post(ClientResponse.class, array.toString());
String output = response.getEntity(String.class);
System.out.println(" op--->" + output);
} catch (Exception e) {
System.out.println(e.getMessage() + " ");
}
}
}
//This is my clinet code for calling a resftful webservice.
//This is my api
#Path("/getVersion")
#POST
#Produces(MediaType.APPLICATION_JSON)
public String getVersion(String getVersionJson) {
String version = "", patches = "", connectionStatus = "", output1 = "", output2 = "";
String output3[] = new String[100];
try {
JSONArray inputarray = new JSONArray();
inputarray.put(getVersionJson);
JSONObject inputJson = new JSONObject(getVersionJson);
String ip = inputJson.getString("ipaddress").trim();
for(int i=0;i<inputarray.length();i++){
output3[i]=inputarray.getString(inputarray[i]);
}
String userName = inputJson.getString("username").trim();
String passWord = inputJson.getString("password").trim();
connectionStatus = getSSHConnection(ip, userName, passWord);
if (connectionStatus.equals("Connected")) {
//Version Check
expect.send("bwshowver" + "\n");
if (expect.expect("$") > -1) {
String contt = "";
contt = (expect.before);
if (contt != null && contt != "") {
contt = contt.replaceAll("\n+", "\n");
contt = contt.replaceAll(" +", " ");
String splitter[] = contt.split("\n");
for (int i = 0; i < splitter.length; i++) {
//
if (splitter[i].contains("Patches")) {
patches = splitter[i];
}
//version
if (splitter[i].contains("version")) {
version = splitter[i];
}
// output1=version.toString();
// output2=patches.toString();
// output3=output1+output2;
//
output1 = contt;
}
}
} else {
output1 = "Error in version check";
System.out.println("Error in version check");
}
} else {
output1 = connectionStatus;
System.out.println(connectionStatus);
}
} catch (Exception e) {
output1 = "Error";
// logger.error("Exception in getVersion Function-ServService Class: " + e.getMessage());
} finally {
stopSSH();
}
return output1;
}
//In my client calling a restful webservice I am sending ipaddress,password,username of two service and I am binding them in an array and calling them.
Now in the calling method I need to access each ipaddress,username and password .But i am not being able to access the ipaddress,username and password.Can u please show me some ways to solve it.

How to List all Public_Ids in Cloudinary using Cloudinary API?

I have tried my best to make this code work but, Alas! Something is definitely wrong. I am tried to list all public_ids in Cloudinary. but it always prints, null. Below is the code -
import java.util.HashMap;
import java.util.Map;
import com.cloudinary.Api;
import com.cloudinary.Cloudinary;
import com.cloudinary.utils.ObjectUtils;
public class AllResources {
#SuppressWarnings("unchecked")
public static void main(String[] Args) {
Map<String, String> config = new HashMap<>();
config.put("cloud_name", "*******");
config.put("api_key", "**************");
config.put("api_secret", "***************************");
Cloudinary c = new Cloudinary(config);
Api api = c.api();
try {
Map<String, Object> result = api.resources(ObjectUtils.asMap());
System.out.println(result.get("public_id"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
You can do something like the following:
String nextCursor = null;
do {
result = api.resources(ObjectUtils.asMap("max_results", 500, "next_cursor", nextCursor));
nextCursor = result.get("next_cursor");
for (Map.Entry<String, Object> data : result.entrySet()) {
String key = data.getKey().toString();
Object value = data.getValue();
System.out.println(key + value);
}
} while (nextCursor != null);
Itay's code is pseudo code / won't compile. Here is a working version:
Cloudinary cloudinaryApi = new Cloudinary(
ObjectUtils.asMap(
"cloud_name", "YOUR CLOUD NAME",
"api_key", "YOUR API KEY",
"api_secret", "YOUR SECRET KEY"));
ApiResponse result = null;
String nextCursor = null;
do {
try {
result = cloudinaryApi.api().resources(ObjectUtils.asMap("max_results", 500, "next_cursor", nextCursor));
nextCursor = result.containsKey("next_cursor") ? result.get("next_cursor").toString() : null;
if(result.containsKey("resources")) {
List<Map<String,Object>> resources = (ArrayList<Map<String,Object>>) result.get("resources");
for (Map<String,Object> resource : resources) {
if(resource.containsKey("public_id")) {
String publicId = resource.get("public_id").toString();
System.out.println(publicId);
}
}
}
}
catch (Exception e) {
nextCursor = null;
LOG.error(e.getMessage());
}
} while (nextCursor != null);

How to read json file into java with simple JSON library

I want to read this JSON file with java using json simple library.
My JSON file looks like this:
[
{
"name":"John",
"city":"Berlin",
"cars":[
"audi",
"bmw"
],
"job":"Teacher"
},
{
"name":"Mark",
"city":"Oslo",
"cars":[
"VW",
"Toyata"
],
"job":"Doctor"
}
]
This is the java code I wrote to read this file:
package javaapplication1;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class JavaApplication1 {
public static void main(String[] args) {
JSONParser parser = new JSONParser();
try {
Object obj = parser.parse(new FileReader("c:\\file.json"));
JSONObject jsonObject = (JSONObject) obj;
String name = (String) jsonObject.get("name");
System.out.println(name);
String city = (String) jsonObject.get("city");
System.out.println(city);
String job = (String) jsonObject.get("job");
System.out.println(job);
// loop array
JSONArray cars = (JSONArray) jsonObject.get("cars");
Iterator<String> iterator = cars.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
}
But I get the following exception:
Exception in thread "main" java.lang.ClassCastException:
org.json.simple.JSONArray cannot be cast to org.json.simple.JSONObject
at javaapplication1.JavaApplication1.main(JavaApplication1.java:24)
Can somebody tell me what I am doing wrong? The whole file is a array and there are objects and another array (cars) in the whole array of the file. But i dont know how I can parse the whole array into a java array. I hope somebody can help me with a code line which I am missing in my code.
Thanks
The whole file is an array and there are objects and other arrays (e.g. cars) in the whole array of the file.
As you say, the outermost layer of your JSON blob is an array. Therefore, your parser will return a JSONArray. You can then get JSONObjects from the array ...
JSONArray a = (JSONArray) parser.parse(new FileReader("c:\\exer4-courses.json"));
for (Object o : a)
{
JSONObject person = (JSONObject) o;
String name = (String) person.get("name");
System.out.println(name);
String city = (String) person.get("city");
System.out.println(city);
String job = (String) person.get("job");
System.out.println(job);
JSONArray cars = (JSONArray) person.get("cars");
for (Object c : cars)
{
System.out.println(c+"");
}
}
For reference, see "Example 1" on the json-simple decoding example page.
You can use jackson library and simply use these 3 lines to convert your json file to Java Object.
ObjectMapper mapper = new ObjectMapper();
InputStream is = Test.class.getResourceAsStream("/test.json");
testObj = mapper.readValue(is, Test.class);
Add Jackson databind:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0.pr2</version>
</dependency>
Create DTO class with related fields and read JSON file:
ObjectMapper objectMapper = new ObjectMapper();
ExampleClass example = objectMapper.readValue(new File("example.json"), ExampleClass.class);
Reading from JsonFile
public static ArrayList<Employee> readFromJsonFile(String fileName){
ArrayList<Employee> result = new ArrayList<Employee>();
try{
String text = new String(Files.readAllBytes(Paths.get(fileName)), StandardCharsets.UTF_8);
JSONObject obj = new JSONObject(text);
JSONArray arr = obj.getJSONArray("employees");
for(int i = 0; i < arr.length(); i++){
String name = arr.getJSONObject(i).getString("name");
short salary = Short.parseShort(arr.getJSONObject(i).getString("salary"));
String position = arr.getJSONObject(i).getString("position");
byte years_in_company = Byte.parseByte(arr.getJSONObject(i).getString("years_in_company"));
if (position.compareToIgnoreCase("manager") == 0){
result.add(new Manager(name, salary, position, years_in_company));
}
else{
result.add(new OrdinaryEmployee(name, salary, position, years_in_company));
}
}
}
catch(Exception ex){
System.out.println(ex.toString());
}
return result;
}
Use google-simple library.
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
Please find the sample code below:
public static void main(String[] args) {
try {
JSONParser parser = new JSONParser();
//Use JSONObject for simple JSON and JSONArray for array of JSON.
JSONObject data = (JSONObject) parser.parse(
new FileReader("/resources/config.json"));//path to the JSON file.
String json = data.toJSONString();
} catch (IOException | ParseException e) {
e.printStackTrace();
}
}
Use JSONObject for simple JSON like {"id":"1","name":"ankur"} and JSONArray for array of JSON like [{"id":"1","name":"ankur"},{"id":"2","name":"mahajan"}].
Might be of help for someone else facing the same issue.You can load the file as string and then can convert the string to jsonobject to access the values.
import java.util.Scanner;
import org.json.JSONObject;
String myJson = new Scanner(new File(filename)).useDelimiter("\\Z").next();
JSONObject myJsonobject = new JSONObject(myJson);
Gson can be used here:
public Object getObjectFromJsonFile(String jsonData, Class classObject) {
Gson gson = new Gson();
JsonParser parser = new JsonParser();
JsonObject object = (JsonObject) parser.parse(jsonData);
return gson.fromJson(object, classObject);
}
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class Delete_01 {
public static void main(String[] args) throws FileNotFoundException,
IOException, ParseException {
JSONParser parser = new JSONParser();
JSONArray jsonArray = (JSONArray) parser.parse(new FileReader(
"delete_01.json"));
for (Object o : jsonArray) {
JSONObject person = (JSONObject) o;
String strName = (String) person.get("name");
System.out.println("Name::::" + strName);
String strCity = (String) person.get("city");
System.out.println("City::::" + strCity);
JSONArray arrays = (JSONArray) person.get("cars");
for (Object object : arrays) {
System.out.println("cars::::" + object);
}
String strJob = (String) person.get("job");
System.out.println("Job::::" + strJob);
System.out.println();
}
}
}
Following is the working solution to your problem statement as,
File file = new File("json-file.json");
JSONParser parser = new JSONParser();
Object obj = parser.parse(new FileReader(file));
JSONArray jsonArray = new JSONArray(obj.toString());
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
System.out.println(jsonObject.get("name"));
System.out.println(jsonObject.get("city"));
System.out.println(jsonObject.get("job"));
jsonObject.getJSONArray("cars").forEach(System.out::println);
}
Hope this example helps too
I have done java coding in a similar way for the below json array example as follows :
following is the json data format : stored as "EMPJSONDATA.json"
[{"EMPNO":275172,"EMP_NAME":"Rehan","DOB":"29-02-1992","DOJ":"10-06-2013","ROLE":"JAVA DEVELOPER"},
{"EMPNO":275173,"EMP_NAME":"G.K","DOB":"10-02-1992","DOJ":"11-07-2013","ROLE":"WINDOWS ADMINISTRATOR"},
{"EMPNO":275174,"EMP_NAME":"Abiram","DOB":"10-04-1992","DOJ":"12-08-2013","ROLE":"PROJECT ANALYST"}
{"EMPNO":275174,"EMP_NAME":"Mohamed Mushi","DOB":"10-04-1992","DOJ":"12-08-2013","ROLE":"PROJECT ANALYST"}]
public class Jsonminiproject {
public static void main(String[] args) {
JSONParser parser = new JSONParser();
try {
JSONArray a = (JSONArray) parser.parse(new FileReader("F:/JSON DATA/EMPJSONDATA.json"));
for (Object o : a)
{
JSONObject employee = (JSONObject) o;
Long no = (Long) employee.get("EMPNO");
System.out.println("Employee Number : " + no);
String st = (String) employee.get("EMP_NAME");
System.out.println("Employee Name : " + st);
String dob = (String) employee.get("DOB");
System.out.println("Employee DOB : " + dob);
String doj = (String) employee.get("DOJ");
System.out.println("Employee DOJ : " + doj);
String role = (String) employee.get("ROLE");
System.out.println("Employee Role : " + role);
System.out.println("\n");
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.json;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class ReadJSONFile {
public static void main(String[] args) {
JSONParser parser = new JSONParser();
try {
Object obj = parser.parse(new FileReader("C:/My Workspace/JSON Test/file.json"));
JSONArray array = (JSONArray) obj;
JSONObject jsonObject = (JSONObject) array.get(0);
String name = (String) jsonObject.get("name");
System.out.println(name);
String city = (String) jsonObject.get("city");
System.out.println(city);
String job = (String) jsonObject.get("job");
System.out.println(job);
// loop array
JSONArray cars = (JSONArray) jsonObject.get("cars");
Iterator<String> iterator = cars.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
}
This issue occurs when you are importing the org. json library for JSONObject class. Instead you need to import org.json.simple library.
private static final JsonParser JSON_PARSER = new JsonParser();
private static final String FILE_PATH = "configuration/data.json";
private JsonObject readJsonDataFromFile() {
try {
File indexFile = new File(FILE_PATH);
String fileData = Files.toString(indexFile, Charsets.UTF_8);
return (JsonObject) JSON_PARSER.parse(fileData);
} catch (IOException | JsonParseException e) {
String error = String.format("Error while reading file %s", FILE_PATH);
log.error(error);
throw new RuntimeException(error, e);
}
}
public class JsonParser {
public static JSONObject parse(String file) {
InputStream is = JsonParser.class.getClassLoader().getResourceAsStream(file);
assert is != null;
return new JSONObject(new JSONTokener(is));
}
}
// Read Json
JSONObject deviceObj = new JSONObject(JsonParser.parse("Your Json filename").getJSONObject(deviceID).toString());
Perform logic to iterate
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
public class JsonParserTest {
public static void main(String[] args) throws IOException {
String data = new String(Files.readAllBytes(Paths.get("C:/json.txt")));
JsonElement jsonElement = JsonParser.parseString(data);
JsonObject json = jsonElement.getAsJsonObject();
System.out.println(json.get("userId"));
System.out.println(json.get("id"));
System.out.println(json.get("title"));
System.out.println(json.get("completed"));
}
}
Use the below repositay from GSON.
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
Sample Json
{
"per_page": 3,
"total": 12,
"data": [{
"last_name": "Bluth",
"id": 1,
"avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/calebogden/128.jpg",
"first_name": "George"
},
{
"last_name": "Weaver",
"id": 2,
//"avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/josephstein/128.jpg",
"first_name": "Janet"
},
{
"last_name": "Wong",
"id": 3,
//"avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/olegpogodaev/128.jpg",
"first_name": "Emma"
}
],
"page": 1,
"total_pages": 4
}
First If statement will convert the single data from the body
Second if statement will differentiate the JsonArray object
public static String getvalueJpath(JSONObject responseJson, String Jpath ) {
Object obj = responseJson;
for(String s : Jpath.split("/"))
if (s.isEmpty())
if(!(s.contains("[") || s.contains("]")))
obj = ((JSONObject) obj).get(s);
else
if(s.contains("[") || s.contains("]"))
obj = ((JSONArray)((JSONObject)obj).get(s.split("\\[")[0])).get(Integer.parseInt(s.split("//[")[1].replaceAll("]", "")));
return obj.toString();
}
}
Solution using Jackson library. Sorted this problem by verifying the json on JSONLint.com and then using Jackson. Below is the code for the same.
Main Class:-
String jsonStr = "[{\r\n" + " \"name\": \"John\",\r\n" + " \"city\": \"Berlin\",\r\n"
+ " \"cars\": [\r\n" + " \"FIAT\",\r\n" + " \"Toyata\"\r\n"
+ " ],\r\n" + " \"job\": \"Teacher\"\r\n" + " },\r\n" + " {\r\n"
+ " \"name\": \"Mark\",\r\n" + " \"city\": \"Oslo\",\r\n" + " \"cars\": [\r\n"
+ " \"VW\",\r\n" + " \"Toyata\"\r\n" + " ],\r\n"
+ " \"job\": \"Doctor\"\r\n" + " }\r\n" + "]";
ObjectMapper mapper = new ObjectMapper();
MyPojo jsonObj[] = mapper.readValue(jsonStr, MyPojo[].class);
for (MyPojo itr : jsonObj) {
System.out.println("Val of getName is: " + itr.getName());
System.out.println("Val of getCity is: " + itr.getCity());
System.out.println("Val of getJob is: " + itr.getJob());
System.out.println("Val of getCars is: " + itr.getCars() + "\n");
}
POJO:
public class MyPojo {
private List<String> cars = new ArrayList<String>();
private String name;
private String job;
private String city;
public List<String> getCars() {
return cars;
}
public void setCars(List<String> cars) {
this.cars = cars;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
} }
RESULT:-
Val of getName is: John
Val of getCity is: Berlin
Val of getJob is: Teacher
Val of getCars is: [FIAT, Toyata]
Val of getName is: Mark
Val of getCity is: Oslo
Val of getJob is: Doctor
Val of getCars is: [VW, Toyata]
your json file look like this
import java.io.*;
import java.util.*;
import org.json.simple.*;
import org.json.simple.parser.*;
public class JSONReadFromTheFileTest {
public static void main(String[] args) {
JSONParser parser = new JSONParser();
try {
Object obj = parser.parse(new FileReader("/Users/User/Desktop/course.json"));
JSONObject jsonObject = (JSONObject)obj;
String name = (String)jsonObject.get("Name");
String course = (String)jsonObject.get("Course");
JSONArray subjects = (JSONArray)jsonObject.get("Subjects");
System.out.println("Name: " + name);
System.out.println("Course: " + course);
System.out.println("Subjects:");
Iterator iterator = subjects.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
the output is
Name: Raja
Course: MCA
Subjects:
subject1: MIS
subject2: DBMS
subject3: UML
took it from here
try {
Object obj = parser.parse(new FileReader("C:/Local Disk/file.json"));
// JSONArray array = (JSONArray) obj;
JSONObject jsonObject = (JSONObject) obj;
JSONObject orchestration = (JSONObject) jsonObject.get("orchestration");
JSONObject trigger = (JSONObject) orchestration.get("trigger-definition");
JSONObject schedule = (JSONObject) trigger.get("schedule");
JSONObject trade = (JSONObject) schedule.get("trade-query");
// loop array
JSONArray filter = (JSONArray) trade.get("filter");
for (Object o : filter) {
JSONObject person = (JSONObject) o;
String strName = (String) person.get("name");
System.out.println("Name::::" + strName);
String operand = (String) person.get("operand");
System.out.println("City::::" + operand);
String value = (String) person.get("value");
System.out.println("value::::" + value);
}
JSONArray parameter = (JSONArray) trade.get("parameter");
for (Object o : parameter) {
JSONObject person = (JSONObject) o;
String strName = (String) person.get("name");
System.out.println("Name::::" + strName);
String value = (String) person.get("value");
System.out.println("value::::" + value);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
try {
//Object obj = parser.parse(new FileReader("C:/Local Disk/file.json"));
// create object mapper instance
ObjectMapper mapper = new ObjectMapper();
// convert JSON string to Book object
Object obj = mapper.readValue(Paths.get("C:/Local Disk/file.json").toFile(), Object.class);
// print book
System.out.println(obj);
String jsonInString = new Gson().toJson(obj);
JSONObject mJSONObject = new JSONObject(jsonInString);
System.out.println("value::::" + mJSONObject);
JSONObject orchestration = (JSONObject) mJSONObject.get("orchestration");
JSONObject trigger = (JSONObject) orchestration.get("trigger-definition");
JSONObject schedule = (JSONObject) trigger.get("schedule");
JSONObject trade = (JSONObject) schedule.get("trade-query");
// loop array
JSONArray filter = (JSONArray) trade.get("filter");
for (Object o : filter) {
JSONObject person = (JSONObject) o;
String strName = (String) person.get("name");
System.out.println("Name::::" + strName);
String operand = (String) person.get("operand");
System.out.println("City::::" + operand);
String value = (String) person.get("value");
System.out.println("value::::" + value);
}
JSONArray parameter = (JSONArray) trade.get("parameter");
for (Object o : parameter) {
JSONObject person = (JSONObject) o;
String strName = (String) person.get("name");
System.out.println("Name::::" + strName);
String value = (String) person.get("value");
System.out.println("value::::" + value);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
You can use readAllBytes.
return String(Files.readAllBytes(Paths.get(filePath)),StandardCharsets.UTF_8);

Categories

Resources