31

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.

Omar Abid
  • 15,753
  • 28
  • 77
  • 108
  • We've all answered the question assuming that you're going to be talking JSON or XML with some kind of client. If you're not (e.g., you're going to product HTML like a "normal" PHP app) then you would not use either of these. Or are you talking about serializing on the server side (e.g., to files)... ??? – Dan Rosenstark Jul 12 '09 at 22:30
  • I said that I'm going to manipulate the data with PHP, I don't know why all were confused and talk about the client side. The server has some data (downloaded from somewhere) and will manipulate it using PHP, what's the easier one. The data is small in size, but very complicated in structure, so I want the easiest to manipulate (Json or XML??) – Omar Abid Jul 13 '09 at 16:40
  • 1
    JSON and XML are formats you would use to get the data out of or into the PHP processor. In PHP, you would use straight PHP objects. Is the data provider forcing you to choose between these two formats? Otherwise you would use PHP Object serialization or a database, perhaps. – Dan Rosenstark Jul 13 '09 at 17:01

11 Answers11

40

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.

geowa4
  • 40,390
  • 17
  • 88
  • 107
  • 2
    same as I said. +1 for "it really depends" – Martin K. Jul 12 '09 at 20:41
  • 7
    I agree with "it depends", but I don't think xml has any edge wrt readability compared to json; they are roughly similar. JSON size benefit exists, but usually doesn't matter all that much: if size does matter, gzipping data makes them about same size (same information content etc). Personally I would choose based on tools (ease-of-use), but can't comment wrt PHP. For Java there are lots of tools for both, I assume same is true for PHP too. – StaxMan Jul 14 '09 at 19:02
  • Read this: http://www.edwardawebb.com/tips/xml-json it's more about the networking but same partly applies to parsing also. – Erik Kubica Nov 06 '17 at 12:29
9

JSON parsing and generating is natively included in php: http://www.php.net/json

Dykam
  • 10,190
  • 4
  • 27
  • 32
8

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:

Community
  • 1
  • 1
Prisoner ZERO
  • 13,848
  • 21
  • 92
  • 137
4

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.

Community
  • 1
  • 1
Peter Štibraný
  • 32,463
  • 16
  • 90
  • 116
  • Are there not libs for XML manipulations on both client (basically all clients) and server (PHP) sides? – Dan Rosenstark Jul 12 '09 at 22:28
  • 1
    Daniel, I am sure there are. My answer focused more on "JSON vs XML" than on libraries. I usually work with Java, and there are tons of libraries for XML in Java world. In PHP I used SimpleXML for my (simple) needs, and it just worked. – Peter Štibraný Jul 13 '09 at 05:15
  • Understood, Peter, I guess I was asking how that 4th paragraph worked its way into your answer. I'll upvote since, reading your answer a few times, I realized that you actually point out something important: XML has an entire world (ecosystem as you say): JSON seems to not have that at all. Not that the questioner cares... :) – Dan Rosenstark Jul 13 '09 at 17:04
3

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 :)

Dan Rosenstark
  • 68,471
  • 58
  • 283
  • 421
  • the difference in size ... is not really interesting << It's processing overhead. For big files (x Gb) it can be also an issue. I've seen a lots of "design by contract" google XML apps. I prefer XML. – Martin K. Jul 12 '09 at 20:44
  • Hmmm... yeah, if we're talking about huge files, the world becomes plainly empirical: you have to test and benchmark and get a clue, I think. JSON is obviously faster than demarshalling from XML... isn't it? – Dan Rosenstark Jul 12 '09 at 22:26
  • 1
    ""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 :)"" not true It'll depend on many things, they don't choose the "BEST", they choose the "BEST ONE FOR GMAIL" – Omar Abid Jul 13 '09 at 16:37
3

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.

Martin K.
  • 4,669
  • 7
  • 35
  • 49
1

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

Andrew G. Johnson
  • 26,603
  • 30
  • 91
  • 135
1

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

Dave Archer
  • 3,022
  • 20
  • 23
1

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.
James Skidmore
  • 49,340
  • 32
  • 108
  • 136
  • 1
    can't you convert an array to XML?? – Omar Abid Jul 13 '09 at 16:35
  • 3
    totally. xml_encode($var, false, "foo"); where $var is the array, false to not display id refs, and "foo" is the name of the XML Root. http://www.projectzero.org/sMash/1.0.x/docs/apidocs/PHP/xapidoc/XMLExtension/xml_encode.html – Dan Rosenstark Jul 13 '09 at 17:20
0

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.

Dan Olson
  • 22,849
  • 4
  • 42
  • 56
0

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!

Tarnschaf
  • 3,955
  • 1
  • 26
  • 36