Skip to content
Advertisement

Laravel access data from JSON object in multiple dimensions

I’m writing a Laravel API that consumes an already existing API. I need my own “middleman” API since it inserts sensitive data before sending the request because I don’t want to store that sensitive data on the client side.

I can fetch the data alright and all that, but I would like to be able to use route parameters to access various “dimensions” of the returned JSON object.

Currently, if I navigate to /get/status I get a response that looks like this.

JavaScript

What I would like to be able to do is navigate to get/status/fuelAmount and only get the fuelAmount back. So I would only get 8 as a response. Currently I am able to do this, but I am not sure how to do this on multiple levels in an efficient way. Because getting the fuelAmount is just one “level deeper”, I also wanna be able to do /get/status/heater/timer1/time and only get 17:30 as the response.

Currently the code looks like this

JavaScript

The route in Laravel

JavaScript

So like I said, if I go to /get/status/heater I successfully request the status endpoint of the 3rd-party API and I am able to print out only the heater data.

JavaScript

But if I go to /get/status/heater/timer1/ I get Undefined index: heater/timer1 because obviously that key doesn’t exist in the initial JSON object.

So I would have to somehow add more keys to this return statement return $response[$dataKey] depending on the dataKey parameter in the URL. I can explode that string and get an array with each individual key but I still need to somehow add each key to the return statement..

I can add more parameters in the route instead of using a single wildcard parameter but then I need to write a bunch of if statements for each of the optional parameters, check if it is set and if it is, use it as a key when displaying the JSON data?

Advertisement

Answer

Replace your current isset code block with the following:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement