Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

Need help Parsing json

Contributor ,
Jan 26, 2019 Jan 26, 2019

Copy link to clipboard

Copied

Hi all,

I could use a bit of help parsing json with php.

the php result page shows:

<?php

$riddledata = date('Y-m-d H:i:s') . ' ' . print_r($_REQUEST, true) . "\n";

file_put_contents('webhook-logs.log', $riddledata, FILE_APPEND);

?>

and the $_REQUEST data looks like:

{"riddleId":179849,"data":{"riddle":{"id":179849,"title":"test1","type":"quiz"},"lead2":{"Name":{"value":"ddddd","type":"text"},"Email":{"value":"b@b.com","type":"email"}},"answers":[{"question":"<p>q1<\/p>","answer":"a1","correct":true,"questionId":1,"answerId":1}],"result":1,"resultData":{"scoreNumber":1,"resultIndex":1,"scorePercentage":100,"scoreText":"Your score: 1\/1","resultId":2},"embed":{"parentLocation":"https:\/\/www.riddle.com\/showcase\/179849\/quiz"}}}

or maybe this after print_r($_REQUEST, true) ???

(

    [data] => {"riddleId":179849,"data":{"riddle":{"id":179849,"title":"test1","type":"quiz"},"lead2":{"Name":{"value":"aaaaaa","type":"text"},"Email":{"value":"b@b.com","type":"email"}},"answers":[{"question":"<p>q1<\/p>","answer":"a1","correct":true,"questionId":1,"answerId":1}],"result":1,"resultData":{"scoreNumber":1,"resultIndex":1,"scorePercentage":100,"scoreText":"Your score: 1\/1","resultId":2},"embed":{"parentLocation":"https:\/\/www.riddle.com\/showcase\/179849\/quiz"}}}

)

I tried:

$form_data = json_decode($_REQUEST);

$riddleId = $form_data->riddleId[0];

also

$name = ' lead2 name '.$_REQUEST['data']['lead2']['Name']['value'];

but nothing working yet.

Q: What's the best way to parse this out and get the varied fields with php?

Views

1.4K
Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Jan 29, 2019 Jan 29, 2019

At the top of your post you are concatenating a date to the request string and saving it to a file. I'm not sure what that has to do with the question itself so I'm just going to the meat of the question, using the JSON response.

Your 3rd quote has one way to handle it, $form_data = json_decode($_REQUEST); which will decode all form (GET/POST) as well as $_COOKIE data, if that's what you want. I would be more specific myself. especially If the entire request is hitting an endpoint that is only ev

...

Votes

Translate
LEGEND ,
Jan 29, 2019 Jan 29, 2019

Copy link to clipboard

Copied

At the top of your post you are concatenating a date to the request string and saving it to a file. I'm not sure what that has to do with the question itself so I'm just going to the meat of the question, using the JSON response.

Your 3rd quote has one way to handle it, $form_data = json_decode($_REQUEST); which will decode all form (GET/POST) as well as $_COOKIE data, if that's what you want. I would be more specific myself. especially If the entire request is hitting an endpoint that is only ever going to give you an application/json type response. In that event I'd rather pipe the input like:

$response = json_decode( file_get_contents('php://input') ); // read/decode standard JSON response

After you get the content, it's really just about understanding the object hierarchy itself. Something that can validate the response and help it appear a bit visual to you is just using a service like https://jsonlint.com/  to paste your response (the top of your second quote, not after print_r). It will help you visualize the data a bit better. e.g.:

You can also get one of many extensions for your browser and look directly at an endpoint URL that hands back a JSON response and let it parse that information and display it the same way.

Using what you see above, you'd access the object following the structure you see. It is an object overall which immediately contains 2 properties, "riddleId" and "data" (object). Use them accordingly.

echo $response->riddleId; // 179849

echo $response->data->riddle->title; // test1

Just make sure you pay attention to the structure. If you try to access it like your final example, $form_data->riddleId[0], you are expecting "riddleId" to be an array and you want entry 0 in the array. It's not an array. It's a numeric value. If the response had brackets surrounding the value for riddleId, it would be an array value. e.g.:

...

    "riddleId: [

           179849

     ],

     "data": {

...

The brackets, as you know, indicate an array. You don't have that which is why your code isn't displaying what you expect.

For more information on JSON, see here:

JSON

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jan 29, 2019 Jan 29, 2019

Copy link to clipboard

Copied

Hi sinious,

THANK YOU VERY MUCH for you thoughtful response. That really helped me understand what is going on!

Thanks Again - Dave

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 30, 2019 Jan 30, 2019

Copy link to clipboard

Copied

LATEST

You're welcome!

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines