Assistance Needed with Parsing JSON Data via PHP Function for Sorting/Processing

HI,

I’ve successfully set up my API and can retrieve JSON data. However, I need guidance on parsing this JSON data through a PHP function to sort and process it efficiently.

Specifically, I have a custom WordPress hook to manipulate this JSON data (e.g., sorting). Currently, my setup involves fetching the JSON directly using the endpoint URL in my php function. However, I’d like to leverage the new API features, particularly for caching and other optimizations.

Can I pass the JSON data as a parameter to my custom hook?

So i set up the API and consume - then create a inner provider for my custom hook where i add a JSON param for the data being fed in by the API e.g.

{
  "jsonfromapi": "{outputted json}"
}

Could you provide some insights or examples on how I can implement this? Specifically, I’m looking for the best way to parse and pass the JSON data from my API to the PHP function linked to my custom hook.

Any help or direction on this would be greatly appreciated.

Thank you!

Hi Jack,

You can pass the JSON data to your custom Hook as a string, and inside the function you need to decode it to use in your PHP code. You can check the methods described in the following article.

https://www.php.net/manual/en/ref.json.php

Please remember that the questions not related to the Themes or bundled plugins are beyond the scope of Theme Support. I would suggest you hire a developer who can assist you to do the customization or you can avail of our newly launched service called One, where we answer the questions beyond normal theme support.

Thanks

Its the passing it as a string im struggling with.

When i create the global api with a name, can i reference this output in my custom hook function?

Not sure how to retrieve the json data in my custom hook function once i have it set up is my problem.

Ideally i wanted to manage my api using the global external api configuration and retrieve the JSON ouput (process as a string?) then pass to my custom hook.

The only piece im missing is what i need to reference in my function to pull in that data.

Any more details would be a great help
Thanks

Hi Jack,

Can you show the code you have written, so we may suggest something on that?

Thanks

I guess it depends on how you call that hook. If you post to it you’ll want to reference the string like so $_POST[‘var_name’]. If you’re passing the variable as part of a URI string you would use $_GET[‘var_name’].

Based on your OP it looks like you’re using GET so stringify your json then pass it in the URI as a var like so
?xyz=STRINGIFIEDJSON if it’s the only var in the URI or just add &xyz=STRINGIFIEDJSON then grab that stringfied json using $_GET[‘xyz’]. You would need to decode that string using json_decode($_GET[‘xyz’]);

Does that help?

Hey @IT_MiamiCountyOhio_gov,

Thank you for sharing your idea, @jackc let us know how it goes on your end.

Cheers!

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.