1

I read several posts on here and have been unable to find success in resolving the problem I am having.

I am trying to figure out how to get the NAME field from the UploadImage array.

I have the following JSON being passed to me from a Webhook.

{
"$version":5,
"Entry":{
    "Number":"11",
    "Order":null,
    "Origin":
        {
        "City":"Portland",
        "CountryCode":"US",
        }
        ,
"Message":"the message",
"UploadImage":[
    {
        "ContentType":"image/png",
        "Id":"F-lMbiCYdwiYS8ppkQS4gsyE",
        "Name":"Screen.png",
        "Size":55907
    }
            ],
"Subject":"here is the subject" 
}

I have no problem grabbing the value of Subject or Message, but I cannot figure out how to grab the NAME within UploadImage.

  $contact = json_decode($json);  
  $subject=$contact->{'Subject'};

When I do

    $uploadimage=$contact->{'UploadImage'};  

it just writes out ARRAY.

I can do

 echo  $contact->{'Entry'}->{'Number'}; 

and it works, so it has to be something with the bracket being there before the curly bracket. I know this has to be something simple that I am missing. Any help is GREATLY appreciated.

7
  • what is your question ? Commented Mar 24, 2016 at 17:24
  • "...but I cannot figure out how to grab the NAME within UploadImage". Commented Mar 24, 2016 at 17:25
  • "I can do echo $contact->{'Entry'}->{'Number'}; and it works, Commented Mar 24, 2016 at 17:26
  • Sorry for any confusion. The Entry/Number is not what I need, and it doesn't have the bracket within the array like the UploadImage/Name does. It's the latter that I am trying to retrieve. Commented Mar 24, 2016 at 17:28
  • $contact->UploadImage[0]->Name alternatively: $contact = json_decode($json, true); $name = $contact['UploadImage'][0]['Name']; Commented Mar 24, 2016 at 17:28

6 Answers 6

1
$uploadimage=$contact->{'UploadImage'}[0]->{'Name'};
Sign up to request clarification or add additional context in comments.

1 Comment

This was it. Thanks!! I tried every combination I think BUT this one. Thanks again!
1

Firstly try

$contact = json_decode($json, true);

Adding the second argument returns an array instead of an object which will make things easier. Objects with numerical keys are troublesome... Now you can,

print_r($contact);

to see exactly what you've got. I imagine that

echo $contact['UploadImage'][0]['Name'];

Will get you what you're looking for.

Notice that UploadImage contains an array of objects (or an array of arrays after conversion).

1 Comment

agree with print_r part, it really shows you the path :)
1

another solution is:

$contact = json_decode($text);
$name = '';
foreach($contact->UploadImage as $k=>$v){
    foreach($v as $k2=>$v2){
        echo $k2.' - '.$v2.'<br />';
        if($k2=='Name'){ $name = $v2;}
    }

};
var_dump($name);

//response

ContentType - image/png
Id - F-lMbiCYdwiYS8ppkQS4gsyE
Name - Screen.png
Size - 55907
//name
string 'Screen.png' (length=10)

Comments

0

Have you try to use like below:

$uploadimage=$contact->UploadImage[0]->Name;  

Comments

0

In JSON the square braces [] mark an array section and the curly braces {} mark an object. In your JSON string UploadImage is an array, which contains a single object.

1 Comment

Thanks for that explanation.
-2

Try changing your JSON as follows:

{
    "$version": 5,
    "Entry": {
        "Number": "11",
        "Order": null,
        "Origin": {
            "City": "Portland",
            "CountryCode": "US"
        },
        "Message": "the message",
        "UploadImage": {
            "ContentType": "image/png",
            "Id": "F-lMbiCYdwiYS8ppkQS4gsyE",
            "Name": "Screen.png",
            "Size": 55907
        },
        "Subject": "here is the subject"
    }
}

1 Comment

If you read the question you'll see that the user is being passed json, probably meaning that the json can't be edited. The question is how to handle the json, not how to format it.

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.