Forum Discussion

j54321ohn's avatar
j54321ohn
Occasional Visitor
10 months ago

Data formats & structures

While preforming the below similar code assertion failure error is occurring and what would be the solution  

def jsonSlurper = new JsonSlurper()
def object = jsonSlurper.parseText '''
    { "simple": 123,
      "fraction": 123.66,
      "exponential": 123e12
    }'''

assert object instanceof Map
assert object.simple.class == Integer
assert object.fraction.class == BigDecimal
assert object.exponential.class == BigDecimal

  • Seems this is inside - JsonSlurper

    JsonSlurper is a class that parses JSON text or reader content into Groovy data structures (objects) such as maps, lists and primitive types like Integer, Double, Boolean and String.

    The class comes with a bunch of overloaded parse methods plus some special methods such as parseText, parseFile and others. For the next example we will use the parseText method. It parses a JSON String and recursively converts it to a list or map of objects. The other parse* methods are similar in that they return a JSON String but for different parameter types.

    More information we have out here - https://docs.groovy-lang.org/latest/html/documentation/core-semantics.html#gpath_expressions

     

    Hope this helps - Happy to help further!!
    Thank you very much and have a great one!

    Warm regards

  • Humashankar's avatar
    Humashankar
    Champion Level 3

    Seems this is inside - JsonSlurper

    JsonSlurper is a class that parses JSON text or reader content into Groovy data structures (objects) such as maps, lists and primitive types like Integer, Double, Boolean and String.

    The class comes with a bunch of overloaded parse methods plus some special methods such as parseText, parseFile and others. For the next example we will use the parseText method. It parses a JSON String and recursively converts it to a list or map of objects. The other parse* methods are similar in that they return a JSON String but for different parameter types.

    More information we have out here - https://docs.groovy-lang.org/latest/html/documentation/core-semantics.html#gpath_expressions

     

    Hope this helps - Happy to help further!!
    Thank you very much and have a great one!

    Warm regards