0

I have tried the this code: $record = $record['data']->Company; but it is always returned as null. I have also tried $record = $record[1]->data->Company; which also results in a null value. I have read so many answers and tried the solutions but nothing works for me.

Some more experience than me please have a look and let me know where i am going wrong.

Thanks in advance

{
    "1": {
        "index": 1,
        "data": {
            "CONTACTID": "3345923000000546002",
            "SMOWNERID": "3345923000000158021",
            "Contact Owner": "Frank Rosa",
            "First Name": "Administrator",
            "Last Name": "Ian",
            "Email": "[email protected]",
            "Created Time": "2018-09-19 14:32:35",
            "Modified Time": "2018-09-20 02:48:51",
            "Full Name": "Administrator Ian",
            "Description": "Equity and Empowerment through Education. It is the Mission of 3e LLC to promote equity and empowerment for all students through engaging professional development for educators and parents, one-on-one coaching for teacher efficacy, and mentoring services for youth to promote enrichment and success. For the empowered, we offer editing, transcribing, and ghostwriting services to ensure your voice is heard.",
            "Last Activity Time": "2018-09-20 02:48:51",
            "Instagram Url": "http://www.instagram.com/3e_llc",
            "Company": "3ELLC",
            "Website": "https://www.3ellc.org",
            "Phone_1": "(727) 420-1050",
            "Full Address": "2152 Arcadia Rd, Holiday, FL 34690, USA",
            "Facebook Url": "http://www.facebook.com/3eLLC/",
            "Logo Url": "https://dev.energypages.com/wp-content/uploads/2018/05/header-logo-57.png",
            "Twitter Url": "http://www.twitter.com/3e_llc",
            "Membership Level": "Basic",
            "Select Service": "Technology",
            "User ID": "347"
        }
    }
}

I am getting the json from my function below:

function webhook_listener($request_data){
    $client = new ZohoCRMClient('Contacts', 'API KEY HERE');

    $parameters = $request_data->get_params();

    if( !isset( $parameters['contactId'] ) || empty($parameters['contactId']) ){
        file_put_contents(plugin_dir_path( __FILE__ ).'invalid.txt', 'No parameter found');
        return array( 'error' => 'no_parameter_given' );
    }else{
        $companyid = $parameters['contactId'];

        file_put_contents(plugin_dir_path( __FILE__ ).'crm.txt', $parameters);

        $record = $client->getRecordById()->id($companyid)->request();

        wp_mail( '[email protected]', 'Post Data', $record );

        $payload = json_decode($record, true);

        $payload = $payload[1]['data']['Company'];

        file_put_contents(plugin_dir_path( __FILE__ ).'record.txt', $payload);

       return $payload;
    }
}
2
  • Unfortunately, it's really tough to get object properties where the property name is a number. It might be best to decode it into an array instead of an object. $record = json_decode($str, true); Commented Sep 20, 2018 at 13:46
  • @aynber when i use json_decode i also receive a null value Commented Sep 20, 2018 at 13:59

4 Answers 4

1

It works for me:

<?php
$var = json_decode('{
    "1": {
        "index": 1,
        "data": {
            "CONTACTID": "3345923000000546002",
            "SMOWNERID": "3345923000000158021",
            "Contact Owner": "Frank Rosa",
            "First Name": "Administrator",
            "Last Name": "Ian",
            "Email": "[email protected]",
            "Created Time": "2018-09-19 14:32:35",
            "Modified Time": "2018-09-20 02:48:51",
            "Full Name": "Administrator Ian",
            "Description": "Equity and Empowerment through Education. It is the Mission of 3e LLC to promote equity and empowerment for all students through engaging professional development for educators and parents, one-on-one coaching for teacher efficacy, and mentoring services for youth to promote enrichment and success. For the empowered, we offer editing, transcribing, and ghostwriting services to ensure your voice is heard.",
            "Last Activity Time": "2018-09-20 02:48:51",
            "Instagram Url": "http://www.instagram.com/3e_llc",
            "Company": "3ELLC",
            "Website": "https://www.3ellc.org",
            "Phone_1": "(727) 420-1050",
            "Full Address": "2152 Arcadia Rd, Holiday, FL 34690, USA",
            "Facebook Url": "http://www.facebook.com/3eLLC/",
            "Logo Url": "https://dev.energypages.com/wp-content/uploads/2018/05/header-logo-57.png",
            "Twitter Url": "http://www.twitter.com/3e_llc",
            "Membership Level": "Basic",
            "Select Service": "Technology",
            "User ID": "347"
        }
    }
}');

foreach($var as $item) {
    echo $item->data->Company . '<br>';
}
?>

