Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 243711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:56:39+00:00 2026-05-11T20:56:39+00:00

I am attempting to use JSON to send data between the browser and my

  • 0

I am attempting to use JSON to send data between the browser and my app.

I am attempting to use Lift 1.0 to create and parse JSON strings, but for some reason I am unable to parse the JSON I just constructed:

scala>import scala.util.parsing.json.JSON._
import scala.util.parsing.json.JSON._

scala> import net.liftweb.http.js._
import net.liftweb.http.js._

scala> import net.liftweb.http.js.JE._
import net.liftweb.http.js.JE._

scala> val json = JsObj(("foo", 4), ("bar", "baz")).toJsCmd
json: String = {'foo': 4, 'bar': 'baz'}

scala>  parseFull(json)  
res3: Option[Any] = None

How do I programmatically construct a valid JSON message in Scala/Lift that can also be parsed again?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T20:56:39+00:00Added an answer on May 11, 2026 at 8:56 pm

    You are using Lift 1.0’s JsCmd, which produces JSON with single-quoted strings and attempting to parse it with scala’s parser, which only supports double-quoted strings.

    It is important to realize that there are multiple definitions for JSON.

    Are single-quoted strings valid in JSON?

    • They are according to ECMAScript 5th Ed
    • They are not according to Crockford’s original RFC 4627

    Lift and Scala provide many ways to parse JSON, sometimes with differing behavior between versions.

    The strings accepted by these parsers are not equivalent.

    Here are some comments and examples of the various methods to product and parse JSON strings.


    Producing JSON with the lift-json library DSL

    • Recommended
    • Despite its name, this is a separate project with no dependencies on the rest of Lift

    example:

    scala> import net.liftweb.json.JsonAST
    import net.liftweb.json.JsonAST
    
    scala> import net.liftweb.json.JsonDSL._
    import net.liftweb.json.JsonDSL._
    
    scala> import net.liftweb.json.Printer._
    import net.liftweb.json.Printer._
    
    scala> val json1 = ("foo" -> 4) ~ ("bar" -> "baz")
    json1: net.liftweb.json.JsonAST.JObject = JObject(List(JField(foo,JInt(4)), JField(bar,JString(baz))))
    
    scala> compact(JsonAST.render(json1))
    res0: String = {"foo":4,"bar":"baz"}
    
    scala> val json2 = List(1,2,3)
    json2: List[Int] = List(1, 2, 3)
    
    scala> compact(JsonAST.render(json2))
    res1: String = [1,2,3]
    
    scala> val json3 = ("foo", 4) ~ ("bar", List(1,2,3))
    json3: net.liftweb.json.JsonAST.JObject = JObject(List(JField(foo,JInt(4)), JField(bar,JArray(List(JInt(1), JInt(2), JInt(3))))))
    
    scala> compact(JsonAST.render(json3))
    res2: String = {"foo":4,"bar":[1,2,3]}
    

    Parsing JSON with the lift-json library

    • Recommended
    • Provides implicit mapping to/from scala case-classes
    • Case-classes defined in the console are not currently supported (will throw a com.thoughtworks.paranamer.ParameterNamesNotFoundException: Unable to get class bytes)
    • The example below uses PublicID, a pre-existing scala case-class so that it will work on the scala console.

    example:

    scala> import scala.xml.dtd.PublicID
    import scala.xml.dtd.PublicID
    
    scala> import net.liftweb.json._
    import net.liftweb.json._
    
    scala> import net.liftweb.json.JsonAST._
    import net.liftweb.json.JsonAST._
    
    scala> import net.liftweb.json.JsonDSL._
    import net.liftweb.json.JsonDSL._
    
    scala> implicit val formats = DefaultFormats 
    formats: net.liftweb.json.DefaultFormats.type = net.liftweb.json.DefaultFormats$@7fa27edd
    
    scala> val jsonAst = ("publicId1" -> "idString") ~ ("systemId" -> "systemIdStr")
    jsonAst: net.liftweb.json.JsonAST.JObject = JObject(List(JField(publicId,JString(idString)), JField(systemId,JString(systemIdStr))))
    
    scala> jsonAst.extract[PublicID]
    res0: scala.xml.dtd.PublicID = PUBLIC "idString" "systemIdStr"
    

    Parsing JSON in scala 2.7.7 and 2.8.1

    • Not Recommended – “No longer really supported“
    • Scala 2.7.7’s parser will not parse single-quoted JSON
    • This parsing method used in the question

    example:

    scala>import scala.util.parsing.json.JSON._
    import scala.util.parsing.json.JSON._
    
    scala>  parseFull("{\"foo\" : 4 }")        
    res1: Option[Any] = Some(Map(foo -> 4.0))
    
    scala> parseFull("[ 1,2,3 ]")
    res2: Option[Any] = Some(List(1.0, 2.0, 3.0))
    
    scala>  parseFull("{'foo' : 4 }")  
    res3: Option[Any] = None
    

    Parsing JSON in Lift 2.0 and 2.2 with util.JSONParser

    • Neutral Recommendation
    • Lift’s util.JSONParser will parse single- or double-quoted JSON strings:

    example:

    scala> import net.liftweb.util.JSONParser._
    import net.liftweb.util.JSONParser._
    
    scala> parse("{\"foo\" : 4 }")    
    res1: net.liftweb.common.Box[Any] = Full(Map(foo -> 4.0))
    
    scala> parse("[ 1,2,3 ]")
    res2: net.liftweb.common.Box[Any] = Full(List(1.0, 2.0, 3.0))
    
    scala> parse("{'foo' : 4}")           
    res3: net.liftweb.common.Box[Any] = Full(Map(foo -> 4.0))
    

    Parsing JSON in Lift 2.0 and 2.2 with json.JsonParser

    • Neutral Recommendation
    • Lift’s json.JsonParser will not parse single-quoted JSON strings:

    example:

    scala> import net.liftweb.json._
    import net.liftweb.json._
    
    scala> import net.liftweb.json.JsonParser._
    import net.liftweb.json.JsonParser._
    
    scala> parse("{\"foo\" : 4 }")
    res1: net.liftweb.json.JsonAST.JValue = JObject(List(JField(foo,JInt(4))))
    
    scala> parse("[ 1,2,3 ]")
    res2: net.liftweb.json.JsonAST.JValue = JArray(List(JInt(1), JInt(2), JInt(3)))
    
    scala> parse("{'foo' : 4}")    
    net.liftweb.json.JsonParser$ParseException: unknown token '
    Near: {'foo' : 4}
        at net.liftweb.json.JsonParser$Parser.fail(JsonParser.scala:216)
        at net.liftweb.json.JsonParser$Parser.nextToken(JsonParser.scala:308)
        at net.liftweb.json.JsonParser$$anonfun$1.apply(JsonParser.scala:172)
        at net.liftweb.json.JsonParser$$anonfun$1.apply(JsonParser.scala:129)
        at net.liftweb.json.JsonParse...
    

    Producing JSON with Lift 1.0 JsCmd

    • Not Recommended – output not valid for all JSON parsers
    • Note the single-quotations around strings:

    example:

    scala> import net.liftweb.http.js._
    import net.liftweb.http.js._
    
    scala> import net.liftweb.http.js.JE._
    import net.liftweb.http.js.JE._
    
    scala> JsObj(("foo", 4), ("bar", "baz")).toJsCmd
    res0: String = {'foo': 4, 'bar': 'baz'}
    
    scala> JsArray(1,2,3).toJsCmd
    res1: String = 
    [1, 2, 3]
    
    scala>  JsObj(("foo", 4), ("bar", JsArray(1,2,3))).toJsCmd
    res2: String = 
    {'foo': 4, 'bar': [1, 2, 3]
    }
    

    Producing JSON with Lift 2.0 JsCmd

    • Neutral Recommendation
    • Note the double quotations around strings:

    example:

    scala> import net.liftweb.http.js._
    import net.liftweb.http.js._
    
    scala> import net.liftweb.http.js.JE._
    import net.liftweb.http.js.JE._
    
    scala> JsObj(("foo", 4), ("bar", "baz")).toJsCmd
    res0: String = {"foo": 4, "bar": "baz"}
    
    scala> JsArray(1,2,3).toJsCmd
    res1: String = 
    [1, 2, 3]
    
    scala> JsObj(("foo", 4), ("bar", JsArray(1,2,3))).toJsCmd
    res3: String = 
    {"foo": 4, "bar": [1, 2, 3]
    }
    

    Producing JSON in scala (tested with 2.10)

    • “No longer really supported“, but it works and it’s there.

    example:

    scala> import scala.util.parsing.json._
    import scala.util.parsing.json._
    
    scala> JSONObject (Map ("foo" -> 4, "bar" -> JSONArray (1 :: 2 :: 3 :: Nil))) .toString()
    res0: String = {"foo" : 4, "bar" : [1, 2, 3]}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 176k
  • Answers 176k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Here's the solution that works. It's based on Tomalak's original… May 12, 2026 at 3:13 pm
  • Editorial Team
    Editorial Team added an answer Funny, I just read a review for a book about… May 12, 2026 at 3:13 pm
  • Editorial Team
    Editorial Team added an answer It sounds like you've got back an empty object; so… May 12, 2026 at 3:13 pm

Related Questions

I am attempting to use AJAX in jQuery to load content to a div,
I have been having some problems with this for a few days now... I
I'm attempting to use the following code to serialize an anonymous type to JSON:
I am submitted a getJSON request to a controller in my application, this controller

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.