1

I am trying to write this using php and json_encode

{"patient": {"demographics": {}}

}

This is the Array I am using with json encode.

Array("patient" =>  Array("demographics" =>  Array()))

When I echo the output, this is what I get:

{"patient":{"demographics":[]}}

I really think this is a stupid mistake ob my part. All help is appreciated.

2 Answers 2

9

try

json_encode($your_array, JSON_FORCE_OBJECT)

as per the docs: http://php.net/json_encode

By default, php arrays stay arrays ([]) when json'd, unless there's a single non-numeric key, in which case it'll be an object

Sign up to request clarification or add additional context in comments.

Comments

0

You can also try this:

json_encode(Array("patient" =>  Array("demographics" => new stdClass)));

This way you can have empty arrays and empty objects in the same JSON, which would not be possible using the other answer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.