Parse Json Recursively and manipulate the object value - java

I've a complex json object which i want to iterate and replace a specific key values. The key value to be replaced will be under rules -> field -> value and inside the value based on the id I've to replace the name. For instance i have a json object { "id" : "123" , "name" : "abc" } i want to replace with { "id" : "123" , "name" : "xyz" }
Input :
{
"condition": {
"rules": [{
"field": "stage.id",
"value": [{
"condition": [{
"rules": [{
"field": "stage.id",
"value": [{
"condition": [{
"rules": [{
"field": "stage.id",
"value": [{
"id": "123",
"name": "abc",
"item": {
"id": "456",
"name": "ffgg"
}
}]
}]
}]
}]
}]
}]
},
{
"field": "tasks.stage.id",
"value": [{
"id": "456",
"name": "def"
}]
},
{
"field": "stage.id",
"value": [{
"id": "123",
"name": "abc"
}]
}
]
}]
}
Expected Output:
{
"condition": {
"rules": [{
"field": "stage.id",
"value": [{
"condition": [{
"rules": [{
"field": "stage.id",
"value": [{
"condition": [{
"rules": [{
"field": "stage.id",
"value": [{
"id": "123",
"name": "xyz",
"item": {
"id": "456",
"name": "ffgg"
}
}]
}]
}]
}]
}]
}]
},
{
"field": "tasks.stage.id",
"value": [{
"id": "456",
"name": "def"
}]
},
{
"field": "stage.id",
"value": [{
"id": "123",
"name": "xyz"
}]
}
]
}]
}
I've iterated to the object based in the instance but replacing the value is not working as expected. I'm not sure what i've missed here. Can someone help me in here.
Code Sample
try {
loopThroughJson(new JSONObject(theInput)));
} catch (JSONException e) {
e.printStackTrace();
}
public static String loopThroughJson(Object input) throws JSONException {
if (input instanceof JSONObject) {
Iterator<?> keys = ((JSONObject) input).keys();
while (keys.hasNext()) {
String key = (String) keys.next();
if (!(((JSONObject) input).get(key) instanceof JSONArray))
if (((JSONObject) input).get(key) instanceof JSONObject) {
loopThroughJson(((JSONObject) input).get(key));
} else
System.out.println(key + "=" + ((JSONObject) input).get(key));
else
loopThroughJson(new JSONArray(((JSONObject) input).get(key).toString()));
}
}
if (input instanceof JSONArray) {
Gson gson = new Gson();
for (int i = 0; i < ((JSONArray) input).length(); i++) {
JSONObject a = ((JSONArray) input).getJSONObject(i);
if (a.opt("field") != null && a.opt("field").equals("stage.id")) {
Map<String, Object> fieldJson = gson.fromJson(a.toString(), Map.class);
List<Map<String, Object>> valueArray = (List<Map<String, Object>>) fieldJson.get("value");
for (Map<String, Object> valueObject : valueArray) {
if (valueObject.get("id").toString().equals("123")) {
valueObject.put("name", "xyz");
}
}
a = new JSONObject(fieldJson);
}
loopThroughJson(a);
}
}
return input.toString();

Related

How to handle JsonObject to own Object

Now i take JsonObject from API like this:
Its XML object converted to JsonObject.
"Details": {
"row": [
{
"item": [
{
"name": "Account",
"value": 12521512
},
{
"name": "ACCNO",
"value": 4214
},
{
"name": "Number",
"value": 5436
}
]
},
"item": [
{
"name": "Account",
"value": 5789678
},
{
"name": "ACCNO",
"value": 6654
},
{
"name": "Number",
"value": 0675
}
]
},
But i need convert this object and send like this:
{
"Details": {
"row": [
{
"Account": 12521512,
"ACCNO": 4214,
"Number": 12412421
},
{
"Account": 5789678,
"ACCNO": 6654,
"Number": "0675"
}
]
}
}
I have rows more than 1000, i need faster way to handle.
How to handle, please help me
You could use the JSON-java library to parse your input and transform it to your desired format. Something like this works but you may need to adjust it to your needs:
JSONObject jsonObject = new JSONObject(json); // Load your json here
JSONObject result = new JSONObject("{\"Details\": {\"row\": []}}");
for (Object row : jsonObject.getJSONObject("Details").getJSONArray("row")) {
if (!(row instanceof JSONObject)) continue;
Map<Object, Object> values = new HashMap<>();
for (Object item : ((JSONObject) row).getJSONArray("item")) {
if (!(item instanceof JSONObject)) continue;
values.put(((JSONObject) item).get("name"), ((JSONObject) item).get("value"));
}
result.getJSONObject("Details").getJSONArray("row").put(values);
}
// Now result is in your format

Java bolt for slack mapping block json and sending it to slack

I am trying to see the following message on slack:
{
"blocks": [{
"type": "actions",
"elements": [{
"type": "button",
"text": {
"type": "plain_text",
"text": "Farmhouse",
"emoji": true
},
"value": "click_me_123"
},
{
"type": "button",
"text": {
"type": "plain_text",
"text": "Kin Khao",
"emoji": true
},
"value": "click_me_123",
"url": "https://google.com"
},
{
"type": "button",
"text": {
"type": "plain_text",
"text": "Ler Ros",
"emoji": true
},
"value": "click_me_123",
"url": "https://google.com"
}
]
}]
}
Now on the java side I am using gson to map this to an object and then send it using ctx.say():
List<LayoutBlock> blocks = answers.stream().map(ans ->
gson.fromJson(ans.getContent(), LayoutBlock.class)
).collect(Collectors.toList());
Now this mapping fails. If I try to use a simpler json it works:
{
"type": "actions",
"elements": [
{
"type": "button",
"text": {
"type": "plain_text",
"text": "Option 1",
"emoji": true
},
"value": "{ \"name\": \"smalltalk.appraisal.thank_you\", \"entities\": { \"reply\": \"test\", \"option\": \"1\" } }"
},
{
"type": "button",
"text": {
"type": "plain_text",
"text": "Option 2",
"emoji": true
},
"value": "{ \"name\": \"smalltalk.appraisal.thank_you\", \"entities\": { \"reply\": \"test\", \"option\": \"2\" } }"
}
]
}
Basically what I'm seeing is that when using ctx.say(), you can either send a String or a List of Blocks:
default ChatPostMessageResponse say(String text) throws IOException, SlackApiException {
this.verifyChannelId();
ChatPostMessageResponse response = this.client().chatPostMessage(ChatPostMessageRequest.builder().text(text).channel(this.getChannelId()).build());
return response;
}
default ChatPostMessageResponse say(List<LayoutBlock> blocks) throws IOException, SlackApiException {
this.verifyChannelId();
ChatPostMessageResponse response = this.client().chatPostMessage(ChatPostMessageRequest.builder().blocks(blocks).channel(this.getChannelId()).build());
return response;
}
How am I supposed to convert the json shown in the slack block kit builder into a java object that can actually be sent using ctx.say()?

REST-assured | How to get a string value based on matching critera using JSONpath

I need to get count value based on matching critera using REST Assured Jsonpath
I tried the following but its not working :
response().jsonPath().getString("{it.B == '456'}.B1.size()")
JSON :
[{ "A": "123", "A1": [{ "A11": "A111" }, { "A12": "A112" }, { "A13": "A113" }] },
{ "B": "456", "A1": [{ "B11": "B111" }, { "B12": "B112" }, { "B13": "B113" }, { "B14": "B114" }, { "B15": "B115" }] },
{ "C": "456", "A1": [{ "C11": "C111" }, { "C12": "C112" }, { "C13": "C113" }, { "C14": "C114" }, { "C15": "C115" }, { "C16": "C116" }] }
]
Please try the below, it will return 456
response().jsonPath().getString("$.[1].B")

Insert object to nested array

I'm trying to insert an object to a nested array through the java api, but I get the following error
MapperParsingException[object mapping for [X] tried to parse field [null] as object, but found a concrete value]
doing it through Kibana as shown below the same script works.
Any ideas on how to fix this?
The java code is the following
HashMap<String, Object> params = new HashMap<>();
params.put("object", objectAsString);
Script script = new Script(ScriptType.INLINE, "painless", "ctx._source.media.add(params.object)", params);
UpdateResponse result = elasticClient.prepareUpdate(indexName, "Type", documentId).setScript(script).execute().actionGet();
Trhough Kibana
POST index/document/id/_update
{
"script": {
"lang": "painless",
"inline": "ctx._source.media.add(params.object)",
"params": {
"object": {
"description" : "A second image",
"height" : 5,
"weight": 5,
"name" : "Test",
"orientation" : "Vertical",
"url" : "htttp://newurl.jpg",
"tags":["first","second"],
"type":"image"
}
}
}
}
The template mapping is the following:
"mappings": {
"FOLDER_MODULE": {
"properties": {
"name": {
"type": "keyword"
},
"publisherId": {
"type": "keyword"
},
"username": {
"type": "keyword"
},
"media": {
"type": "nested",
"properties": {
"id": {
"type": "text"
},
"name": {
"type": "text"
},
"publisherId": {
"type": "short"
},
"tags": {
"type": "text"
},
"description": {
"type": "text"
},
"url": {
"type": "keyword"
},
"createdDate": {
"format": "strict_date_optional_time||epoch_millis",
"type": "date"
},
"height": {
"type": "float"
},
"width": {
"type": "float"
},
"weight": {
"type": "float"
},
"orientation": {
"type": "keyword"
},
"status": {
"type": "keyword"
},
"type": {
"type": "keyword"
},
"username": {
"type": "keyword"
}
}
}
}
}
}

Parsing JSON using GSON with multiple inner objects.

I would like to parse the following JSON response, using GSON. As you can see, there are a lot of inner json objects in the response. Do I need to create POJO classes for all the inner objects or is there any alternative?
{
"nodeProperties": [
{
"properties": {
"timeStamp": {
"value": 1400475483694,
"name": "connectedSince"
},
"macAddress": {
"value": "00:00:00:00:00:03"
},
"tables": {
"value": -1
},
"capabilities": {
"value": 199
},
"tier": {
"value": 1
},
"supportedFlowActions": {
"value": "[Controller, Drop, Enqueue, HwPath, Output, PopVlan, SetDlDst, SetDlSrc, SetNwDst, SetNwSrc, SetNwTos, SetTpDst, SetTpSrc, SetVlanId, SetVlanPcp, SwPath]"
},
"buffers": {
"value": 256
},
"description": {
"value": "NEXT_NEWSwitch3"
},
"forwarding": {
"value": 0
}
},
"node": {
"id": "00:00:00:00:00:00:00:03",
"type": "OF"
}
},
{
"properties": {
"timeStamp": {
"value": 1400475481261,
"name": "connectedSince"
},
"macAddress": {
"value": "00:00:00:00:00:02"
},
"tables": {
"value": -1
},
"capabilities": {
"value": 199
},
"tier": {
"value": 1
},
"supportedFlowActions": {
"value": "[Controller, Drop, Enqueue, HwPath, Output, PopVlan, SetDlDst, SetDlSrc, SetNwDst, SetNwSrc, SetNwTos, SetTpDst, SetTpSrc, SetVlanId, SetVlanPcp, SwPath]"
},
"buffers": {
"value": 256
},
"description": {
"value": "None"
},
"forwarding": {
"value": 0
}
},
"node": {
"id": "00:00:00:00:00:00:00:02",
"type": "OF"
}
},
{
"properties": {
"timeStamp": {
"value": 1400475478695,
"name": "connectedSince"
},
"macAddress": {
"value": "00:00:00:00:00:01"
},
"tables": {
"value": -1
},
"capabilities": {
"value": 199
},
"supportedFlowActions": {
"value": "[Controller, Drop, Enqueue, HwPath, Output, PopVlan, SetDlDst, SetDlSrc, SetNwDst, SetNwSrc, SetNwTos, SetTpDst, SetTpSrc, SetVlanId, SetVlanPcp, SwPath]"
},
"buffers": {
"value": 256
},
"description": {
"value": "None"
},
"forwarding": {
"value": 0
}
},
"node": {
"id": "00:00:00:00:00:00:00:01",
"type": "OF"
}
}
]
}
Simply convert the JSON string into Map.
Sample code:
FileReader in = new FileReader(new File("resources/json3.txt"));
BufferedReader br = new BufferedReader(in);
// Convert JSON string into MAP object
Gson gson = new Gson();
Type type = new TypeToken<Map<String, ArrayList<Map<String, Map<String, Object>>>>>() {}.getType();
Map<String, ArrayList<Map<String, Map<String, Object>>>> map = gson.fromJson(br, type);
for (String key : map.keySet()) {
System.out.println(key);
for (Map<String, Map<String, Object>> value : map.get(key)) {
for (String k : value.keySet()) {
System.out.println(k);
for (String k1 : value.get(k).keySet()) {
System.out.println(k1 + ":" + value.get(k).get(k1));
}
}
System.out.println("--------------");
}
}
in.close();
br.close();
You can do it using some POJO classes as well which are replica of the JSON string that is more simple and shorter.
Sample code:
class PropertiesNode {
Properties properties;
Node node;
// getter & setter
}
class Node {
String id;
String type;
// getter & setter
}
class Properties {
Map<String, Object> timeStamp;
Map<String, String> macAddress;
Map<String, Integer> tables;
Map<String, Integer> capabilities;
Map<String, Integer> tier;
Map<String, String> supportedFlowActions;
Map<String, Integer> buffers;
Map<String, String> description;
Map<String, Integer> forwarding;
// getter & setter
}
Gson gson = new Gson();
Type type = new TypeToken<Map<String, ArrayList<PropertiesNode>>>() {}.getType();
Map<String, ArrayList<PropertiesNode>> nodeProperties = gson.fromJson(br, type);
System.out.println(new GsonBuilder().setPrettyPrinting().create().toJson(nodeProperties).toString());
output:
{
"nodeProperties": [
{
"properties": {
"timeStamp": {
"value": 1.400475483694E12,
"name": "connectedSince"
},
"macAddress": {
"value": "00:00:00:00:00:03"
},
"tables": {
"value": -1
},
"capabilities": {
"value": 199
},
"tier": {
"value": 1
},
"supportedFlowActions": {
"value": "[Controller, Drop, Enqueue, HwPath, Output, PopVlan, SetDlDst, SetDlSrc, SetNwDst, SetNwSrc, SetNwTos, SetTpDst, SetTpSrc, SetVlanId, SetVlanPcp, SwPath]"
},
"buffers": {
"value": 256
},
"description": {
"value": "NEXT_NEWSwitch3"
},
"forwarding": {
"value": 0
}
},
"node": {
"id": "00:00:00:00:00:00:00:03",
"type": "OF"
}
},
{
"properties": {
"timeStamp": {
"value": 1.400475481261E12,
"name": "connectedSince"
},
"macAddress": {
"value": "00:00:00:00:00:02"
},
"tables": {
"value": -1
},
"capabilities": {
"value": 199
},
"tier": {
"value": 1
},
"supportedFlowActions": {
"value": "[Controller, Drop, Enqueue, HwPath, Output, PopVlan, SetDlDst, SetDlSrc, SetNwDst, SetNwSrc, SetNwTos, SetTpDst, SetTpSrc, SetVlanId, SetVlanPcp, SwPath]"
},
"buffers": {
"value": 256
},
"description": {
"value": "None"
},
"forwarding": {
"value": 0
}
},
"node": {
"id": "00:00:00:00:00:00:00:02",
"type": "OF"
}
},
{
"properties": {
"timeStamp": {
"value": 1.400475478695E12,
"name": "connectedSince"
},
"macAddress": {
"value": "00:00:00:00:00:01"
},
"tables": {
"value": -1
},
"capabilities": {
"value": 199
},
"supportedFlowActions": {
"value": "[Controller, Drop, Enqueue, HwPath, Output, PopVlan, SetDlDst, SetDlSrc, SetNwDst, SetNwSrc, SetNwTos, SetTpDst, SetTpSrc, SetVlanId, SetVlanPcp, SwPath]"
},
"buffers": {
"value": 256
},
"description": {
"value": "None"
},
"forwarding": {
"value": 0
}
},
"node": {
"id": "00:00:00:00:00:00:00:01",
"type": "OF"
}
}
]
}

Categories