Java - How to get all the field names from JSON Schema - java

{
"type": "TestType",
"schema": {
"type": "object",
"properties": {
"field1": {
"format": "date",
"label": "field 1",
"type": "string"
},
"field2": {
"format": "date",
"label": "field 2",
"type": "string"
}
},
"required": [],
"additionalProperties": false
}
}
Hi Guys, I am new to JSON Schema, I just want to know how to define the Java model to mapping above JSON Schema, I want to get all the field name from the definition.
So it means that I want to parse the JSON Schema and return like this:
[field1, field2]
Note that the field list is automatic, may add more fields into the JSON Schema, field3, field4 ...

1.convert to JSONObject
2.save in List
3.convert List to Array
try this .
public void getKey(String response) {
List<String> keyList = new ArrayList<String>();
try {
JSONObject jsonObject = new JSONObject(response);
JSONObject schema = jsonObject.getJSONObject("schema");
JSONObject properties = schema.getJSONObject("properties");
Iterator iterator = properties.keys();
while (iterator.hasNext()) {
String key = iterator.next().toString();
keyList.add(key);
}
String[] arr = (String[]) keyList.toArray(new String[keyList.size()]);
} catch (JSONException e) {
e.printStackTrace();
}
}

I assume your json is in a String variable named var.
use org.json for the below code.
JSONObject jObj=new JSONObject(var);
ArrayList arr=new ArrayList();
for (Object keyObj: jObj.keySet())
{
String key = (String)keyObject;
arr.add(key);
}
That's it.
I have added maven dependency for org.json package for those who are working with maven!
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20160810</version>
</dependency>

Recursive solution that will work with nested objects
public Set<String> getMetadataSchemaFields(JsonNode metadataSchema) {
if (metadataSchema.has("properties")) {
val fields = new HashSet<String>();
metadataSchema.get("properties").fields().forEachRemaining(entry -> {
val nodeFields = getMetadataSchemaFields(entry.getValue());
if (nodeFields.isEmpty()) {
fields.add(entry.getKey());
} else {
nodeFields.forEach(nodeField -> fields.add(entry.getKey() + "/" + nodeField));
}
});
return fields;
} else {
return Set.of();
}
}

Related

Insert complex nested JSON into MongoDB using Java

