|

JSON in Laravel

JSON In Laravel

If you know the name Douglas Crockford, you know JSON. Mr Crockford is a Chuck Norris of sorts in the Javascript world and created the JSON standard. It stands for JavaScript Object Notation and it provides a great way to share data between languages and applications. Douglas I’m sure is proud of his baby, and we can thank him for declaring himself a standards body and bringing JSON into existence. Let’s learn a little more about JSON in Laravel!

Why JSON?

When dealing with data transfer, we need a convenient method of markup. XML was the first way to accomplish this, but now we are seeing a major uptake of JSON. Why? Because JSON is very easy to read and convenient for storing arrays and objects with values as strings.

AJAX is a popular method of using JSON. Let’s say you make an AJAX hit on your application from the front end, often times the server side will respond with JSON as that data can now be easily parsed and integrated into the Javascript front end. It’s quite magical in fact! If you have ever used the Twitter API, you will also notice that JSON is their method of data exchange.

In PHP we have some nifty functions named json_encode() and json_decode() and Laravel makes great use of these. Let’s now take a look at some JSON!

note: If you are using Firefox you may need to install an add-on like JSONview to make the JSON output pretty in the browser.

Remembering our Laravel Eloquent ORM Tutorial, let’s take an example Database query, and return it’s output to the browser:

Hey that’s pretty cool! Laravel gives us a beautiful output of the query results in JSON format when we simply return the result. This is awesome for troubleshooting or when you are building the logic in your application. You can quickly devise your Eloquent queries, then just return the result and see if you get the results you expect.

The nice thing about this format is that it is easy to read. Now we have the JSONView add on installed so it makes everything pretty, but even without it, you can look at the key value pairs and pretty much figure out what is going on. Still, I think we would rather read our JSON from the above snippet rather than this 🙂

Hey this looks like Javascript!

It does indeed young traveller! There are a few key differences though, which you must command to your grey matter such that you will not be confused!

  • JSON has No Variables
  • Double quotes are Mandatory for strings in JSON!
  • JSON does Not Support Comments!

I like to strictly use single quotes in my Javascript and double quotes in JSON, that way I don’t get mixed up 🙂

What can I put into JSON?

You can use any of the following data types in your JSON:

  • Double
  • Float
  • String
  • Boolean
  • Array
  • Object
  • Null

Nested Objects and Arrays in JSON

You can easily nest Objects and Arrays in JSON and the following snippet of JSON contains an example of both:

Integrate PHP and JSON

So let’s say we have some data such as a PHP array that needs to be in JSON. We can easily convert our PHP array to a JSON string with json_encode() – Let’s do it!

[“paintbrush”,”canvas”,”apron”]

{“physical”:”cables”,”data link”:”mac address”,”network”:”ip address”,”transport”:”tcp”,”session”:”application connections”,”presentation”:”translation”,”application”:”email”}

JSON Decoder Ring

The handy PHP json_decode() function provides us with a way to take our JSON data and import it for use within PHP. By default, it will construct an object of stdClass like so:

physical => cables
data link => mac address
network => ip address
transport => tcp
session => application connections
presentation => translation
application => email

or we can do this!

This might be a little confusing so if you would rather have json_decode() create an array for you instead of stdClass, just pass in true as the second parameter to the function like so:

Having a good knowledge of JSON will keep us running mean and lean in our applications, and it will also help us when working with Composer since it uses JSON.

Thank you for reading JSON In Laravel – If you found this post helpful, Please do share using the buttons below!