Nested JSON parsing using Java - java

{
"transaction": {
"id": 1,
"empid": "12345",
"details1": {
"name": "xyz",
"age": "30",
"sex": "M",
"Address": {
"Office": "office",
"Home": "Home"
}
},
"abcDetails": "asdf",
"mobile": 123455
},
"details2": {
"id": 2,
"empid": "64848",
"details": {
"name": "eryje",
"age": 3027,
"sex": "M",
"Address": {
"Office": "office",
"Home": "Home"
}
},
"abcDetails": "fhkdl",
"mobile": 389928
}
}
I am getting the data in above format. Here I did split and Iterating the data using loop. First time am getting below formatted data. So in this I want get name and age value and details1.Address.Office value also(keys are not static).
"details1": {
"name": "xyz",
"age": "30",
"sex": "M",
"Address": {
"Office": "office",
"Home": "Home"
}
}

Try using JSONObject keys() to get the key and then iterate each key to get to the dynamic value.
// searchResult refers to the current element in the array "search_result"
JSONObject questionMark = searchResult.getJSONObject("question_mark");
Iterator keys = questionMark.keys();
while(keys.hasNext()) {
// loop to get the dynamic key
String currentDynamicKey = (String)keys.next();
// get the value of the dynamic key
JSONObject currentDynamicValue = questionMark.getJSONObject(currentDynamicKey);
// do something here with the value...
}
Reference : How to parse a dynamic JSON key in a Nested JSON result?

Related

Jackson/Gson get specified key from Json - Java

i have problem with get specified value from json. I need get Key value from json below, its start with array/list.
[
{
"Version": 1,
"Key": "353333_PC",
"Type": "PostalCode",
"Rank": 500,
"LocalizedName": "Suwalki",
"EnglishName": "Suwalki",
"PrimaryPostalCode": "16-400",
"Region": {
"ID": "EUR",
"LocalizedName": "Europe",
"EnglishName": "Europe"
},
"Country": {
"ID": "PL",
"LocalizedName": "Poland",
"EnglishName": "Poland"
}
}
]
I have tried code like this:
String content = parent.path("Key").asText();
return content;
but it returns empty string. Have u any idea how get this?

How to count by attribute in JSON?

I have the following JSON:
{
"items": [
{
"id": "1",
"name": "John",
"location": {
"town": {
"id": "10"
},
"address": "600 Fake Street",
},
"creation_date": "2010-01-19",
"last_modified_date": "2017-05-18"
},
{
"id": "2",
"name": "Sarah",
"location": {
"town": {
"id": "10"
},
"address": "76 Evergreen Street",
},
"creation_date": "2010-01-19",
"last_modified_date": "2017-05-18"
},
{
"id": "3",
"name": "Hamed",
"location": {
"town": {
"id": "20"
},
"address": "50 East A Street",
},
"creation_date": "2010-01-19",
"last_modified_date": "2017-05-18"
}
]
}
And I need to get something like this, count how many times each townId appears:
[ { "10": 2 }, {"20": 1 }]
I'm trying to find the most eficient way to do this. Any idea?
Most efficient way is to load the String in a StringBuilder and remove all line breaks and white spaces. Then search for index of "town":{"id":" string (town start index) and then search for the end index (String `"}'). Using the 2 indexes you can extract town ids and count them.
No need to deserialize the JSON into POJO objects:) and extract values by xpath from the POJOs.

Checking Keys in JSON Records using Java

"transaction": {
"id": 1,
"empid": "12345",
"details1": {
"name": "xyz",
"age": "30",
"sex": "M",
"Address": {
"Office": "office",
"Home": "Home"
}
},
"abcDetails": "asdf",
"mobile": 123455
},
I need to test if JSON record contains more then two keys(details, Address).
Then, I need to pass those key input to this line:
parserValue1 = parserValue.asObject().get("firstKey").asObject().get("secondKey");
Can anyone help me?
Many json parsers have a has("key") or contains("key") accessor.
Otherwise you will have to add a condition to check if get("") returns null, or turn your whole Json object into a map, where you do the same checks.

Tuple and bags to Json

I had 4 files. I joined all the files using Pig and obtained the final output and grouped the data as required. Now that I have my input something like this.
({(9723,(N,N)),({({(11,G),(H,House),(1,1ST),(02/25/2015)}),({(10,L),(H,House),(16,EMPTY),(02/25/2015)})})})
which is my pig output.
I want to convert it into JSON.
My output should look like this.
{
"department": {
"department_id": "9723",
"department_group": {
"flag1": "N",
"flag2": "N"
},
"employee_detail1": {
"employee_type": {
"code": "11",
"name": "G"
},
"employee_level": {
"code": "H",
"name": "House"
},
"employee_dmg": {
"code": "1",
"name": "1st"
},
"DOJ": "02/25/2015"
},
"employee_detail2": {
"employee_type": {
"code": "10",
"name": "L"
},
"employee_level": {
"code": "H",
"name": "House"
},
"employee_dmg": {
"code": "0",
"name": "No"
},
"DOJ": "02/25/2015"
}
}
}
There are 2 bags(meaning 2 employee details).... grouped by emp_id and employee group(tuple with flag1 and flag2)....
Can someone suggest me the best way to convert this into JSON...
You can STORE your data with JsonStorage, it will handle nicely a bag.

HOW to remove duplicate JSON Objects from JSON Array

I have got the JSON as below
{
"brands": [
{
"name": "ACC",
"quantity": "0",
"listedbrandID": 1,
"status": "0"
}
],
"others": [
{
"name": "dd",
"quantity": "55"
},
{
"name": "dd",
"quantity": "55"
}
]
}
How can i remove the duplicates from others JSON array
i have tried as following
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
String json = "{
"brands": [
{
"name": "ACC",
"quantity": "0",
"listedbrandID": 1,
"status": "0"
}
],
"others": [
{
"name": "dd",
"quantity": "55"
},
{
"name": "dd",
"quantity": "55"
}
]
}
";
JSONObject json_obj = new JSONObject(json);
JSONArray array = json_obj.getJSONArray("others");
HashMap<String, String> map = new HashMap<String, String>();
System.out.println(array.length());
for(int i=0;i<array.length();i++)
{
String name = array.getJSONObject(i).getString("name");
String quantity = array.getJSONObject(i).getString("quantity");
if(name!=null && !name.trim().equals(""))
{
map.put(name, quantity);
}
}
But no idea how to remove duplicate JSON other than which are present under Map only.
Create an object representing your Others. It will have a name and quantity property and it will also override the equals method, wherein, two Other objects are considered to be equal if they have the same name and quantity properties.
Once you have that, iterate over your JSON, create a new Other object and place it in a HashSet<Other>. The .equals will ensure that the HashSet will contain unique items, as per your definition of unique.

Categories