0

I am trying to parse the data from the following json

{
"id":"1",
"rawId":"1",
"displayName":"Distress Number",
"name":{"familyName":"Number","formatted":"Distress Number","givenName":"Distress"},
"nickname":null,
"phoneNumbers":[{"type":"other","value":"112","id":"2","pref":false}],
"emails":[{"type":"other","value":"[email protected]","id":"19","pref":false}],
"addresses":null,
"ims":null,"organizations":null,
"birthday":null,
"note":"","photos":null,
"categories":null,
"urls":null
}

I want to insert the displaName,phoneNumbers and emails to database table but its failed Here is my php

$contact=json_decode($json,true){
    foreach($contact as $con){

         $num=$con['phoneNumbers']['value'];
        $name=$con['displayName'];
        $email=$con['emails']['value'];
         //function to dbinsert 
        insertToDb($num,$name,$email);
     }

But it returns illegal offset warning

2
  • stackoverflow.com/questions/14425700/… Commented Mar 4, 2015 at 10:00
  • I don't understand how your data are structured. Is there only one record? Commented Mar 4, 2015 at 10:15

2 Answers 2

0

It should be like this , phoneNumbers and emails are again the json array you need to parse

        $contact=json_decode($json,true);
        foreach($contact as $con){
        $num=$con['phoneNumbers'][0]['value'];
        $name=$con['displayName'];
        $email=$con['emails'][0]['value'];
     //function to dbinsert 
        insertToDb($num,$name,$email);
        }
Sign up to request clarification or add additional context in comments.

Comments

0

You are calling json_decode in the wrong way. Try to do like this:

$contact=json_decode($json,true);
foreach($contact as $con) {

    $num=$con['phoneNumbers']['value'];
    $name=$con['displayName'];
    $email=$con['emails']['value'];

    insertToDb($num,$name,$email);
 }

2 Comments

I think this json_decode($json,true) { is incorrect. The bracket in particular
i wiil get displayName but not $num and $name

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.