-1

Here is my json data:

      {
      "resourceType": "Patient",
      "birthDate": "1985-08-01T00:00:00Z",
      "active": true,
      "gender": "male",
      "deceasedBoolean": false,
      "id": "Tbt3KuCY0B5PSrJvCu2j-PlK.aiHsu2xUjUM8bWpetXoB",
      "careProvider": [
        {
          "display": "Physician Family Medicine",
          "reference": "https://open-ic.epic.com/FHIR/api/FHIR/DSTU2/Practitioner/T3Mz3KLBDVXXgaRoee3EKAAB"
        }
      ],
      "name": [
        {
          "use": "usual",
          "family": [
            "Argonaut"
          ],
          "given": [
            "Jason"
          ]
        }
      ],
      "identifier": [
        {
          "use": "usual",
          "system": "urn:oid:1.2.840.114350.1.13.327.1.7.5.737384.0",
          "value": "E3826"
        },
        {
          "use": "usual",
          "system": "urn:oid:1.2.3.4",
          "value": "203579"
        }
      ],
      "address": [
        {
          "use": "home",
          "line": [
            "1979 Milky Way Dr."
          ],
          "city": "Verona",
          "state": "WI",
          "postalCode": "53593",
          "country": "US"
        }
      ],
      "telecom": [
        {
          "system": "phone",
          "value": "608-271-9000",
          "use": "home"
        },
        {
          "system": "phone",
          "value": "608-771-9000",
          "use": "work"
        },
        {
          "system": "email",
          "value": "[email protected]"
        }
      ],
      "maritalStatus": {
        "text": "Married"
      },
      "communication": [
        {
          "preferred": true,
          "language": {
            "text": "English",
            "coding": [
              {
                "system": "urn:oid:2.16.840.1.113883.6.99",
                "code": "en",
                "display": "English"
              }
            ]
          }
        }
      ],
      "extension": [
        {
          "url": "http://hl7.org/fhir/StructureDefinition/us-core-race",
          "valueCodeableConcept": {
            "text": "Asian",
            "coding": [
              {
                "system": "2.16.840.1.113883.5.104",
                "code": "2028-9",
                "display": "Asian"
              }
            ]
          }
        },
        {
          "url": "http://hl7.org/fhir/StructureDefinition/us-core-ethnicity",
          "valueCodeableConcept": {
            "text": "Not Hispanic or Latino",
            "coding": [
              {
                "system": "2.16.840.1.113883.5.50",
                "code": "2186-5",
                "display": "Not Hispanic or Latino"
              }
            ]
          }
        }
      ]
    }
2
  • Can you share what you've tried? Commented Mar 29, 2016 at 13:00
  • $json = '{.....}'; $obj= json_decode($json,true); for($i=0; $i<count($obj['extension']); $i++) { echo $obj['extension'][$i]["url"]. " <br /> "; } but unable to read inner loop for valueCodeableConcept, please suggest me how to read json inner key & values Commented Mar 29, 2016 at 13:07

4 Answers 4

2

json_decode() - Decodes a JSON string

Example:

<?php
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';

var_dump(json_decode($json));
var_dump(json_decode($json, true));

?>

Output:

object(stdClass)#1 (5) {
    ["a"] => int(1)
    ["b"] => int(2)
    ["c"] => int(3)
    ["d"] => int(4)
    ["e"] => int(5)
}

array(5) {
    ["a"] => int(1)
    ["b"] => int(2)
    ["c"] => int(3)
    ["d"] => int(4)
    ["e"] => int(5)
}
Sign up to request clarification or add additional context in comments.

2 Comments

please check my above json object and suggest me how to read nested json objects.for example in the above json object how to read communication key and its inner values (language and its inner loop ( coding key))
2

use json_decode. It returns a PHP object from a well-formed JSON string

Assuming the string in your question is stored under a variable called $json_str, the decoding should go something like this

$json_obj = json_decode($json_str);

You may then access it like so

$json_obj->resourceType; // "Patient"

1 Comment

$json = '{.....}'; $obj= json_decode($json,true); for($i=0; $i<count($obj['extension']); $i++) { echo $obj['extension'][$i]["url"]. " <br /> "; } but unable to read inner loop for valueCodeableConcept, please suggest me how to read json inner key & values
1

You can use the PHP JSON library: http://php.net/manual/en/function.json-decode.php

Comments

0

Try as below

$obj= json_decode($json,true);
if(!empty($obj->extension))
{
    foreach($obj->extension as $extension)
    {
         echo  $extension->url;
       // similarly others
     }
}

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.