Monday, April 17th, 2006

BadgerFish: Translating XML to JSON

Category: Library

Brian McCallister says that conversation started, innocently enough, discussing serialization strategies for web services… and out came BadgerFish:

BadgerFish is a convention for translating an XML document into a JSON object. Once you’ve got your XML document represented as a JSON object, it’s easy to manipulate from within Javascript. If you’re familiar with PHP’s SimpleXML extension, think of BadgerFish as aiming for a similar goal: making it simpler to do common manipulations of XML documents with a predictable structure.

The Rules

  • Element names become object properties
  • Text content of elements goes in the $ property of an object.
  • Nested elements become nested properties
  • Multiple elements at the same level become array elements.
  • Attributes go in properties whose names begin with @.
  • Active namespaces for an element go in the element’s @xmlns property.
  • The default namespace URI goes in @xmlns.$.
  • Other namespaces go in other properties of @xmlns.
  • Elements with namespace prefixes become object properties, too.


  1. <alice xmlns="http://some-namespace" xmlns:charlie="http://some-other-namespace"> <bob>david</bob> <charlie :edgar>frank</charlie> </alice>


  1. { "alice" : { "bob" : { "$" : "david" , "@xmlns" : {"charlie" : "http:\/\/some-other-namespace" , "$" : "http:\/\/some-namespace"} } , "charlie:edgar" : { "$" : "frank" , "@xmlns" : {"charlie":"http:\/\/some-other-namespace", "$" : "http:\/\/some-namespace"} }, "@xmlns" : { "charlie" : "http:\/\/some-other-namespace", "$" : "http:\/\/some-namespace"} } }

Posted by Dion Almaer at 12:28 pm

3.5 rating from 49 votes


Comments feed TrackBack URI

<heresy tr=””>
Does it have an option to NOT emit the namespace stuff? I can’t imagine a javascript client application really using that in a real world way

Comment by Marc Brooks — April 17, 2006

Would it be too much to ask that the magick xmlns attribute not pollute the “normal” attribute namespace?

I could have an element named xmlns, couldn’t I?

How about normal attribs get prefixed with “@”, while namespaces get prefixed with “:”?

Comment by Jeremy Dunck — April 17, 2006

Interesting Finds

Trackback by Jason Haley — April 18, 2006

It looks like this is only for PHP and Ruby users. If it was a Javascript library it would be server-language neutral, right?

Comment by Jon — April 18, 2006

Jeremy — you actually can’t have an attribute named “xmlns” (or, that is, if you did, it has to be what’s setting the namespace). And if you had a child element named ‘xmlns’, it wouldn’t collide with the attribute — the attribute is “@xmlns” while the child element is “xmlns”.

Jon — the convention on translation from XML to JSON is language neutral. You are right, however, that so far libraries have only been written (that I am aware of) for PHP and Ruby. I have been noodling around with a Javascript version as well, but I need to fix a few IE-specific bugs in it before it’s ready for release.

Comment by David Sklar — April 20, 2006

[…] BadgerFish: Translating XML to JSON (tags: ajax json php javascript) […]

Pingback by BarelyBlogging » Blog Archive » links for 2006-04-22 — April 21, 2006

My english is very bad, but I believe that the communication can be established…

I developed a JSON – XML translator in Javascript and, to do that with BadgerFish, i found some difficulties.
There is no orientation to work with comments or CDATA sections…
I didn’t find anything about it, because of that i used arbitrarily the characters “!” for comments and ” #” for CDATA sections, besides implementing the suggestion to the text fragments in $1, $2, … properties.

The script was not very tested, but it looks good. Works well.


Comment by Cau Guanabara — June 5, 2006

[…] The conversion follows the convention set by BadgerFish. I didn’t handle namespaces though. […]

Pingback by fiat lux (cahier lukhnos) » ObjectiveFlickr: ContactsBrowser Demo — September 26, 2006

Leave a comment

You must be logged in to post a comment.