How to remove empty objects from Json Array - java

So the json is something like it,
"stores": [
{
"amazon": []
},
{
"flipkart": {
"product_store": "Flipkart",
"product_store_logo": "http://images-api.datayuge.in/image/ZmxpcGthcnRfc3RvcmUucG5n.png",
"product_store_url": "https://price-api.datayuge.com/redirect?id=aHR0cHM6Ly9kbC5mbGlwa2FydC5jb20vZGwvbWktYTEtYmxhY2stNjQtZ2IvcC9pdG1leDl3eHh6M2FtamF0P3BpZD1NT0JFWDlXWFVTWlZZSEVUJmFmZmlkPWFydW5iYWJ1bA",
"product_price": "14999",
"product_offer": "",
"product_color": "",
"product_delivery": "3-4",
"product_delivery_cost": "0",
"is_emi": "1",
"is_cod": "1",
"return_time": "10 Days"
}
},
{
"snapdeal": []
}
]
So the non empty object like flipkart is a JsonObject but all other empty objects are array. So I am so confused about how to remove them.
JSONArray store_array = product_details_json.getJSONObject("data").getJSONArray("stores");
for (int i = 0; i<store_array.length(); i++){
JSONObject store = store_array.getJSONObject(i);
if (!store.getJSONObject(store.keys().next()).has("product_store")){
store_array.remove(i);
}else {
Log.i("Size :",store_array.length()+"");
}
}
But that's not working. I know I am doing this all wrong. Because it has both array and objects so i get the following error
Value [] at amazon of type org.json.JSONArray cannot be converted to JSONObject
Need Help!

