Forum Discussion

luluberlu's avatar
luluberlu
Occasional Contributor
6 years ago
Solved

record json response into an array

hello;

 

i have this kind of API json response : 

 

{"response": {"responseRecherche": {
"1": {
"Acheteur": {
"Id": "azertyui",
"Date de création": "2018-03-29 12:19:27",
"Date de modification": "2018-03-29 12:19:27",
"Statut": "01",
"Accessibilité": true
},
},
"2": {
"dumeAcheteur": {
"Id": "itkqiisy",
"Date de création": "2018-03-21 10:41:05",
"Date de modification": "2018-03-21 10:41:05",
"Statut": "01",
"Accessibilité": true
},
},
...

"nn": {
"dumeAcheteur": {
"Id": "iizopooj",
"Date de création": "2018-03-23 02:34:35",
"Date de modification": "2018-03-23 02:34:35",
"Statut": "01",
"Accessibilité": true
},
}
}}}

 

i want to retrieve all "date de création" or "Id" in an array. I just have a groovy syntax code problem. 

 

I script that : 

 

import net.sf.*
import net.sf.json.*
import net.sf.json.groovy.*
import com.eviware.soapui.support.XmlHolder
def GU = new com.eviware.soapui.support.GroovyUtils( context )

 

def Response = context.expand ( '${TestStep#Response}').toString()
def Slurp = new JsonSlurper().parseText(Response)
def ResRec = Slurp.response.responseRecherche
def cpt = ResRec.size()

// log.info " Nb = " + cpt <= this test is good. It returns Nb = 30 in my example. So response is right in RecRec.
def node = Slurp.response.responseRecherche.'1'.dumeAcheteur.'Date de création'
// log.info "noeud : " + node <= this test is good. It returns 2018-03-29 12:19:27

 

def i=1
while (i<=cpt)
{
selectID[i] = resrec.'(i)'.dumeAcheteur.Id
i++
}
log.info selectID[1]

def i=1

def Testdate = testRunner.testCase.getProperty("DateReference").value

def checkdate = true
while (i<=cpt and checkdate==true)
{
if selectID[i] > Testdate { checkdate == false }
i++
}

 

=> java.null.pointException: Cannot get property 'dumeAcheteur' on null object error at line: 18

 

selectID[i] = resrec.'(i)'.dumeAcheteur.Id is bad :(

I try everything ! [i], $i, ..., anything works :( 

I think it is easy, but i am noobie in groovy. can you help me please ?

  • Or to step back one step further, consider just using the data from the Slurper result directly.

     

    In your case, your JSON is a bit unusual because it labels the objects as "1" and "2" etc

     

            "responseRecherche": {
                "1": { ... },
    "2": { ... },

    Instead of just giving a list as I would normally have expected:

            "responseRecherche": [
                { ... },
    { ... },

    So, yes, in this case it might make sense to partially transform it, into a proper list (not unlike what Rao has suggested):

     

    dumeAcheteurs = new groovy.json.JsonSlurper()
     .parseText(jsonString)
     .response
     .responseRecherche
     .collect { keyName, keyValue -> keyValue } // creates a list from the values behind "1" and "2", ignoring those names for them
     .collect { it.dumeAcheteur }

    Here you're still keeping the whole objects rather than only pulling out a specific subset. And you can just access what you need directly:

     

    assert dumeAcheteurs[0].Id == "azertyui"
    assert dumeAcheteurs[1]."Date de création" == "2018-03-21 10:41:05"
    // etc

     

  • Hello

     

    thank you for your answers :smileyhappy:

    yesterday we didnt work in france. so i look this morning. 

    I tried quick code with $i but it doesnt' work :(

    After, i try last code with collect function, and it runs very good. 

    i can check all dates before or after a given date, i am very happy to that. thank you so much.

     

    jhunt, yes, this json is a little weak format json, but it is maken by a junior, so ok. in fact, your are a genius :smileywink:

     
     

4 Replies

  • JHunt's avatar
    JHunt
    Community Hero

    To fix that one line...

     

    1) ResRec needs to have capital letters in it.

     

    2) SelectID should be defined as an empty list before you assign anything to it.

     

    SelectID = []

    3) '(i)' isn't right, because

     

    a.'b'.c 

     

    is short for

     

    a.getAt('b' as String).c

    What you are wanting to do is:

     

    selectID[i] = ResRec.getAt(i.toString()).dumeAcheteur.Id

    which you can simplify to:

     

    selectID[i] = ResRec."$i".dumeAcheteur.Id
  • JHunt's avatar
    JHunt
    Community Hero

    Or to step back one step further, consider just using the data from the Slurper result directly.

     

    In your case, your JSON is a bit unusual because it labels the objects as "1" and "2" etc

     

            "responseRecherche": {
                "1": { ... },
    "2": { ... },

    Instead of just giving a list as I would normally have expected:

            "responseRecherche": [
                { ... },
    { ... },

    So, yes, in this case it might make sense to partially transform it, into a proper list (not unlike what Rao has suggested):

     

    dumeAcheteurs = new groovy.json.JsonSlurper()
     .parseText(jsonString)
     .response
     .responseRecherche
     .collect { keyName, keyValue -> keyValue } // creates a list from the values behind "1" and "2", ignoring those names for them
     .collect { it.dumeAcheteur }

    Here you're still keeping the whole objects rather than only pulling out a specific subset. And you can just access what you need directly:

     

    assert dumeAcheteurs[0].Id == "azertyui"
    assert dumeAcheteurs[1]."Date de création" == "2018-03-21 10:41:05"
    // etc

     

    • luluberlu's avatar
      luluberlu
      Occasional Contributor

      Hello

       

      thank you for your answers :smileyhappy:

      yesterday we didnt work in france. so i look this morning. 

      I tried quick code with $i but it doesnt' work :(

      After, i try last code with collect function, and it runs very good. 

      i can check all dates before or after a given date, i am very happy to that. thank you so much.

       

      jhunt, yes, this json is a little weak format json, but it is maken by a junior, so ok. in fact, your are a genius :smileywink: