Evaluate JSON object with expression with variable name, Josson - java

We need to evaluate JSON object expressions in java.
We have the following source JSON object
{
"a": 100,
"b": 200,
"c": 300,
"d": "calc(a+c)",
"f": {
"g": 100,
"h": 200,
"i": "calc(g+h)"
}
}
we need output this format
{
"a": 100,
"b": 200,
"c": 300,
"d": 400,
"f": {
"g": 100,
"h": 200,
"i": 300
}
}
We tried
we tried https://github.com/octomix/josson but that is more of a filtering JSON.

Hope this helps. All the necessary information is mentioned inside the code itself.
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.json.JSONObject;
public class Main {
public static void main(String[] args) {
String inputString = "{'a': 100, 'b': 200, 'c': 300, 'd': 'calc(a+c)', 'f': {'g': 100, 'h': 200, 'i': 'calc(g+h)'} }";
JSONObject newJSON = parseJSONValue(inputString, new JSONObject());
System.out.println(newJSON.toString());
// {"a":100,"b":200,"c":300,"d":400,"f":{"g":100,"h":200,"i":300}}
}
public static JSONObject parseJSONValue(String inputString, JSONObject resultJSON) {
// Parse your string as JSONObject
JSONObject jsonObject = new JSONObject(inputString);
Iterator<String> keys = jsonObject.keys();
// Iterate through your keys
while (keys.hasNext()) {
String key = keys.next();
Object value = jsonObject.get(key);
if (value instanceof Integer) {
// Your normal values
} else if (value instanceof String) {
// Your 'calc(x+y)' strings
// Extract everything between "(" and ")" from calc(a+c)
Pattern pattern = Pattern.compile("\\((.*?)\\)");
Matcher m = pattern.matcher(value.toString());
while (m.find()) {
// a+c
String evalString = m.group(1);
// Split by '+'
String[] splitEvalString = evalString.split("\\+");
// Check if exactly 2 values are found
if (splitEvalString.length == 2) {
value = (Integer) jsonObject.get(splitEvalString[0])
+ (Integer) jsonObject.get(splitEvalString[1]);
}
}
} else if (value instanceof JSONObject) {
// Your nested JSONObjects
// Recursively call this method
value = parseJSONValue(value.toString(), new JSONObject());
}
// Add to your new JSON Object
resultJSON.put(key, value);
}
return resultJSON;
}
}

Josson josson = Josson.fromJsonString(jsonString);
JsonNode node = josson.getNode("field(d:eval(d), f.field(i:eval(i)))");
System.out.println(node.toPrettyString());
Output
{
"a" : 100,
"b" : 200,
"c" : 300,
"d" : 400.0,
"f" : {
"g" : 100,
"h" : 200,
"i" : 300.0
}
}
For unknown structure
This solution assumes
the evaluation statement contains ()
there is only one evaluation statement in each level
you need to know the next level child name (f in this example)
Transformation
JsonNode node = josson.getNode(
"let($p: '.+\\(.*\\).*')" +
".field(entries().[value =~ $p].key :: eval(*[value =~ $p])," +
" f.field(entries().[value =~ $p].key :: eval(*[value =~ $p]))" +
")");
Suggest to group all the expressions in a pre-defined array
Josson josson = Josson.fromJsonString(
"{" +
" \"a\": 100," +
" \"b\": 200," +
" \"c\": 300," +
" \"f\": {" +
" \"g\": 100," +
" \"h\": 200" +
" }," +
" \"exp\": [\"$.calc(a+c)\"," +
" \"$.calc(g+h, g:f.g, h:f.h)\"," +
" \"$.concat(a, '+', c, '=', calc(a+c))\"," +
" \"$.concat(f.g, '+', f.h, '=', calc(g+h, g:f.g, h:f.h))\"" +
" ]" +
"}");
JsonNode node = josson.getNode("field(exp#.eval())");
System.out.println(node.toPrettyString());
Output
{
"a" : 100,
"b" : 200,
"c" : 300,
"f" : {
"g" : 100,
"h" : 200
},
"exp" : [ 400.0, 300.0, "100+300=400.0", "100+200=300.0" ]
}

Related

compare 2 JSON objects having nested fields in Java

I have 2 JSON objects
First
{
"fields": [
{
"name": "address",
"type": "string",
"default": "NONE"
},
{
"name": "age",
"type": "int",
"default": "NONE"
}
]
}
and second JSON
{
"fields": [
{
"name": "age",
"type": "int",
"default": "NONE"
},
{
"name": "address",
"type": "string",
"default": "NONE"
}
]
}
Please note that
fields object may not be always named "name","type" and "default"
the field values may not be always primitives. For example an
address which is of type "ArrayList".
How do I compare the JSON objects in such cases since these are equivalent JSON objects except for field value ordering. I am using Java 11 for my work. I tried using a custom Field class and map individual fields element to the class and then sort it but then the solution is not generic.
Following is one solution I am trying
public class JsonComparator {
public static boolean compareJson(String json1, String json2) {
JSONObject obj1 = new JSONObject(json1);
JSONObject obj2 = new JSONObject(json2);
JSONArray fields1 = obj1.getJSONArray("fields");
JSONArray fields2 = obj2.getJSONArray("fields");
fields1 = sortJsonArray(fields1);
fields2 = sortJsonArray(fields2);
for (int i = 0; i < fields1.length(); i++) {
JSONObject field1 = fields1.getJSONObject(i);
JSONObject field2 = fields2.getJSONObject(i);
for (String key : field1.keySet()) {
if (!field1.get(key).equals(field2.get(key))) {
return false;
}
}
}
return true;
}
private static JSONArray sortJsonArray(JSONArray array) {
ArrayList<JSONObject> sortedList = new ArrayList<>();
for (int i = 0; i < array.length(); i++) {
sortedList.add(array.getJSONObject(i));
}
sortedList.sort(new Comparator<JSONObject>() {
#Override
public int compare(JSONObject o1, JSONObject o2) {
return o1.toString().compareTo(o2.toString());
}
});
return new JSONArray(sortedList);
}
}
but need to analyze the JSON objects since all the JSON object need not contain the field "fields" and one JSON can be superset of other so trying to see if a better library available to give a solution.
You could try JSONCompare from JSONAssert library
<dependency>
<groupId>org.skyscreamer</groupId>
<artifactId>jsonassert</artifactId>
<version>1.5.0</version>
</dependency>
with compareMode as STRICT,LENIENT,IGNORE_ORDER etc.
public static boolean isTwoJsonEquals(JSONObject firstObj,JSONObject secondObj, JSONCompareMode compareMode){
try {
JSONCompareResult result = JSONCompare.compareJSON(firstObj, secondObj, compareMode);
return result.passed();
} catch (JSONException e) {
e.printStackTrace();
}
return false;
}
You may try library Josson & Jossons. It has JSON join, set and compare operation.
https://github.com/octomix/josson
Deserialization
Jossons jossons = new Jossons();
jossons.putDataset("json1", Josson.fromJsonString(
"{" +
" \"fields\": [" +
" {" +
" \"name\": \"address\"," +
" \"type\": \"string\"," +
" \"default\": \"NONE\"" +
" }," +
" {" +
" \"name\": \"age\"," +
" \"type\": \"int\"," +
" \"default\": \"NONE\"" +
" }" +
" ]" +
"}"));
jossons.putDataset("json2", Josson.fromJsonString(
"{" +
" \"fields\": [" +
" {" +
" \"name\": \"age\"," +
" \"type\": \"int\"," +
" \"default\": \"NONE\"" +
" }," +
" {" +
" \"name\": \"gender\"," +
" \"type\": \"string\"," +
" \"default\": \"NONE\"" +
" }" +
" ]" +
"}"));
Subtract right from left
JsonNode node = jossons.evaluateQuery("json1->fields <-< json2->fields");
System.out.println(node.toPrettyString());
// Output:
// [ {
// "name" : "address",
// "type" : "string",
// "default" : "NONE"
// } ]
Subtract left from right
JsonNode node = jossons.evaluateQuery("json1->fields >-> json2->fields");
System.out.println(node.toPrettyString());
// Output:
// [ {
// "name" : "gender",
// "type" : "string",
// "default" : "NONE"
// } ]
Symmetric difference
JsonNode node = jossons.evaluateQuery("json1->fields <-> json2->fields");
System.out.println(node.toPrettyString());
// Output:
// [ {
// "name" : "address",
// "type" : "string",
// "default" : "NONE"
// }, {
// "name" : "gender",
// "type" : "string",
// "default" : "NONE"
// } ]
Equals
JsonNode node = jossons.evaluateQuery("json1->fields <==> json2->fields");
System.out.println(node.toPrettyString());
// Output:
// false
Not equals
JsonNode node = jossons.evaluateQuery("json1->fields <!=> json2->fields");
System.out.println(node.toPrettyString());
// Output:
// true

