Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

552
Vistas
Java 8 - working with JsonObject (javax.json) - how to determine if a node has child nodes?

I've just started using javax.json package. I know how to extract values from JSON object in Java 8 if I know the structure of my JSON string. But how about strings I don't know the structure of? The question: how to determine if a node has child nodes?

To read values, I simply need to use "get*" methods - it works OK, but there's no method like "checkIfArray" or "checkIfObject" to check if I even can use methods like "getString"...

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

In javax.json package, both available types support emptiness checks because they both implement a java.collection interfaces:

JsonObject is-a java.util.Map<String, JsonValue>. As a result, one can check if such object contains any values by simply calling isEmpty() method.

JsonArray is-a java.util.List<JsonValue>. As a result, one - again - can check if the array is empty by calling isEmpty() method on it.

For traversing a tree of JsonStructures while marking all empty nodes, you can use this helper method:

boolean isValueEmpty(JsonValue v) {
  if (v == null) {
    return true; // or you may throw an exception, for example
  }
  switch(v.getValueType()) {
    case NULL:
      return true; // same as with Java null, we assume that it is Empty
    case ARRAY: 
      return ((JsonArray) v).isEmpty();
      // additionally, you can say that empty array is array with only empty elements
      // this means a check like this:
      // return ((JsonArray v).stream().allMatch(isValueEmpty); // recurse
    case OBJECT:
      return ((JsonObject v).isEmpty();
    case STRING:
      // optionally: if you want to treat '' as empty
      return ((JsonString v).getString().isEmpty();
    default:
      return false; // to my knowledge, no other Json types can be empty
  }
}
about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda