blahblah blahblah - 3 months ago 12
JSON Question

PHP json_decode error: Array value found, but an object is required

I am trying to decode following JSON file

{
"email": "mail@gmail.com",
"password": "12345",
"languageProficiency": {
"language": "English",
"proficiency": 4
},
"tags": [
{
"name": "singing"
},
{
"name": "dance"
}
]
}


When I do this

$data = json_decode($jsonContent, true);
echo $data;
die();


I have following error:

Array value found, but an object is required

Question

1) How can I view data from JSON
2) How can I access property of each object in tags array

I am validating Json content againsts this schema

{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"email": {
"type": "string"
},
"password": {
"type": "string"
},
"languageProficiency": {
"type": "object",
"properties": {
"language": {
"type": "string"
},
"proficiency": {
"type": "integer"
}
},
"required": [
"language",
"proficiency"
]
},
"tags": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string"
}
},
"required": [
"name"
]
}
}
},
"required": [
"email",
"password",
"languageProficiency",
"tags"
]
}


UPDATE

I tried following to view json content

print_r($data)


But I still get same error

Answer

$data = json_decode($jsonContent, true);

Above line of code will return Array, for which you can not use echo directly to print an array.

To print specific value of an array (e.g. email), do it like this:

echo $data["email"];

Tip:

Use print_r() to know the array structure like this,

echo "<pre>";
print_r($data);
Comments