Json manipulation and conversion in Java

I have an input json like:
{
"Employee": [
{
"employee1.id": 1
},
{
"employee1.name": "John"
},
{
"employee1.address.street": "main street"
},
{
"employee1.address.pin": "123456"
},
{
"employee2.id": 2
},
{
"employee2.name": "Mike"
},
{
"employee2.address.street": "51 street"
},
{
"employee2.address.pin": "234543"
}
]
}
And I am trying to convert it into:
{
"employee1":{
"id": 1,
"name": "John",
"address": {
"street": "main street",
"pin": "123456"
}
},
"employee2":{
"id": 2,
"name": "Mike",
"address": {
"street": "51 street",
"pin": "234543"
}
}
}
I tried to split the key from input json with dot i.e. '.' and tried to iterate over to construct a map: Map<String, Object>
But the problem is the input json key depth can go beyond 2 i.e. in future the input key can be like:
{
"employee1.address.tempAddress.street": "main street"
},
{
"employee1.address.permanentAddress.street": "main street"
}
So, is there any library available to achieve this, or anything closely related to this using which I can achieve this?
The comments about the input format being "weird" (to put it politely) are spot-on: This is a convoluted way to represent simple hierarchical data.
But sometimes we have to deal with sub-optimal input formats, that's why we can fix them like this:
private static JSONObject arrayToStructure(JSONArray inputArray) {
JSONObject output = new JSONObject();
for (Object o : inputArray) {
assert o instanceof JSONObject;
JSONObject jso = (JSONObject) o;
assert jso.length() == 1;
String key = jso.keys().next();
Object value = jso.get(key);
setJsonPath(output, key, value);
}
return output;
}
private static void setJsonPath(JSONObject target, String key, Object value) {
String[] keyParts = key.split("\\.");
JSONObject parent = target;
for (int i = 0; i < keyParts.length - 1; i++) {
String keyPart = keyParts[i];
if (parent.has(keyPart)) {
parent = parent.getJSONObject(keyPart);
} else {
JSONObject child = new JSONObject();
parent.put(keyPart, child);
parent = child;
}
}
assert !parent.has(keyParts[keyParts.length - 1]);
parent.put(keyParts[keyParts.length - 1], value);
}
This code uses JSON-Java, probably better known as org.json:json.
And it can be used like this:
public static void main(String[] args) {
String input = """
[
{
"employee1.id": 1
},
{
"employee1.name": "John"
},
{
"employee1.address.street": "main street"
},
{
"employee1.address.pin": "123456"
},
{
"employee2.id": 2
},
{
"employee2.name": "Mike"
},
{
"employee2.address.street": "51 street"
},
{
"employee2.address.pin": "234543"
}
]""";
JSONArray inputArray = new JSONArray(input);
JSONObject structure = arrayToStructure(inputArray);
System.out.println(structure.toString(2));
}
Note that this code is lacking proper sanity checks (some of which I've hinted at using assert) and the nature of this "protocol" means that you can get misleading, ambiguous or straight up malicious inputs. For example nothing stops the input from having both "employee1.id": 1 and "employee1.id": 2 in there. How that is to be interpreted is up to the parser and such ambiguity is a great source of bugs and potential security issues.
Library Josson can do the transformation by one expression.
https://github.com/octomix/josson
Josson josson = Josson.fromJsonString(
"{" +
" \"Employee\": [" +
" {" +
" \"employee1.id\": 1" +
" }," +
" {" +
" \"employee1.name\": \"John\"" +
" }," +
" {" +
" \"employee1.address.street\": \"main street\"" +
" }," +
" {" +
" \"employee1.address.pin\": \"123456\"" +
" }," +
" {" +
" \"employee2.id\": 2" +
" }," +
" {" +
" \"employee2.name\": \"Mike\"" +
" }," +
" {" +
" \"employee2.address.street\": \"51 street\"" +
" }," +
" {" +
" \"employee2.address.pin\": \"234543\"" +
" }" +
" ]" +
"}");
JsonNode node = josson.getNode("Employee.mergeObjects().unflatten('.')");
System.out.println(node.toPrettyString());
Output
{
"employee1" : {
"id" : 1,
"name" : "John",
"address" : {
"street" : "main street",
"pin" : "123456"
}
},
"employee2" : {
"id" : 2,
"name" : "Mike",
"address" : {
"street" : "51 street",
"pin" : "234543"
}
}
}

Iterate through a Json object and store some values in an array

I retrieved the following JSON-object. These json array consists of posts. As can be seen from the below, each post has a userId, id, title and a body. Now I need to iterate through this json and get the id of the posts IF the userId is 5 (in this case all four id's will be stored because in all of them the userId is 5
[
{
"userId": 5,
"id": 21,
"title": "A title",
"body": "A body"
},
{
"userId": 5,
"id": 52,
"title": "B title",
"body": "B body"
},
{
"userId": 5,
"id": 13,
"title": "C title",
"body": "C body"
},
{
"userId": 5,
"id": 44,
"title": "D title",
"body": "D body"
},
]
and then I need to store the id values in an array like this:
postIds = [21, 52, 13, 44]
So far I have done this
GetPosts[] getPosts = getPostResponse.as(GetPosts[].class);
String[] userPostIds;
for (int i = 0; i < getPosts.length; i++) {
if(getPosts[0].getId().equals(userId)) {
}
}
Then I tried to write a for loop like this but I know it is wrong. Maybe the statement in if parentheses is true, but for loop needs to be something else and I need to write something inside the if statement but I cannot progress here.
Edit: I now modified the for/if loop like this:
for (int i = 0; i < getPosts.length; i++) {
Integer userPostIds = null;
if (getPosts[i].getId().equals(userId)) {
userPostIds = getPosts.getId();
}
System.out.println(userPostIds);
}
But this time userPostIds is printed like this:
null
null
null
null
whereas I was expecting it to be:
21
52
13
44
Let's assume that you have stored your JSON as a String. You can use Jackson object mapper to map JSON to Object.
private static String JSON = "[\n" +
" {\n" +
" \"userId\": 5,\n" +
" \"id\": 21,\n" +
" \"title\": \"A title\",\n" +
" \"body\": \"A body\"\n" +
" },\n" +
" {\n" +
" \"userId\": 5,\n" +
" \"id\": 52,\n" +
" \"title\": \"B title\",\n" +
" \"body\": \"B body\"\n" +
" },\n" +
" {\n" +
" \"userId\": 5,\n" +
" \"id\": 13,\n" +
" \"title\": \"C title\",\n" +
" \"body\": \"C body\"\n" +
" },\n" +
" {\n" +
" \"userId\": 5,\n" +
" \"id\": 44,\n" +
" \"title\": \"D title\",\n" +
" \"body\": \"D body\"\n" +
" }\n" +
"]";
You need to create object for mapping:
public class Foo {
private Integer userId;
private Integer id;
private String title;
private String body;
// getters
// setters
}
Then you can map your JSON to list of objects:
private static List<Foo> mapJsonToObject() {
var om = new ObjectMapper();
List<Foo> list = null;
try {
list = Arrays.asList(om.readValue(JSON, Foo[].class));
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return list;
}
In the end you need to iterate through this list to get all ids.
public static void main(String[] args) {
var list = mapJsonToObject();
var ids = list.stream().map(Foo::getId).collect(Collectors.toList());
System.out.println(ids.toString());
}
Thankfully, the hard part (deserialization) is complete per the OP.
Therefore, to iterate over the object list, I recommend something similar to the following, presuming that the post.getId returns a String (Otherwise, you may need to do a cast conversion)
Log.info("Store all the posts of the user");
GetPosts[] getPosts = getPostResponse.as(GetPosts[].class);
ArrayList<Integer> userIdsFromPosts = new ArrayList<Integer>();
if(getPosts != null) { // Avoiding a Possible NPE in list iteration...Just in case ;)
for (GetPosts post : getPosts) {
Integer currentUserId = post.getId();
if (!userIdsFromPosts.contains(currentUserId)) {
userIdsFromPosts.add(currentUserId);
}
}
}
return userIdsFromPosts;
One final note, if you are expecting a VERY large number of posts, then you should use a HashSet instead of ArrayList for performance reasons (when we run the .contains() method, each element of the ArrayList is iterated over).

Build new json string using a json template file and a json data file

I have a json template file as below
{
"value": "abc",
"Treatments": [{
"value": "def",
"Stages": [{
"value": "ghi"
}]
}]
}
And a json data
{ "abc": {
"labelabc": "Assembly name abc",
"typeabc": "STRING_TYPE abc",
"formatabc": "N abc"
},
"def": {
"labeldef": "Assembly name def",
"typedef": "STRING_TYPE def",
"formatdef": "N def"
},
"ghi": {
"labelghi": "Assembly name ghi",
"typeghi": "STRING_TYPE ghi",
"formatghi": "N ghi"
}
}
I'm looking for a solution to parse the template into
{
"labelabc": "Assembly name abc",
"typeabc": "STRING_TYPE abc",
"formatabc": "N abc",
"Treatments": [
{
"labeldef": "Assembly name def",
"typedef": "STRING_TYPE def",
"formatdef": "N def",
"Stages": [
{
"labelghi": "Assembly name ghi",
"typeghi": "STRING_TYPE ghi",
"formatghi": "N ghi"
}
]
}
]
}
Meant:
Replace "value" entry with the jsonobject value in json data file
I already used the code below to achieve the result with 2 levels template. But failed with above 3 levels template
public static JsonElement generateTemplate(JsonElement templateJson, JsonElement dataTemplate) {
if (templateJson.isJsonArray()) {
for (JsonElement jsonElement1 : templateJson.getAsJsonArray()) {
generateTemplate(jsonElement1, dataTemplate);
}
} else if (templateJson.isJsonObject()) {
for (Iterator<Map.Entry<String, JsonElement>> iterator = templateJson.getAsJsonObject().entrySet()
.iterator(); iterator.hasNext();) {
Map.Entry<String, JsonElement> entry = iterator.next();
if (entry.getKey().equals("value")) {
templateJson = dataTemplate.getAsJsonObject().get(entry.getValue().getAsString()).getAsJsonObject();
} else {
if (entry.getValue().isJsonObject()) {
generateTemplate(entry.getValue(), dataTemplate);
} else
templateJson.getAsJsonObject().add(entry.getKey(), p.parse(
dataTemplate.getAsJsonObject().get(entry.getValue().getAsString()).getAsJsonObject()
));
}
generateTemplate(entry.getValue(), dataTemplate);
}
}
return templateJson;
}
Very appreciated your advices
You could use recursion. This is main idea, you can adjust actual replacements according to your needs later in modifyObject method which for "value": "abc" would expect object held in data under
"abc": {
"labelabc": "Assembly name abc",
"typeabc": "STRING_TYPE abc",
"formatabc": "N abc"
}
private static void modifyObject(JsonObject obj, JsonObject replacement) {
obj.remove("value");
for (String key : replacement.keySet()) {
obj.addProperty(key, replacement.get(key).getAsString());
}
}
public static JsonElement traverse(JsonElement element, JsonObject allReplacements) {
if (element.isJsonObject()) {
JsonObject asJsonObject = element.getAsJsonObject();
///keys can change after we modify object,
//but we want to iterate only over original keys
Set<String> originalKeys = new HashSet<>(asJsonObject.keySet());
for (String key : originalKeys) {
if (key.equals("value")) {
String value = asJsonObject.get(key).getAsString();
modifyObject(asJsonObject, allReplacements.getAsJsonObject(value));
} else {
traverse(asJsonObject.get(key), allReplacements);
}
}
} else if (element.isJsonArray()) {
for (JsonElement innerElement : element.getAsJsonArray()) {
traverse(innerElement, allReplacements);
}
}
return element;
}
Usage:
public static void main(String[] args) throws Exception {
String jsonTemplate =
"{\n" +
" \"value\": \"abc\",\n" +
" \"foo\": \"bar\",\n" +
" \"Treatments\": [ {\n" +
" \"value\": \"def\",\n" +
" \"Stages\": [ [ {\n" +
" \"value\": \"ghi\"\n" +
" } ] ]\n" +
" } ]\n" +
"}";
String jsonData = "{ \"abc\": {\n" +
" \"label\": \"Assembly name abc\",\n" +
" \"type\": \"STRING_TYPE abc\",\n" +
" \"format\": \"N\"\n" +
" },\n" +
" \"def\": {\n" +
" \"label\": \"Assembly name def\",\n" +
" \"type\": \"STRING_TYPE\",\n" +
" \"format\": \"N\"\n" +
" },\n" +
" \"ghi\": {\n" +
" \"label\": \"Assembly name ghi\",\n" +
" \"type\": \"STRING_TYPE\",\n" +
" \"format\": \"N\"\n" +
" }\n" +
"}";
JsonParser jsonParser = new JsonParser();
JsonElement template = jsonParser.parse(jsonTemplate);
JsonObject data = (JsonObject) jsonParser.parse(jsonData);
JsonElement obj = template.deepCopy();//in case we don't want to modify original template
Gson gson = new GsonBuilder().setPrettyPrinting().create();
System.out.println(gson.toJson(traverse(obj, data)));
}
Output:
{
"foo": "bar",
"Treatments": [
{
"Stages": [
[
{
"label": "Assembly name ghi",
"type": "STRING_TYPE",
"format": "N"
}
]
],
"label": "Assembly name def",
"type": "STRING_TYPE",
"format": "N"
}
],
"label": "Assembly name abc",
"type": "STRING_TYPE abc",
"format": "N"
}

How to add data to a nested hashmap correctly

I am parsing json file and adding the schema information to a nested hash map.But when I tried to print the nested hashmap it is giving me the same values for all key.Need help on how to store data into a nested hashmap correctly.
My json file :
{
"status":"success",
"tables":[
{
"dbname":"idn",
"tableName":"my_monthly_hits_b",
"schema":"(cm11:chararray)",
"location":"/user/mydb/"
},
{
"dbname":"idn",
"tableName": "my_monthly_match",
"schema":"(city:chararray,match:chararray,cm11:chararray)",
"location":"/user/mydb1"
}
]
}
My code :
public Map<String,Map<String,String>> getDataTypes(String responsePath){
Map<String,Map<String,String>> maped = new HashMap<String,Map<String,String>>();
Map<String,String> colDataTypes = new HashMap<String,String>();
try{
JsonParser parser = new JsonParser();
Object obj = parser.parse(new FileReader(responsePath);
JsonObject jObj = (JsonObject) obj;
JsonArray jArray = (JsonArray) jObj.get("tables");
Iterator<JsonElement> itr = jArray.iterator();
while(itr.hasNext())
{
JsonObject innerObj = (JsonObject) itr.next();
JsonElement shm = innerObj.get("schema");
JsonElement jTableName = innerObj.get("tableName");
String tableName = jTableName.toString();
String ss = shm.toString().replaceAll("\"","").replaceAll("[()]",""):
System.out.println("The required JSON string --->" + ss);
if(ss.contains(","){
String[] str = ss.split(",");
for(String s: str){
String[] ptr = s.split(":");
colDataTypes.put(prt[0],ptr[1]);
}
}
else{
String[] str1 = ss.split(":");
colDataTypes.put(str1[0],str1[1]);
}
maped.put(tabName,colDataTypes);
for(String tab : maped.keySet()){
System.out.println("#####" + "Table Name " + tab + "value" + maped.get(tab));
}
}
}
catch(FileNotFoundException ex)
{
}
return maped;
}
You can use a library like Jackson to manipulate the JSON tree (shown here) or marshal the JSON to an object graph.
package foo;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
public class Foo {
public static void main(String[] args) throws Exception {
final ObjectMapper mapper = new ObjectMapper();
final JsonNode node = mapper.readTree("{ " +
" \"status\":\"success\"," +
" \"tables\":[ " +
" { " +
" \"dbname\":\"idn\"," +
" \"tableName\":\"my_monthly_hits_b\"," +
" \"schema\":\"(cm11:chararray)\"," +
" \"location\":\"/user/mydb/\"" +
" }," +
" { " +
" \"dbname\":\"idn\"," +
" \"tableName\":\"my_monthly_match\"," +
" \"schema\":\"(city:chararray,match:chararray,cm11:chararray)\"," +
" \"location\":\"/user/mydb1\"" +
" }" +
" ]" +
"}");
final ArrayNode tables = (ArrayNode) node.get("tables");
// Add a new schema
final ObjectNode newSchema = tables.addObject();
newSchema.put("dbname", "foo db name");
newSchema.put("tableName", "foo table name");
newSchema.put("schema", "(foo:chararray,bar:chararray)");
newSchema.put("location", "/foo/bar");
mapper.enable(SerializationFeature.INDENT_OUTPUT);
System.out.println(mapper.writeValueAsString(node));
}
}
The example above will print:
{
"status" : "success",
"tables" : [ {
"dbname" : "idn",
"tableName" : "my_monthly_hits_b",
"schema" : "(cm11:chararray)",
"location" : "/user/mydb/"
}, {
"dbname" : "idn",
"tableName" : "my_monthly_match",
"schema" : "(city:chararray,match:chararray,cm11:chararray)",
"location" : "/user/mydb1"
}, {
"dbname" : "foo db name",
"tableName" : "foo table name",
"schema" : "(foo:chararray,bar:chararray)",
"location" : "/foo/bar"
} ]
}

Categories