0

I have the following json :-

{
    "firstName": "Jhon",
    "lastName": "Doe",
    "username": "jhon",
    "avatar": "localhost/uploads/avatars/default.jpg",
    "language": "ar",
    "birth_date": "2017-11-22 00:00:00",
    "weight_chart": [],
    "health_status": {
        "id": 130,
        "user_id": 258,
        "weight": 95,
        "height": 171,
    },

I decoded the above json

$user = json_decode($response);

Now i am able to print the firstname by using: $user->firstName

My questions is :-

  • Can i access the json values without decoding it ?
  • How can i access the "health_status" values id, weight ... ?

1 Answer 1

2

Your json is malformed, try this structure:

$str = '{
    "firstName": "Jhon",
    "lastName": "Doe",
    "username": "jhon",
    "avatar": "localhost/uploads/avatars/default.jpg",
    "language": "ar",
    "birth_date": "2017-11-22 00:00:00",
    "weight_chart": [],
    "health_status": {
        "id": 130,
        "user_id": 258,
        "weight": 95,
        "height": 171
    }
}';

$obj = json_decode($str);
echo $obj->firstName.' - ';
echo $obj->health_status->id.' - ';
echo $obj->health_status->weight; 

response: Jhon - 130 - 95

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

2 Comments

The json i am getting is from some api web services not a json file , i cannot control the structure of the json .
@user2873860 Your JSON document in the question is malformed. If it's just a typo because you hand-wrote/edited it you should just admit/fix that so everyone can move on.

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.