Error reading JSON with PHP

2

I'm trying to read a JSON file in php in the following format:

{"leads":
  [{"id":"1",
    "email":"[email protected]",
    "user": "[email protected]",
    "first_conversion": {
      "content": {
        "identificador":"ebook-abc",
      },
      "created_at":"2012-06-04T15:31:35-03:00",
      "conversion_origin": {
            "source": "source 1",
          }
    },
    "last_conversion": {
      "content": {
        "identificador":"webinar-abc",
        "email_lead":"[email protected]"
      },
      "created_at":"2012-06-04T15:31:35-03:00",
      "cumulative_sum":"2",
    },
    "custom_fields": {
        "Destino": "EUA"
      },
    "website": "http://www.site.com.br",
    "mobile_phone":"48 30252598",
    "city":"Sao Paulo",
    "state": "SP",
    "tags": ["tag 1", "tag 2"],
  }]
}

I tried to return the results in php in 2 formats. The first one is as follows:

$request = file_get_contents('php://input');
$input = json_decode($request, true);
$id = $input['leads']['nome'];
echo $id;

And the second way using foreach:

$request = file_get_contents('php://input');
$input = json_decode($request, true);
$lead = $input->leads;
foreach($lead as $result){
   echo "ID: ". $result->id;
}

Neither way is returning a result. Can someone help me and tell me where I'm going wrong ?! Thanks: D

    
asked by anonymous 16.08.2017 / 17:15

3 answers

3

There are some errors in your json which cause PHP to return null when you use json_decode , errors are the commas in the last elements of each objeto , eg

"conversion_origin": {
    "source": "source 1",
}


"last_conversion": {
    "content": {
        "identificador": "webinar-abc",
        "email_lead": "[email protected]"
    },
    "created_at": "2012-06-04T15:31:35-03:00",
    "cumulative_sum": "2", // <-- virgula ao final
},

To verify that json is valid, you can use JSONLint .

  

The last element in a list can not contain a comma in json

In addition, also see that the index leads is a array , this way you need to enter the index number to access it as array and as an object, see:

// Utilizando array
$json = json_decode($raw, true);
var_dump($json['leads'][0]['id']); // Saida: string(1) "1"

// Utilizando objeto
$obj = json_decode($raw);
var_dump($obj->leads[0]->id); // Saida: string(1) "1"

Using an iteration

foreach($obj->leads as $lead) {
   echo $lead->id;
}

OU:

foreach($json['leads'] as $lead) {
    echo $lead['id'];
}
    
16.08.2017 / 17:44
3

I downloaded your code and tested here, at least the error I identified was that following the format of your json is giving the comma in the last items on your list.

Ex: (wrong)

$json = '{
      "id":[{
          "nome":"nome de teste",
          "sobrenome": "sobrenome aqui", //no caso do json a última virgula no último item não deve existir
      }]
}';

Ex: (correct)

$json = '{
      "id":[{
          "nome":"nome de teste",
          "sobrenome": "sobrenome aqui" //vírgula removida
      }]
}';

I tested its format with these modifications I mentioned above.

$json = '{
    "leads":[{"id":"1",
    "email":"[email protected]",
    "user": "[email protected]",
    "first_conversion": {
      "content": {
        "identificador":"ebook-abc"
      },
      "created_at":"2012-06-04T15:31:35-03:00",
      "conversion_origin": {
            "source": "source 1"
          }
    },
    "last_conversion": {
      "content": {
        "identificador":"webinar-abc",
        "email_lead":"[email protected]"
      },
      "created_at":"2012-06-04T15:31:35-03:00",
      "cumulative_sum":"2"
    },
    "custom_fields": {
        "Destino": "EUA"
      },
    "website": "http://www.site.com.br",
    "mobile_phone":"48 30252598",
    "city":"Sao Paulo",
    "state": "SP",
    "tags": ["tag 1", "tag 2"]
  }]
}';

$converte = json_decode($json, true);
print_r($converte);
    
16.08.2017 / 17:45
2

Your code is correct. The error is in json content.

The last element in a list can not contain a comma in json as it does in PHP. If you use the block below, you will find that your first code works perfectly.

{"leads": [
    {
        "id": "1",
        "email": "[email protected]",
        "user": "[email protected]",
        "first_conversion": {
            "content": {
                "identificador": "ebook-abc"
            },
            "created_at": "2012-06-04T15:31:35-03:00",
            "conversion_origin": {
                "source": "source 1"
            }
        },
        "last_conversion": {
            "content": {
                "identificador": "webinar-abc",
                "email_lead": "[email protected]"
            },
            "created_at": "2012-06-04T15:31:35-03:00",
            "cumulative_sum": "2"
        },
        "custom_fields": {
            "Destino": "EUA"
        },
        "website": "http://www.site.com.br",
        "mobile_phone": "48 30252598",
        "city": "Sao Paulo",
        "state": "SP",
        "tags": [
            "tag 1",
            "tag 2"
        ]
    }
]}
    
16.08.2017 / 17:43