How to validate a json if there are null parameters in java

0

I wanted to ask you how I can validate using the if you execute certain actions, what happens is that in a json come null values when I want to set the records that are in the BD, set so to say the records that the user inserted ( edit) this is my java code:

if (notificacion.getJson_notif_config() != null) {
            accVenEmer = true;
            booMiniMap = true;
            booleanAcciones();

            Gson gson = new Gson();
            JsonObject jsonObject = gson.fromJson(notificacion.getJson_notif_config(), JsonObject.class);
            System.out.println("Que trae: " + jsonObject);
            if (jsonObject.get("url") != null) {
                stringSonido = jsonObject.get("url").getAsString();
                System.out.println("Sonido " +jsonObject);
                if (jsonObject.get("color") != null) {
                    switch (jsonObject.get("color").getAsString()) {
                        case "FF0000":
                            selectedColor1();
                            color1 = true;
                            break;

                        case "FF8000":
                            selectedColor2();
                            color2 = true;
                            break;
                        case "00994C":
                            selectedColor3();
                            color3 = true;
                            break;

                        case "009999":
                            selectedColor4();
                            color4 = true;
                            break;
                        case "0080FF":
                            selectedColor5();
                            color5 = true;
                            break;
                        case "6666FF":
                            selectedColor6();
                            color6 = true;
                            break;
                        case "FF3399":
                            selectedColor7();
                            color7 = true;
                    }

                }

            }
        }

It works only if no null comes in the json, but when I edit another one that if it comes null, it generates this error:

Que trae: {"url":null,"color":"00994C","minimapa":true}
07-Sep-2018 18:08:09.251 ADVERTENCIA [http-nio-8080-exec-648] com.sun.faces.lifecycle.InvokeApplicationPhase.execute /Notificaciones/notificaciones.xhtml @380,262 actionListener="${notificationsBean.selectEditNotificacion(notificacion)}": java.lang.UnsupportedOperationException: JsonNull
 javax.el.ELException: /Notificaciones/notificaciones.xhtml @380,262 actionListener="${notificationsBean.selectEditNotificacion(notificacion)}": java.lang.UnsupportedOperationException: JsonNull
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:111)
    at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:147)
    at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
    at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:813)
    at javax.faces.component.UICommand.broadcast(UICommand.java:300)
    at javax.faces.component.UIData.broadcast(UIData.java:1108)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at Seguridad.AuthorizationFilter.doFilter(AuthorizationFilter.java:43)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:475)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:625)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:498)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:796)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1372)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.UnsupportedOperationException: JsonNull
    at com.google.gson.JsonElement.getAsString(JsonElement.java:191)
    at avo.ob.notifications.NotificationsBean.selectEditNotificacion(NotificationsBean.java:782)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.el.parser.AstValue.invoke(AstValue.java:247)
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:267)
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
    ... 36 more

And the one that comes full would be something like this:

Que trae: {"url":"sonid intervalo","color":"00994C","minimapa":true}

As well I mentioned in the part above that json comes a Null, the question is: Is there any way I can force that execution? what happens is that it comes from a field that is not obligatory for the user to insert it, and that is why it is expressed null, I hope and can give me to understand, I remain alert, good afternoon.

    
asked by cratus666jose 08.09.2018 в 01:12
source

3 answers

1

The error is caused precisely because the value you are trying to obtain does not exist in the json.

  

Caused by: java.lang.UnsupportedOperationException: JsonNull       at com.google.gson.JsonElement.getAsString (JsonElement.java:191)

Validating null values is done using the isNull () method

 //if (jsonObject.get("url") != null) {
  if(!jsonObject.isNull("url")) {
  

isNull () : Returns true if the value in the specified location   in this array is JsonValue.NULL.

    
answered by 08.09.2018 в 01:38
0

You can use these two functions:

With this function you check if the "url" attribute exists

objJson.has("url")

And with this function you check if it's null

objJson.isNull("url")
    
answered by 08.09.2018 в 01:32
0

I was able to solve this problem to some extent, what it was to do another if, one for the null, and another for those who did not, that's how it was:

if (jsonObject.isJsonNull()) {
                stringSonido = jsonObject.get("url").getAsString();
                System.out.println("Sonido " + jsonObject);
            }

            if (jsonObject.get("color") != null) {
                switch (jsonObject.get("color").getAsString()) {
                    case "FF0000":
                        selectedColor1();
                        color1 = true;
                        break;

                    case "FF8000":
                        selectedColor2();
                        color2 = true;
                        break;
                    case "00994C":
                        selectedColor3();
                        color3 = true;
                        break;

                    case "009999":
                        selectedColor4();
                        color4 = true;
                        break;
                    case "0080FF":
                        selectedColor5();
                        color5 = true;
                        break;
                    case "6666FF":
                        selectedColor6();
                        color6 = true;
                        break;
                    case "FF3399":
                        selectedColor7();
                        color7 = true;
                }

            }

If there is any other way, I would be grateful if you told me please, but if it does not cause conflicts, greetings and good vibes.

    
answered by 10.09.2018 в 18:13