MrTechie
MrTechie

Reputation: 1847

How to properly format a json encoded array?

I am trying to create a json encoded array that results like this:

{
    "properties": [
        {
            "property": "email",
            "value": "[email protected]"
        }
    ]
}

This is my code:

$data = array("properties"=>array("property"=>"status", "value"=> "Pending Approval"));   
$data_string = json_encode($data, true); 

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

Which then gives me the results of:

{
    "properties":{
        "property":"status",
        "value":"Pending Approval"
    }
}

But yet the API then responds back after a curl method that states:

Json node is missing child property

Why is the child property missing? It's been defined - what am I missing on this?

Upvotes: 1

Views: 706

Answers (1)

Jonathan Kuhn
Jonathan Kuhn

Reputation: 15301

you are missing an array inside the properties key.

$data = array(
    "properties" => array(
        array("property" => "status", "value" => "Pending Approval"),
    )
);
//....rest of your code

properties in json appears to be an array of objects. You were providing a single object, not an array of objects.

Upvotes: 6

Related Questions