I see two problems with your code:
Your JSON structure for "stores" is heterogeneous — some elements have a key that maps to an array and some to an object. That's the immediate cause of the error you are seeing. You can either modify your JSON so everything key maps to an object or code defensively.
When you remove an entry, all subsequent entries move up one space, but since you then increment the loop index i, you skip the entry that just moved into the index you just removed. The easiest way to deal with that is to iterate through store_array in reverse order.
Putting this all together (and assuming you aren't going to change your JSON structure), something like the following (untested) should work:
JSONArray store_array = product_details_json.getJSONObject("data").getJSONArray("stores");
for (int i = store_array.length() - 1; i >= 0; i--){
JSONObject store = store_array.getJSONObject(i);
Object storeData = store.get(store.keys().next());
boolean isValidStore = storeData instanceof JSONObject
&& ((JSONObject) storeData).has("product_store");
if (!isValidStore) {
store_array.remove(i);
}
}

Related

Looping over an array with alternating elements

Difficult to sum up the question in one sentence. It's easier to show you what I want. I have the following response of a Solr query:
{
.....
},
"facet_counts":{
"facet_queries":{},
"facet_fields":{
"city":[
"New York",23258,
"Los Angeles",13322,
"Paris",1189]},
"facet_ranges":{},
"facet_intervals":{},
"facet_heatmaps":{}}}
I use GSON to parse this JSON data and get a JsonArray containing the array "city". That contains alternating elements for the field name (key) and the corresponding value. So e.g. we have 13322 hits for "Los Angeles". I want to iterate over this array to get out the key=value pairs it contains.
I can think of a number of simple solutions for this task, for example
boolean isKey = true;
String key;
String val;
for(JsonElement je : facetMT) {
if(isKey) {
key = je.getAsString();
isKey = false;
} else {
val = je.getAsString();
resultMap.add(key,val);
isKey = true;
}
}
Or any other way to distinguish the odd and even elements, like checking for divisibility by 2.
But that feels crude. I'm sure Java has some other, elegant way to do this, like working with an iterator and skipping every other element. But I'm not good enough in Java to know how to do that, or any other supersmart way.
Any suggestions, or should I just go with the crude code?
If you're sure about the length of the array then you should read twice at one:
for(int i = 0; i < facetMT.size() - 1; i = i + 2) {
JsonElement key = facetMT.get(i).getAsString();
JsonElement value= facetMT.get(i + 1).getAsString();
resultMap.add(key,val);
}
Note the i < facetMT.size() - 1 condition to avoid IndexOutOfBoundsException
If you are sure that array will always have even amount of elements you can use two of them in one iteration like
Iterator<JsonElement> it = facetMT.iterator();
while (it.hasNext()) {
// iterate over two elements
resultMap.put(it.next().getAsString(), it.next().getAsString());
}
But to be honest proper answer would be enforcing results in form of array of objects like
"city":[
{"cityName":"New York", "amount":23258},
{"cityName":"Los Angeles", "amount":13322},
{"cityName":"Paris", "amount":1189}
]
instead of what you have now
"city":[
"New York",23258,
"Los Angeles",13322,
"Paris",1189
]
This way your code could look like:
for (JsonElement jsonElement : facetMT) {
JsonObject obj = (JsonObject)jsonElement;
resultMap.put(obj.get("cityName").getAsString(),
obj.get("amount").getAsString());
}
In this case use for loop with indexes
For(int i=0;i<...
FaceMT[i]= altered value.
Alterate value doesn't work with iterator for

How to loop through json arrays nested inside json objects

I have this json text which has some symptoms associated with head diseases:
{
"Head": {
"Agitation": {
"conditions": "Generalized anxiety disorder,Medication reaction or side-effect"
},
"Anxiety": {
"conditions": "Generalized anxiety disorder,Depression (Adult)"
},
"Apathy": {
"conditions": "Depression (Adult),Medication reaction or side-effect,Dementia in head injury"
}
}
}
What I want is to access and display every symptom in this Head block using a for loop, and then access each symptom's conditions and store them separately as arrays.
This java code works but it's functionality is limited:
JSONObject jsonObject = (JSONObject) object;
JSONObject bodyPart = (JSONObject) jsonObject.get("Head");
JSONObject symptoms = (JSONObject) name.get("Agitation");
String res = (String) symptoms.get("conditions");
String[] tokens = res.split(",");
for (int i = 0; i < tokens.length; i++){
System.out.println(tokens[i]);}
Instead of displaying just the conditions of Agitation, how can I display every condition associated with every symptom without having to pass their String values manually into the get methods?
I don't know if I should use JSONArray for "Head" instead of JSONObject to access the symptoms.
Assuming this is the org.json package, you can use JSONObject's keys() method to get an iterator over the object's keys.
Iterator bodyParts = jsonObject.keys();
while (bodyParts.hasNext()){
String bodyPart = (String) bodyParts.next();
JSONObject symptomsJson = jsonObject.getJSONObject(bodyPart);
Iterator symptoms = symptomsJson.keys();
// And so on...
}
My friend, if you're using java, use objects, no plain text, my recommendation is to use Json.simple,
That way you can have List of objects and use the properties you want, take a look on the link examples.

read nested json object using loop

I have been searching from a long time and no solutions is working for me.
I have to retrieve the values from the json object using some loop , and number of nested values is random these can be 1 or may be 10.
json looks like this :
{
"keyInfo":[
{
"name":"ipek",
"key":"1221"
},
{
"name":"ipek",
"key":"1221"
}
],
"terminalInfo":{
"dateExp":"2-2-2",
"deviceId":"1222",
"tid":"122"
}
}
I have tried alot of solutions one of them is this :
JSONObject jsonObject =new JSONObject(jsonString);
JSONObject jsonChildObject = (JSONObject)jsonObject.get("keyInfo");
Iterator iterator = jsonChildObject.keys();
String key = null;
while(iterator.hasNext()){
key = (String)iterator.next();
System.out.println("inval value: "
+ ((JSONObject)jsonChildObject.get(key)).get("inval"));
}
but none of them is working for me please help.
THANKS IN ADVANCE.
You are trying to use an array as a map. jsonChildObject is actually an JSONArray. It does not have keys; for instance you can have the same {"name":"ipek","key":"1221"} multiple times. If you know that the key is unique among items in this array, you can try and build a HashMap out of it if you need, but the structure you have is definitely an array.

Remove object from json response Android

Hello guys I need your help. How do I remove this from my json response?
[
{
"Woa": [
"Seo",
"Rikjeo",
"JDa"
]
},
"Aha",
"Aad",
"Char"
]
I want to remove this:
{
"Woa": [
"Seo",
"Rikjeo",
"JDa"
]
}
This is what I tried so far:
for (int i = 0; i < array.length(); ++i) {
list.add(array.getString(i));
}
list.remove(0);
But it is still not removed. How do I do that? Any ideas will be greatly appreciated
Edited list.remove(1) to (0)
After removing the item you need to create the JSON again using the list.
list.remove(1);
JSONArray jsArray = new JSONArray(list);
If you want to convert JSONArray to JSON string:
jsArray.toString()
Your list will be updated, not your JSON object.
Why don't you use a JSONparser.
JSONObject obj = new JSONObject(mystring);
obj.remove("entryname");
One problem is that the element you are trying to remove is the first one, and the index of the first element of a JSONArray object is zero.
You are calling list.remove(1) which removes the second element of the array.
The following should be sufficient:
list.remove(0);
... without the stuff prior to it.
If this is not working, then my guess is that you are removing the element too late; i.e. after the JSONArray object has been serialized. However, we need to see more (relevant) code to be sure.
The Android documentation for JSONArray fails to mention that array indexing is zero-based. However, it is. I checked the source code. Furthermore most other Java data structures (arrays, lists etcetera) use zero-based indexing. (A notable exception is Java data structures modelled on the W3C DOM APIs.)
You should make something that's more general so you don't have to modify your code EVERY time the JSON changes. For example:
//here we create the JSONArray object from the string that contains the json data
JSONArray array = new JSONArray(myJsonString);
//here we create a list that represents the final result - a list of Strings
List<String> list = new ArrayList<>();
//here we parse every object that the JSONArray contains
for (int i = 0; i < array.length(); i++) {
//if the current item is an JSONObject, then we don't need it, so we continue our iteration
//this check is not necessary because of the below check,
//but it helps you see things clearly: we IGNORE JSONObjects from the array
if (array.get(i) instanceof JSONObject) {
continue;
}
//if our current object is a String, we add it to our final result
if (array.get(i) instanceof String) {
list.add(array.getString(i));
}
}
you can parse your string to JSONArray, the first element of the array is what you want.
#Test
public void test() {
String str = "[ {\"Woa\": [\"Seo\",\"Rikjeo\",\"JDa\"]},\"Aha\",\"Aad\",\"Char\"]";
try {
JSONArray array;
array = new JSONArray(str);
System.out.println(array);
System.out.println(array.get(0));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
[{"Woa":["Seo","Rikjeo","JDa"]},"Aha","Aad","Char"]
{"Woa":["Seo","Rikjeo","JDa"]}
PASSED: test
You Can one by one remove
while (user_data.length() > 0) {
user_data.remove(user_data.keys().next());
}

Android: Handling json objects that can be a string or array

Ran into a situation where am not sure how to handle it.
I have json data that comes from a server; for example:(am just posting part of the json, so, yes, the json is valid)
"wall_id": 889149,
"poster_image_thumbnail": "http:\/\/www.mface.me\/images\/avatar\/thumb_62441559ddb1dda7513d0f94.jpg",
"post_type": "profile",
"post_content": [{
"text": "",
"images_count": 1,
"images": ["https:\/\/fbcdn-sphotos-a-a.akamaihd.net\/hphotos-ak-ash4\/227408_475848819113499_663318592_n.jpg"]
}]
Created a class to store this json data
public class feedFormat{
Integer wall_id;
String poster_image_thumbnail;
String post_type;
String post_content;
}
There are times when post_content can be empty or an array as the example above. I have declared post_content as String in feedFormat. This is obviously throwing a cast exception (Converting array to string?).
I was expecting JSONObject to read it as a string and later convert it into an array from there, but does'nt seem to go that way.
How can i dynamically handle a string or an array? if it is an array, i need to break it down.
I am porting this app from IOS to android, there is a "id" object in IOS that can be of any class. I check if the class is a NSSTring or NSArray and take it from there. Here in Java, am not sure how to handle it.
Any suggestions are highly appreciated
If your JSON array is empty, it will be like that :
"post_content": []
It will then remain an array, with the particularity of being 0-sized.
Then I suggest you parse directly your JSON array into a appropriate data structure, whatever the size, like an ArrayList> for example. You will then be able to go through all the items of your JSON array, and for each item, add a new HashMap in your arraylist. Every hashmap will contain there pairs of key values.
However, if I understand well your JSON, it seems that it will be always an array of three elements, the third element being itself a array, which size is given bu the attribute images_count. This is not very good, your JSON structure should be :
"post_content": {
"text": "",
"images": [
"https://fbcdn-sphotos-a-a.akamaihd.net/hphotos-ak-ash4/227408_475848819113499_663318592_n.jpg"
]
}
Since images is an array, you can easily get its size.
JSONObject has functions called has(String key) which checks if there is a mapping for a key and isNull(String key) which checks if a particular key is null. Use these to check the key before reading.
public class FeedFormat{
Integer wall_id;
String poster_image_thumbnail;
String post_type;
JSONArray post_content;
}
feedFormat toto = new feedFormat();
toto.post_content = yourJsonObject.getJsonArray("post_content");
This is the easiest way to do what you want. Another way is to create another class.
public class FeedFormat{
Integer wall_id;
String poster_image_thumbnail;
String post_type;
ArrayList<PostContent> post_content = new ArrayList<PostContent>();
}
public class PostContent {
String text;
Integer imageCount;
ArrayList<String> images = new ArrayList<String>();
}
With that you can handle each post content into specific object instead of use JSONObject / JSONArray.
you can check like this jsonobject.has("post_content")
if(jsonobject.has("post_content")) {
/// read array and do remaining stuff
}else {
// if not read another strings and put post_content as null.
}
You can use something like this:
String data= "wall_id": 889149,
"poster_image_thumbnail": "http:\/\/www.mface.me\/images\/avatar\/thumb_62441559ddb1dda7513d0f94.jpg",
"post_type": "profile",
"post_content": [{
"text": "",
"images_count": 1,
"images": ["https:\/\/fbcdn-sphotos-a-a.akamaihd.net\/hphotos-ak-ash4\/227408_475848819113499_663318592_n.jpg"]
}]
JSONArray jArray=data.getJSONArray("post_content");
for(int i=0; i<jArray.length(); i++)
{
JSONObject jObj=jArray.getJSONObject(i);
String text=jObj.getString("text");
int images_count=jObj.getInt("images_count");
String images=jObj.getInt("images");
}

Categories

Resources