I have a complex and nested JSON File which looks like this
{
"databases": {
"component": "pages/dems/transparency/workspaces/DatasourcesWorkspace.vue",
"children": [
{
"name": "Databases",
"path": "databases",
"component": "pages/dems/transparency/workspaces/Databases.vue",
"hide": true,
"help": "context-sensitive-help/transparency/workspaces/datasources/databases/databases/databases.html"
},
{
"name": "Schemas",
"path": "schemas",
"component": "containers/EmptyContainer.vue",
"hide": true,
"children": [
{
"name": "",
"component": "pages/dems/transparency/workspaces/Schemas.vue",
"hide": true,
"help": "context-sensitive-help/transparency/workspaces/datasources/databases/schemas.html"
},
{
"name": "",
"path": "relationship/:id",
"component": "pages/dems/transparency/workspaces/profilecolumn/ViewRelationShip.vue",
"hide": true,
"help": "context-sensitive-help/transparency/workspaces/relationship/relationship.html"
}
]
}
]
}
}
I want to insert it into MongoDB. The structure would be like this:
{
"_id":"id1",
"field":"databases",
"value":{
"component": "pages/dems/transparency/workspaces/DatasourcesWorkspace.vue",
"children": [
{
"name": "Databases",
"path": "databases",
"component": "pages/dems/transparency/workspaces/Databases.vue",
"hide": true,
"help": "context-sensitive-help/transparency/workspaces/datasources/databases/databases/databases.html"
},
{
"children": [
{
"name": "",
"component": "pages/dems/transparency/workspaces/Schemas.vue",
"hide": true,
"help": "context-sensitive-help/transparency/workspaces/datasources/databases/schemas.html"
},
{
"name": "",
"path": "relationship/:id",
"component": "pages/dems/transparency/workspaces/profilecolumn/ViewRelationShip.vue",
"hide": true,
"help": "context-sensitive-help/transparency/workspaces/relationship/relationship.html"
}
]
}
]
}
}
I need to also check if any object has the containers/EmptyContainer.vue inside the component value then do not insert that object inside MongoDB.
I have tried many methods but didn't work.
Code tried, which is not working as expected:
public ArrayList<Document> processJsonV4(Object source) throws JSONException {
ArrayList<Document> rootDoc = new ArrayList<>();
if (source instanceof JSONObject) {
JSONObject jsonObject = (JSONObject) source;
Document document = new Document();
if (jsonObject.has("component") && !"containers/EmptyContainer.vue".equals(jsonObject.getString("component"))) {
for (Iterator keys = jsonObject.keys(); keys.hasNext(); ) {
String key = (String) keys.next();
Object value = jsonObject.get(key);
document.append(key, value);
}
} else {
for (Iterator keys = jsonObject.keys(); keys.hasNext(); ) {
String key = (String) keys.next();
Object value = jsonObject.get(key);
processJsonV4(value);
}
}
rootDoc.add(document);
}
KLogger.info("RootDoc: " + rootDoc);
return rootDoc;
}
How can I do this using Java?
A simple recursive way could be to construct and add to the list of Documents when you invoke them:
public void readAndConstructMongoDoc() {
JSONObject jsonData = readFileData("<your-file.json>");
List<Document> rootDoc = new ArrayList<>();
constructRootDoc(rootDoc, jsonData);
// Save in mongo -> rootDoc
}
private void constructRootDoc(List<Document> rootDoc, Object source) {
if (source instanceof JSONObject) {
JSONObject jsonObject = new JSONObject();
if (jsonObject.has("component") && !"containers/EmptyContainer.vue".equals(jsonObject.getString("component"))) {
rootDoc.add(convertToMongoDocument(jsonObject));
JSONArray children = jsonObject.getJSONArray("children");
int length = children.length();
for (int i = 0; i < length; i++) {
// Recursively call for all child node and pass the rootDoc
testJson(rootDoc, children.getJSONObject(i));
}
}
}
}
private Document convertToMongoDocument(JSONObject jsonObject) {
Document document = new Document();
document.append("name", jsonObject.getString("name"));
document.append("path", jsonObject.getString("path"));
document.append("hide", Boolean.valueOf(jsonObject.getString("hide")));
document.append("component", jsonObject.getString("component"));
return document;
}
Side Note: I believe your Mongo Document schema can be improvised and doesn't have to be storing the data in this format.

how to remove json object from json Array using org.json.simple

