4

I have the below JSON , i need to remove all the keys which have null value

I have tried this

import java.util.Iterator;
import org.json.JSONException;
import org.json.JSONObject;
public class Remove {
    public static void main(String[] args) throws JSONException {
    String str = "{\r\n" + 
            "       \"videos\": {\r\n" + 
            "   
            "       }}";
        JSONObject json_obj = new JSONObject(str);
        JSONObject allKeys_json=    json_obj.getJSONObject("videos");
        Iterator<String> keys = allKeys_json.keys();
        while( keys.hasNext() ) {
        String keyanme = (String)keys.next();
        String keyvalue = allKeys_json.getString(keyanme);
        if(keyvalue.contains("null"))
        {
            System.out.println(keyanme+"\t"+keyvalue);
            json_obj.remove(keyanme);
        }
        }
    System.out.println(allKeys_json);
    }
}

but the actual json is unaffected , could you please tell me how to do this .

2
  • You could try: JSONObject putOpt(String name, Object value) Equivalent to put(name, value) when both parameters are non-null; does nothing otherwise. Commented Feb 9, 2017 at 12:21
  • org.json.JSONObject is from what API ? Commented Feb 9, 2017 at 12:34

6 Answers 6

3

If it's only about manipulating a string which structure you know well a solution would be to use some regex

str.replaceAll(".*\": null(,)?\\r\\n", "");

It could be easier to find a good regex than to invest time in building a model that could be used by Jackson.

Three notes:

  • the code above doesn't figure out which is the last line and adapt the json accordingly.

  • the pattern should be compiled separately.

  • org.json is very inefficient compared to Jackson.

Sign up to request clarification or add additional context in comments.

4 Comments

@NikunjParadva as mentionned in my answer :-)
Yes but got any solutions?
@NikunjParadva you could try this regex I just googled kaspars.net/blog/dev/regex-remove-all-line-breaks-from-text or google for another one. Let me know once you find the solution. The answer here is that using a Json manipulation library would be more cumbersome than manipulating the json string with a regex.
Sure i ll try and inform you soon
1

Check your null value like this

if(keyvalue == null)

This fixex the issue

Comments

0

Firstly, create an model class which is corresponding to the JSON string.

Add

@JsonIgnoreProperties(ignoreUnknown = true)

to your model class such as

@JsonIgnoreProperties(ignoreUnknown = true) 
public class Video  {
    //the properties
}

http://www.baeldung.com/jackson-deserialize-json-unknown-properties

1 Comment

0

Use Jackson API and use @JsonInclude(Include.NON_NULL) on your model/DTO class to remove.

Like

@JsonInclude(Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown=true)
public class Video  {
    //the properties
}

1 Comment

Use Jackson API it is a bit extrem to redirect to a different API (if I am not mistaken, this doesn't look like Jackson) just to remove Null value ;-)
0

thx!

Here is an improved regex expression that also works for an formatted and unformatted JSON string:

 str.replaceAll(" *\"\\w+\"*:(\\s)?null(,)?(\\r)?(\\n)?", "");

Comments

-2

I used it like this and it worked for me.

@JsonInclude(value = Include.NON_NULL)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.