Parsing JSON with JavaScript
I'd write it as a recursive descent parser since this is quite simple:

http://en.wikipedia.org/wiki/Recursive_descent_parser

Actually I already did this a couple of years back, when I wanted to create something that simply formats and highlights JSON a bit. If you can understand it, then you can modify it to your needs:

&lt;html&gt;
    &lt;head&gt;
        &lt;style type="text/css"&gt;
.str
{
    color: green;
}

.obj
{
    font-weight: bold;
}

.num
{
    color: red;
}
        &lt;/style&gt;
        &lt;script language="javascript" type="text/javascript"&gt;

String.prototype.repeat = function(n) {
  result = '';
  for (var i = 0; i &lt; n; i++) result += this;
  return result;
}

function jsonFormater() {
  this.reset = function() {
    this.txt = '';
    this.pos = 0;
    this.result = '';
    this.indent = 0;
    this.classes = Array();
  };

  this.undoindent = function() {
    this.indent -= 4;
    this.nline();
  };

  this.doindent = function() {
    this.indent += 4;
    this.nline();
  };

  this.nline = function() {
    this.result += '&lt;br /&gt;' + '&amp;nbsp;'.repeat(this.indent);
  };

  this.chClass = function(neu) {
    if (this.classes.length &gt; 0) this.result += '&lt;/span&gt;';
    this.result += '&lt;span class="' + neu + '"&gt;';
    this.classes.push(neu);
  };

  this.endClass = function() {
    this.classes.pop();
    this.result += '&lt;/span&gt;';
    if (this.classes.length &gt; 0) this.result += '&lt;span class="' + this.classes[this.classes.length - 1] + '"&gt;';
  };

  this.formatJson = function(txt) {
    this.txt = txt;
    this.pos = 0;
    this.result = '';
    while (this.pos &lt; this.txt.length) {
      if (this.txt[this.pos] == '{') this.parseObj();
      else if (this.txt[this.pos] =='[') this.parseArray();
      this.pos++;
    }

    return this.result;
  }

  this.parseObj = function(ende) {
    if (typeof ende =='undefined') var ende = '}';
    this.chClass('obj');

    do {
      if ((this.txt[this.pos] == '{') || (this.txt[this.pos] == '[')) this.nline();
      this.result += this.txt[this.pos];
      if (this.txt[this.pos] == ',') this.nline();
      if ((this.txt[this.pos] == '{') || (this.txt[this.pos] == '[')) this.doindent();
      this.pos++;
      if (this.txt[this.pos] == '{') this.parseObj();
      if (this.txt[this.pos] == '[') this.parseArray();
      if (this.txt[this.pos] == '"') this.parseString();
      if (/\d/.test(this.txt[this.pos])) this.parseNum();
      if ((this.txt[this.pos] == '}') || (this.txt[this.pos] == ']')) this.undoindent();
    } while ((this.pos &lt; this.txt.length) &amp;&amp; (this.txt[this.pos] != ende));

    this.result += this.txt[this.pos];
    this.pos++;
    this.endClass();
  };

  this.parseArray = function() {
    this.parseObj(']');
  };

  this.parseString = function() {
    this.chClass('str');
    do {
      this.result += this.htmlEscape(this.txt[this.pos]);
      this.pos++;
    } while ((this.pos &lt; this.txt.length) &amp;&amp; ((this.txt[this.pos] != '"') || (this.txt[this.pos - 1] == '\\')));

    this.result += this.htmlEscape(this.txt[this.pos]);
    this.pos++;
    this.endClass();
  };

  this.parseNum = function() {
    this.chClass('num');
    do {
      this.result += this.txt[this.pos];
      this.pos++;
    } while ((this.pos &lt; this.txt.length) &amp;&amp; (/[\d\.]/.test(this.txt[this.pos])));

    this.endClass();
  };

  this.htmlEscape = function(txt) {
    return txt.replace(/&amp;/,'&amp;amp;').replace(/&lt;/g, '&amp;lt;').replace(/&gt;/g, '&amp;gt;').replace(/"/g, '&amp;quot;');
  };

  this.reset();
}

var parser = new jsonFormater();

function go(txt) {
  document.getElementById('ausgabe').innerHTML = parser.formatJson(txt);
  parser.reset();
}

        &lt;/script&gt;
    &lt;/head&gt;
    &lt;body onLoad="go(document.getElementById('thetextarea').value);"&gt;
        &lt;textarea id="thetextarea" rows="25" cols="70" onKeyUp="go(this.value);"&gt;[{"Dies":"Ist ein Beispiel...","mit":["Arrays","und","so"]},{"alles":"sch&amp;ouml;n verschachtelt..."},"tippt einfach json-zeugs in dem grossen Feld.","Die Anzeige aktualisiert sich sofort...","Die Formatierungen sind als &amp;lt;style&amp;gt; gespeichert. Ihr k&amp;ouml;nnt sie so beliebig &amp;auml;ndern.",{"Zahlen":1,"sind":[1,4,55.67],"auch":"sch&amp;ouml;n"}]&lt;/textarea&gt;
        &lt;div id="ausgabe"&gt;&lt;/div&gt;
    &lt;/body&gt;
&lt;/html&gt;