I have a JSON file which is an JSON ARRAY with some JSON Objects and I want to remove the entire json Object if the value of longitude or latitude is an empty string "".
I am using the library org.json.simple. Here is my json file:
[ { "Longitude": 9.96233,
"Latitude": 49.80404 },
{
"Longitude": 6.11499,
"Latitude": 50.76891
},
{ "Longitude": 6.80592,
"Latitude": 51.53548
},
{
"Longitude": 9.50523,
"Latitude": 51.31991 },
{
"Longitude": ""
"Latitude": ""
},
{
"Longitude": 9.93368,
"Latitude": ""
},
{
"Longitude": 11.56122,
"Latitude": 48.14496
},
{
"Longitude": 13.34253,
"Latitude": 52.5319
},
{
"Longitude": 6.11327,
"Latitude": 50.77715
},
{
"Longitude": ""
"Latitude": ""
}
]
and here's where I am stuck. :
JSONParser jsonParser = new JSONParser();
try (FileReader reader = new FileReader ("output.json")) {
Object obj = jsonParser.parse(reader);
JSONArray list = (JSONArray) obj;
list.forEach(node -> {
String vari = (String)((JSONObject)node).get("longitude").toString();
if (vari==null) {
((JSONObject) node).remove();
System.out.println("deleted");
}
}
...
Any suggestions how can I change my code ?
You need to remove the node from the list but because of concurrent modification problem (modifying the list you are looping) you need another list.
I think it is easiest to collect into the new list all the nodes that qualify so like:
#SuppressWarnings("unchecked")
#Test
public void test() throws Exception {
JSONParser jsonParser = new JSONParser();
JSONArray listNonNull = new JSONArray();
try (FileReader reader = new FileReader("output.json")) {
JSONArray list = (JSONArray) jsonParser.parse(reader);
((Collection<JSONObject>)list).forEach(node -> {
// here any check that qualifies the node like also checking "Latitude"
// Also no need to cast to a String
Object vari = node.get("Longitude");
if (vari != null && !vari.equals("")) {
listNonNull.add(node);
}
});
} catch (Exception e) {
throw e;
}
}
If you wish to use only the original list you can collect items to be removed to an another list and use it to remove nodes from the original list:
public void test2() throws Exception {
JSONParser jsonParser = new JSONParser();
JSONArray toBeRemoved = new JSONArray();
try (FileReader reader = new FileReader("output.json")) {
JSONArray list = (JSONArray) jsonParser.parse(reader);
((Collection<JSONObject>) list).forEach(node -> {
Object vari = node.get("Longitude");
// here any check that qualifies the node like also checking "Latitude"
if (vari != null && !vari.equals("")) {
return; // not to be removed
}
toBeRemoved.add(node);
});
list.removeAll(toBeRemoved);
} catch (Exception e) {
throw e;
}
}
use .remove instead of .clear in the code.
JSONParser jsonParser = new JSONParser();
try (FileReader reader = new FileReader("output.json")) {
Object obj = jsonParser.parse(reader);
JSONArray list = (JSONArray) obj;
ArrayList<JSONObject> objList = (ArrayList<JSONObject>) list.stream().filter(node -> {
String longitude = ((JSONObject) node).get("Longitude").toString();
String latitude = ((JSONObject) node).get("Latitude").toString();
if (longitude.isEmpty() || latitude.isEmpty()) {
return false;
}
return true;
}).collect(Collectors.toList());
} catch (IOException | ParseException e) {
e.printStackTrace();
}

Gson append new object array to existing JSON file

i need some help appending new arrays into a existing file. I have a JSON file like this:
[
{
"name": "any",
"address": {
"street": "xxxx",
"number": 1
},
"email": "teste#gmail.com"
}
]
I want to insert new array, so my file will be like this:
[
{
"name": "any",
"address": {
"street": "xxxx",
"number": 1
},
"email": "test#gmail.com"
},
{
"name": "any2",
"address": {
"street": "yyyyy",
"number": 2
},
"email": "test2#gmail.com"
}
]
Here's my code:
Gson gson = new GsonBuilder().setPrettyPrinting().create();
ArrayList<Person> ps = new ArrayList<Person>();
// .... reading entries...
ps.add(new Person(name, address, email));
String JsonPerson = gson.toJson(ps);
File f = new File("jsonfile");
if (f.exists() && !f.isDirectory()) {
JsonReader jsonfile = new JsonReader(new FileReader("jsonfile"));
JsonParser parser = new JsonParser();
JsonElement element = parser.parse(jsonfile);
//here goes the new entry?
try (FileWriter file = new FileWriter("pessoas.json")) {
file.write(JsonPessoa);
file.close();
} catch (Exception e) {
e.printStackTrace();
}
So, what's the best way to do this?
Thanks in advance.
Gson really shines when combined with Pojos, so my suggestion would be use of mapped pojos. Consider below two classes.
public class Contact {
#SerializedName("address")
private Address mAddress;
#SerializedName("email")
private String mEmail;
#SerializedName("name")
private String mName;
// getters and setters...
}
public class Address {
#SerializedName("number")
private Long mNumber;
#SerializedName("street")
private String mStreet;
// getters and setters...
}
Read JSON and add new contact and convert it back to JSON, It also works for other way around seamlessly. Similarly you can use this approach for solve many use cases. Pass json array string by reading from file or using similar way, after
Gson gson = new Gson();
List<Contact> contacts = gson.fromJson("JSON STRING", new TypeToken<List<Contact>>() {}.getType());
Contact newContact = new Contact();
// set properties
contacts.add(newContact);
String json = gson.toJson(contacts);
There are tools like this one to create pojos from JSON.

Parse nested Json object and store in database in Android

I just tried to get values that are stored in my JSON file and save it into sqlite database:
This is my JSON file:
{
"list": {
"meta": {
"count": 132,
"start": 0,
"type": "resource-list"
},
"resources": [
{
"resource": {
"classname": "Quote",
"fields": {
"date": "2017-03-16",
"price": 3.6720000000000002,
"type": "currency",
"symbol": "AED=X"
}
}
},
{
"resource": {
"classname": "Quote",
"fields": {
"date": "2017-03-16",
"price": 65.075000000000003,
"type": "currency",
"symbol": "AFN=X"
}
}
},
{
.............
}
............
I have tried like this but getting exception :
JSONObject mainObj = null;
try {
mainObj = new JSONObject(JSON);
JSONObject getSth = mainObj.getJSONObject("list");
if(mainObj != null){
JSONArray list = getSth.getJSONArray("resources");
if(list != null){
for(int i = 0; i < list.length();i++){
JSONObject elem = list.getJSONObject(i);
if(elem != null){
JSONObject prods = elem.getJSONObject("fields");
Object level = prods.get("type");
Toast.makeText(getApplicationContext(),""+level.toString(),Toast.LENGTH_LONG).show();
}
}
}
}
}catch (Exception e){
Toast.makeText(getApplicationContext(),""+e.toString(),Toast.LENGTH_LONG).show();
}
I was getting exception : no values in fields...
And pls give some suggestions that storing these values in Database table(matrotable) of(row fields) name, prize, symbol and type, I may try by making String Array and retrieving and storing the values for sqlite, is there any other easy options...
thanks
your fields objects are inside resource object so do
for(int i = 0; i < list.length();i++){
JSONObject elem = list.getJSONObject(i);
if(elem != null){
JSONObject prods = elem.getJSONObject("resource")
.getJSONObject("fields");
Object level = prods.get("type");
Toast.makeText(getApplicationContext(),""+level.toString(),Toast.LENGTH_LONG).show();
}
}
"resources": [ // resources list
{ // object i
"resource": { // fields are inside "resource" object
"classname": "Quote",
"fields": {
"date": "2017-03-16",
"price": 3.6720000000000002,
"type": "currency",
"symbol": "AED=X"
}
}
}
You are missing the resource JOSNObject parsing...
for(int i = 0; i < list.length();i++){
JSONObject elem = list.getJSONObject(i);
JSONObject resource = elem.getJSONObject("resource");
if(resource != null){
JSONObject prods = resource.getJSONObject("fields");
Object level = prods.get("type");
Toast.makeText(getApplicationContext(),""+level.toString(),Toast.LENGTH_LONG).show();
}
}
I recommend to you to use the simplest and easiest way to parse a json response to avoid this kind of issues:
1- generate your model classes by using this tool: http://www.jsonschema2pojo.org/ download and add the generated classes to your model package.
2- add this dependency to your gradle file:
compile 'com.google.code.gson:gson:2.4'
3- Call this method to parse your response:
Gson gson = new Gson();
ResponseModel responseModel = gson.fromJson(json, ResponseModel.class);

JAVA org.json not showing empty value

Here is my JAVA JSON code
#GET
#Consumes("application/x-www-form-urlencoded")
#Path("/getAllEmp")
public Response GetAllEmp() {
JSONObject returnJson = new JSONObject();
try {
ArrayList<Emp_Objects> objList = new ArrayList<Emp_Objects>();
DBConnection conn = new DBConnection();
objList = conn.GetEmpDetails();
JSONArray empArray = new JSONArray();
if (!objList.isEmpty()) {
GetLocation loc = new GetLocation();
for (Emp_Objects obj : objList) {
JSONObject jsonObj = new JSONObject();
jsonObj.put("id", obj.id);
jsonObj.put("name", obj.name);
jsonObj.put("email", obj.email);
jsonObj.put("address", obj.address);
empArray.put(jsonObj);
}
}
returnJson.put("data", empArray);
} catch (Exception e) {
}
return Response.ok(returnJson.toString()).header("Access-Control-Allow-Origin", "*").build();
}
When i execute this it gives me the following json
{
"data": [{
"id": 1,
"name": "123_name"
}, {
"id": 2,
"name": "321_name",
"email": "xyz#asd.com"
}]
}
In the above json email and address are missing because email and address is null on database.
So can i show json with empty value like following
{
"data": [{
"id": 1,
"name": "123_name",
"email": "",
"address": ""
}, {
"id": 2,
"name": "321_name",
"email": "",
"address": ""
}]
}
I am using JAVA and org.json with MySQL database.
If the objects are null, insert an empty String instead.
jsonObj.put("email", obj.email == null ? "" : obj.email);
jsonObj.put("address", obj.address == null ? "" : obj.address);
If you have a larger amount of rows to process, I recommend you to turn this is to a function for better readability and to save you some time.
jsonObj.put("email", nullToEmpty(obj.address));
private String nullToEmpty(String arg) {
return arg == null ? "" : arg;
}

Categories

Resources