Menu

#77 Make JsonSlurper more dynamic

open
aalmiray
5
2009-01-06
2009-01-02
No

Last year I wrote an article about parsing json in grails and you added a comment saying json-lib can do the same. Well today I needed this ability finally outside of grails and started using JsonSlurper.

I think JsonSlurper is a great idea, but needs to be improved to mimc XmlSlurper more closely to make it easier to get to values.
For example, when I use XmlSlurper (from this example http://groovy.codehaus.org/Reading+XML+using+Groovy%27s+XmlSlurper\), I can say things like, records.car[0].name().
Whereas, to the best of my understanding, with JsonSlurper, I still have to use JSONObject and JSONArray to traverse the json tree and this isn't very groovy. For the same example, I would have to say something like, JSONObject.getJSONArray("records").getJSONObject(0).getString("name").

I think it would be very popular if JsonSlurper supported this kind of dynamic capability. Everyone loves XmlSlurper and there really isn't an equivalent slurper for JSON.

I notice in the API that XmlSlurper returns groovy.util.slurpersupport.GPathResult. Perhaps that is where to begin?

Discussion

  • aalmiray

    aalmiray - 2009-01-06

    James,

    JSONObject implements Map, JSONarray implements List so Json-lib/Groovy should be able to handle an expression like records.cars[0].name

    However I think it won't be able to handle @ syntax for accessing attributes, nor depthFirst() nor calling text() on a node.

    Cheers,
    Andres

     
  • aalmiray

    aalmiray - 2009-01-06
    • assigned_to: nobody --> aalmiray
     

Log in to post a comment.