title: How to add an additional json item per object in scala
body: <p>I'm writing a simple scala application that opens a flat file of json data, parses it and finally prints it out to the screen.</p>
  
  <p>The next step will require that I stop at each object and add another item (string) to the front of it.  My question is how can I add a new string per object in this list?</p>
  
  <p>The following is my JSON implementation (credit goes to the init author <a href="http://www.hvergi.net/2008/06/parsing-json-with-scala/" rel="nofollow">here</a>)</p>
  
  <pre><code>import scala.util.parsing.combinator._
  
  class JSON extends JavaTokenParsers {
          def obj: Parser[Map[String, Any]] =
                  "{"~&gt; repsep(member, ",") &lt;~"}" ^^ (Map() ++ _)
  
          def arr: Parser[List[Any]] =
                  "["~&gt; repsep(value, ",") &lt;~"]"
  
          def member: Parser[(String, Any)] =
                  stringLiteral~":"~value ^^
                          { case name~":"~value =&gt; (name, value) }
  
          def value: Parser[Any] = (
                  obj
                  | arr
                  | stringLiteral
                  | floatingPointNumber ^^ (_.toInt)
                  | "null" ^^ (x =&gt; null)
                  | "true" ^^ (x =&gt; true)
                  | "false" ^^ (x =&gt; false)
                  )
  }
  </code></pre>
  
  <p>Next I call this w/ a flat file like so</p>
  
  <pre><code>import java.io.FileReader
  import scala23.JSON
  
  class JSONTest extends JSON {
          def main(args: String) {
                  val reader = new FileReader(args)
                  println(parseAll(value, reader))
          }
  }
  </code></pre>
  
  <p>Then I get a valid println of the json contents.  Instead I would like to pass this parse method a String and have it append it or create a new json object that has the string at the front of each object inside</p>
  
  <p><strong>Update</strong></p>
  
  <p>My current attempt looks something like the below</p>
  
  <pre><code>class JSONTest extends JSON {
    def main(args: String) {
      val reader = new FileReader(args)
      val header = ("abc", "def")
  //    println(parseAll(value, reader).map(addHeader(_, header)))
      println(parseAll(value, reader).map(addHeader(_.asInstanceOf[Map[String, Any]], header)))
    }
    def addHeader(xyz:Map[String, Any], header:(String, Any)):Map[String, Any] = {
      xyz.map {
        case (k, m:Map[String, Any]) =&gt; (k, addHeader(m))
        case e =&gt; e
      } + header
    }
  }
  </code></pre>
  
  <p>But I'm currently getting a few errors in Intellij</p>
  
  <p>error: missing parameter type for expanded function ((x$1) => x$1.asInstanceOf[Map[String, Any]])
  println(parseAll(value, reader).map(addHeader(_.asInstanceOf[Map[String, Any]], header)))</p>
  
  <p>AND </p>
  
  <p>error: not enough arguments for method addHeader: (xyz: Map[String,Any],header: (String, Any))Map[String,Any].
  Unspecified value parameter header.
  case (k, m:Map[String, Any]) => (k, addHeader(m))</p>
  
  <p>Any help would be much appreciated (thank you in advance!)</p>