2

in the response of a request i have this content:

"comp":[
{
"type":"header",
"version":1,
"settings":
     {"logo":"mylogo",
      "logoPosition":"left",
      "inverseLogosPosition":false,
      "headerTitle":"My Report",
      "headerTitlePosition":"left",
      "pageBreak":false
}
},

I want to assert the content of settings. i try this for example to assert the logoPosition = "left"

assert json.components.settings[0].logoPosition[0] == "left"

it's not working

This part is working well:

assert json.comp.type[0] == "header"
assert json.comp.version[0] == 1

Any help please, thank you

Opal
  • 81,889
  • 28
  • 189
  • 210
kirk douglas
  • 577
  • 5
  • 18
  • 35

2 Answers2

2

The json provided is invalid. You can use both paths:

assert slurped.comp.settings.logoPosition[0] == "left"
assert slurped.comp[0].settings.logoPosition == "left"

Full example:

import groovy.json.JsonSlurper

def json = '''{
"comp":[
    {
        "type":"header",
        "version":1,
        "settings": {
            "logo":"mylogo",
            "logoPosition":"left",
            "inverseLogosPosition":false,    
            "headerTitle":"My Report",    
            "headerTitlePosition":"left",        
            "pageBreak":false
        }
    }
]}'''

def slurped = new JsonSlurper().parseText(json)

assert slurped.comp.settings.logoPosition[0] == "left"
assert slurped.comp[0].settings.logoPosition == "left"
Opal
  • 81,889
  • 28
  • 189
  • 210
1

It will just be logoPosition, not logoPosition[0]

Why not have some expected json as a string, convert it to a map with JsonSlurper, then compare these?

tim_yates
  • 167,322
  • 27
  • 342
  • 338