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

  • SEARCH
  • Home
  • 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 3999426
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:39:20+00:00 2026-05-20T07:39:20+00:00

I think I may be missing something fundamental from the list-json xpath architecture. The

  • 0

I think I may be missing something fundamental from the list-json xpath architecture. The smoothest way I’ve been able to extract and traverse a list is shown below. Can someone please show me a better technique:

class Example {

    @Test
    def traverseJsonArray() {

        def myOperation(kid:JObject) = println("kid="+kid)

        val json = JsonParser.parse("""
            {   "kids":[
                {"name":"bob","age":3},
                {"name":"angie","age":5},
            ]}
        """)

        val list = ( json \\ "kids" ).children(0).children
        for ( kid <- list ) myOperation(kid.asInstanceOf[JObject])

    }

}
  • 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-20T07:39:21+00:00Added an answer on May 20, 2026 at 7:39 am

    If at all possible you should upgrade to Lift JSON 2.3-M1 (http://www.scala-tools.org/repo-releases/net/liftweb/lift-json_2.8.1/2.3-M1/). It contains two important improvements, the other affecting the path expressions.

    With 2.3 the path expressions never return JFields, instead the values of JFields are returned directly. After that your example would look like:

    val list = (json \ "kids").children
    for ( kid <- list ) myOperation(kid.asInstanceOf[JObject])
    

    Lift JSON provides several styles to parse values from JSON: path expressions, query comprehensions and case class extractions. It is possible to mix and match these styles and to get the best results we often do. For completeness sake I’ll give you some variations of the above example to get a better intuition of these different styles.

    // Collect all JObjects from 'kids' array and iterate
    val JArray(kids) = json \ "kids"
    kids collect { case kid: JObject => kid } foreach myOperation
    
    // Yield the JObjects from 'kids' array and iterate over yielded list
    (for (kid@JObject(_) <- json \ "kids") yield kid) foreach myOperation
    
    // Extract the values of 'kids' array as JObjects
    implicit val formats = DefaultFormats
    (json \ "kids").extract[List[JObject]] foreach myOperation
    
    // Extract the values of 'kids' array as case classes
    case class Kid(name: String, age: Int)
    
    (json \ "kids").extract[List[Kid]] foreach println
    
    // Query the JSON with a query comprehension
    val ks = for { 
      JArray(kids) <- json
      kid@JObject(_) <- kids
    } yield kid 
    ks foreach myOperation
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think I may be missing something or haven't grasped a fundamental of jQuery.
I may be missing something in the standard libs, but I don't think so.
I think I may be missing something here or my understanding of rails model
I think I may be missing something coming to object reference, in the below
I think I may have used a repeater when I should have used something
I think I may be way in over my head here... I'm using a
I think I may be using a wrong window style or something or maybe
I'm a total newb to stored procedures, so I may be missing something easy,
I've bending my mind for a while, but I think I'm missing something, so
I am relatively new to chef, so I may be missing something extremely basic.

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.