Home » Php » What's better: Json or XML (PHP)

What's better: Json or XML (PHP)

Posted by: admin April 23, 2020 Leave a comment


I’m manipulating few data with PHP, I have the choice between Json and XML. But I don’t know what to choose, I never worked with one of them. So I want the easiest one.

Also I wonder if there’s good classes that can make parsing (XML or Json) easier.

I focus mainly on ease of use rather than speed and scale.

How to&Answers:

json.org makes JSON parsing simple. SimpleXML makes XML simple for PHP.

When choosing which one, it really depends on your application. If you will be passing data to JavaScript for processing on the client-side, then I recommend JSON. IMO, XML is better for data interchange between systems because it is very well understood and human readable (making interpreting easier for you). JSON is smaller over the wire, as there are no closing tags.


JSON parsing and generating is natively included in php:


I feel the following StackOverflow entries answer your question best:

As for which one is easiest?
jQuery makes serializing your JavaScript objects into JSON objects easy.

But, if you’re still unsure…

Here are some other articles:


JSON views your data as structures known from programming languages: maps/dictionaries (called ‘objects’ in JSON), arrays and literals. JSON puts these basic constructs into hierarchies.

XML is also hierarchical, but it has three basic parts: elements (with start/end tags), attributes within elements, and text content. Elements and text content can be mixed, so it is suited to marking up documents. JSON isn’t very good for this.

XML has huge ecosystem built around it with lot of ready tools: various schemas to check that your XML documents are valid, transformation language, XPath for navigating your document, etc. JSON is lacking in this area too.

XML is much more complex than JSON though. It’s easy and fun to write your own JSON parser, but writing XML parser is huge undertaking (surely not for somebody who is new to XML). In Javascript, JSON can be parsed by evaluating the text (this is pretty insecure though).

If you need to transfer data between two systems, JSON is fine for this. If you want to use more advanced properties from XML (entities, automatic inclusion of other documents, schema, implicit attribute values, etc.), or mix content and markup, XML will work better for you.


One thing to consider is that, at least on download, the difference in size between XML and JSON (XML being bigger) is not really interesting because you will using GZIP compression over HTTP.

If you are sure that you will working with Javascript and never have any other type of client (another website, a Flash/Flex client, whatever) then JSON is great and perhaps a bit less painless. On the other hand, doing XML now saves you time should you ever need to get stuff working over XML.

Anyway, a good thing to do is check what the Google guys are doing using LiveHTTPHeaders for Gmail or any of their apps. I’m pretty sure that it’s XML and not JSON, but you can check me on that. In all cases, doing what Google does might be considered to be “not thinking,” but they’re generally smarter than me and have many more person-hours to think anyway 🙂


It depends.

Example: You want to place your application into a big context (EAI), then you should use XML (maybe Webservices & WSDL).

Example2: If you want a simple user interface, based on javascript and ajax you should consider JSON.

JSON is most useful when you work with javascript. In the most cases XML libraries are so easy to use, that the ease of use is nearly the same.


If you choose XML use SimpleXML, an extremely simple to use library


If you’ve not used either, then go for JSON. The answers so far are correct in that there is a lot of help for both and you’ll get up and running on either fairly quickly. JSON has the added advantage of being less bloated than XML and can easily be ‘translated’ to javascript when on the client


I use JSON with PHP because of how simple it is:

$arr = array('foo' => 'bar', 'animals' => array('cat', 'dog')); // Create any array.
$json = json_encode($arr); // Converts any array to JSON.


There are a number of (perhaps one-sided) JSON vs XML articles at the bottom of the json.org page.

Here’s one.

I generally prefer JSON because it’s much better suited for data I typically would represent, which in my line of work would be things like vertices, meshes, etc. Depending on your data XML might be a fine format.


The easiest one to start is XML.
The main reason for this is not parsing (for there are very good libs for XML and JSON that do this for you, see other posts), but the understandability:

JSON works with a lot of different parentheses, when looking at your XML data you will probably see any errors faster. (Assuming you know e.g. HTML)

It is also possible (but optional) to create an XML schema, that makes verifying your data automatically easy. This can save you a lot of time afterwards!