The problem was in the key "1", you can't access it like that $var->1, but you can iterate it in an array.

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

1 Comment

Works perfectly
0

You may try to do something like this:

<?php

$json = '{
    "1": {
        "index": 1,
        "data": {
            "CONTACTID": "3345923000000546002",
            "SMOWNERID": "3345923000000158021",
            "Contact Owner": "Frank Rosa",
            "First Name": "Administrator",
            "Last Name": "Ian",
            "Email": "[email protected]",
            "Created Time": "2018-09-19 14:32:35",
            "Modified Time": "2018-09-20 02:48:51",
            "Full Name": "Administrator Ian",
            "Description": "Equity and Empowerment through Education. It is the Mission of 3e LLC to promote equity and empowerment for all students through engaging professional development for educators and parents, one-on-one coaching for teacher efficacy, and mentoring services for youth to promote enrichment and success. For the empowered, we offer editing, transcribing, and ghostwriting services to ensure your voice is heard.",
            "Last Activity Time": "2018-09-20 02:48:51",
            "Instagram Url": "http://www.instagram.com/3e_llc",
            "Company": "3ELLC",
            "Website": "https://www.3ellc.org",
            "Phone_1": "(727) 420-1050",
            "Full Address": "2152 Arcadia Rd, Holiday, FL 34690, USA",
            "Facebook Url": "http://www.facebook.com/3eLLC/",
            "Logo Url": "https://dev.energypages.com/wp-content/uploads/2018/05/header-logo-57.png",
            "Twitter Url": "http://www.twitter.com/3e_llc",
            "Membership Level": "Basic",
            "Select Service": "Technology",
            "User ID": "347"
        }
    }
}';
$payload = json_decode($json, true);
$error = json_last_error();
if ($error !== JSON_ERROR_NONE) {
    throw new RuntimeException("JSON decode error: $error");
}
var_dump($payload[1]['data']['Company']);

As result you will see:

string(5) "3ELLC"

In case you don't need associative array but class, try this:

$payload = json_decode($json);
$error = json_last_error();
if ($error !== JSON_ERROR_NONE) {
    throw new RuntimeException("JSON decode error: $error");
}
var_dump($payload->{"1"}->data->Company);

Result will be the same.

2 Comments

So i ran your answer in postman and the value returned was null
I should also mention i am using the code in a function
0
  1. You have to decode the record like this.

    $array = json_decode($record, true);

  2. Then you can access the data like this.

    echo $array['1']['data']['Company'];

Checkout it out here, https://www.tehplayground.com/UpHMPFxfSKdxXBmn

Have a good day!

6 Comments

Try this out , if it doesn't work I'll try to help you.
Please format your question as per the SO standards.
You should print the $record before you try to decode it and check if there is any data there.
@urreeK in my function i return $record and i receive the json in my post, but trying to get the value from the json it is always null.
what do you get if you var_dump($record) ?
|
0

Having the json resource in the data.json file

$json = file_get_contents('/tmp/data.json');

Or in a string

$json = '{
    "1": {
        "index": 1,
        "data": {
            "CONTACTID": "3345923000000546002",
            "SMOWNERID": "3345923000000158021",
            "Contact Owner": "Frank Rosa",
            "First Name": "Administrator",
            "Last Name": "Ian",
            "Email": "[email protected]",
            "Created Time": "2018-09-19 14:32:35",
            "Modified Time": "2018-09-20 02:48:51",
            "Full Name": "Administrator Ian",
            "Description": "Equity and Empowerment through Education. It is the Mission of 3e LLC to promote equity and empowerment for all students through engaging professional development for educators and parents, one-on-one coaching for teacher efficacy, and mentoring services for youth to promote enrichment and success. For the empowered, we offer editing, transcribing, and ghostwriting services to ensure your voice is heard.",
            "Last Activity Time": "2018-09-20 02:48:51",
            "Instagram Url": "http://www.instagram.com/3e_llc",
            "Company": "3ELLC",
            "Website": "https://www.3ellc.org",
            "Phone_1": "(727) 420-1050",
            "Full Address": "2152 Arcadia Rd, Holiday, FL 34690, USA",
            "Facebook Url": "http://www.facebook.com/3eLLC/",
            "Logo Url": "https://dev.energypages.com/wp-content/uploads/2018/05/header-logo-57.png",
            "Twitter Url": "http://www.twitter.com/3e_llc",
            "Membership Level": "Basic",
            "Select Service": "Technology",
            "User ID": "347"
        }
    }
}';

Try the following

$record = (array) json_decode($json);

print_r($record[1]->data->Company);

Output

3ELLC

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.