Go through JSON and differentiate between null and empty fields

0

I'm doing a script in PHP, which allows me to run a JSON to make debug when any of the tags is null, it's a problem is that when you convert the JSON to an array, the data that was empty becomes and remains as null . I attach the code below:

This is the JSON:

$json = "{
"Comprobante": {
    "TipoComprobante": "XXXXXXXXXXXXXXXXX",
    "Fecha": "2099-12-12",
    "Serie": "XXXXXXXXXXXXXXXXX",
    "Folio": "XXXXXXXXXXXXXXXXX",
    "Moneda": "XXXXXXXXXXXXXXX",
    "Referencia": "",
    "ConceptoRef": "",
    "Descripcion": [
        {
            "Nombre": "Observaciones",
            "Valor": "XXXXXXXXXXXXXX"
        }
    ],
    "MetodoPago": [
        {
            "Codigo": "XX",
            "Valor": "XXXXXXXXXXXXXXXXX"
        }
    ]
},
"Emisor": {
    "Identificacion": "XXXXXXXXXXXXXXXXX",
    "TipoIdentificacion": "XXXXXXXXXXXXXXXXX",
    "RazonSocial": "XXXXXXXXXXXXXXXXX",
    "NombreComercial": "XXXXXXXXXXXXXXXXX",
    "Direccion": "XXXXXXXXXXXXXXXXX",
    "Pais": "XXXXXXXXXXXXXXXXX",
    "email": "XXXXXXXXXXXXXXXXX",
    "Department": "XXXXXXXXXXXXXXXXX",
    "CitySubdivisionName": "XXXXXXXXXXXXXXXXX",
    "CityName": "",
    "Descripcion": [
        {
            "Nombre": "Tipo De Regimen",
            "Valor": "XXXXXXXXXXXXXXXXX"
        }
    ]
},
"Receptor": {
    "Identificacion": " XXXXXXXXXXXXXXXXX",
    "TipoIdentificacion": null,
    "RazonSocial": "XXXXXXXXXXXXXXXXX",
    "NombreComercial": "XXXXXXXXXXXXXXXXX",
    "Direccion": "XXXXXXXXXXXXXXXXX",
    "Pais": "XXXXXXXXXXXXXXXXX",
    "email": "",
    "Department": "XXXXXXXXXXXXXXXXX",
    "CitySubdivisionName": "XXXXXXXXXXXXXXXXX",
    "CityName": "",
    "Descripcion": [
        {
            "Nombre": "Sector Empresarial",
            "Valor": "XXXXXXXXXXXXXXXXX"
        }
    ]
},
"Detalles": [
    {
        "Nombre": "XXXXXXXXXXXXXXXXX",
        "Cantidad": "1.00",
        "ValorUnitario": XXXXXXXXXXXXXXXXX,
        "Subtotal": XXXXXXXXXXXXXXXXX,
        "Total": XXXXXXXXXXXXXXXXX00,
        "Codigo": XXXXXXXXXXXXXXXXX,
        "Impuestos": "",
        "Descripcion": [
            {
                "Nombre": "Descuento",
                "Valor": 0
            },
            {
                "Nombre": "Observaciones",
                "Valor": ""
            }
        ]
    }
],
"Totales": {
    "Total": XXXXXXXXXXXXXXXXX,
    "SubTotal": XXXXXXXXXXXXXXXXX,
    "Impuestos": []
},
"DetallesComprobante": [
    {
        "Nombre": "Fecha Vencimiento",
        "Valor": "2099-12-31"
    }
]}";

PHP code:

$debugJSON = json_decode($json);
foreach($debugJSON as $primerIndice => $primerValor){
    foreach($primerValor as $segundoIndice => $segundoValor){
        if($segundoValor == null){
            echo "El tag contenido en $primerIndice => $segundoIndice esta vacio o nulo<br>";
                }
            }
        }
    
asked by David Espinal 30.11.2017 в 15:03
source

2 answers

1

The json_decode does distinguish the voids of the null:

<?php
$json = '{
"Comprobante": {
    "TipoComprobante": "XXXXXXXXXXXXXXXXX",
    "Fecha": "2099-12-12",
    "Referencia": null,
    "ConceptoRef": ""

}}';

$decoded=json_decode($json);

var_dump($decoded);

Your problem is that you are using comparison == instead of === therefore both sides of the comparison are casted implicitly. You can try the command line ( php -a )

php > var_dump('' == null);
bool(true)
php > var_dump('' === null);
bool(false)
    
answered by 30.11.2017 / 15:42
source
0

Good day, to solve what you mention it is necessary to validate with the tripe operator equal to verify both value and type of data "===", the following is an example of what I mention:

$myArr = array("John",
               null,
               "",
               array("John", null, "", "Sally"),
               "Sally");

$myJSON = json_encode($myArr);

echo "<pre>";
print_r($myJSON);
echo "</pre>";

$debugJSON = json_decode($myJSON);

echo "<pre>";
print_r($debugJSON);
echo "</pre>";


foreach($debugJSON as $primerIndice => $primerValor){
    foreach($primerValor as $segundoIndice => $segundoValor){
            if($segundoValor === null){
                echo "El tag contenido en $primerIndice => $segundoIndice esta nulo<br>";
            }else{
                if($segundoValor == ''){
                    echo "El tag contenido en $primerIndice => $segundoIndice esta con valores vacios<br>";
                }
            }           
        }

   }

It would already be implemented according to what is presented in the structure of your Json and in that way you identify if it is null or if it is empty by differentiating them. I hope you find it useful, greetings.

    
answered by 30.11.2017 в 15:43