Forum Discussion

harshabongle's avatar
harshabongle
Occasional Contributor
4 years ago
Solved

how to write a script to extract field values from response in the form of a comma separated string?

In the script assertion, I am trying to build a comma-separated string of Ids( "129,169,152") so that I can compare it with the data source 

 

Sample Response:

{
"result": {
"formula" : [
{
"Id" : 129,
"formulaName" : "MO172-00-001",
},
{
"Id" : 169,
"formulaName" : "MO172-50-014",
}

{
"Id" : 152,
"formulaName" : "MO1212-50-024",
}
}

             }

 

 

Please help.

Thanks 

harsha

  • Hey harshabongle 

     

    I'm not picking holes here but can you clarify your description?  reason I ask is that the title of the post leads me one way of doing it, but the description leads me another.  The title of the description says you want to extract attribute values from a response in a comma separated string, but the post body states you say you want to map back the content of the Id attributes in response to the datasource.

     

    whats the endpoint you're after - is it just to assert the content of the response against specific values? OR - do you need the values specifically in a comma separated string for some reason?

     

    Also - whats your datasource(s)?  one file? csv? excel? xml? multiple files?  etc.

     

     

    if you have hardcoded values and you know them - you could add a Script Assertion like the following:

     

    (oh the sample response you provided wasn't wellformed json - and the following script assertion uses jsonpath - so I had to alter the structure slightly to get it wellformed - if I've guessed incorrectly you might have to alter the jsonpath a bit)

     

    def json = new groovy.json.JsonSlurper().parseText(context.response)
    def valList = [129,169,152]
    assert json.result.formula.Id.every{it in valList}
    //where jsonpath to Id attribute is x.result.formula[n].Id
    
    //e.g. Response data as follows:
    
    // {"result": { "formula": [{"Id": 129,"formulaName": "MO172-00-001"},
    //			{"Id": 169,"formulaName": "MO172-50-014"},
    //			{"Id": 152,"formulaName": "MO1212-50-024"}]}}

     

    if you dont want hardcoded values in your assertion you could parameterise the script so it sources the comma separated values in the array from the datasource.

     

    OR - if you still want the values in a string - you could extract the response id values to a Properties step and then a tiny bit of groovy will concatenate them into a comma separated string?

     

    nice one,

     

    rich

     

3 Replies

  • richie's avatar
    richie
    Community Hero

    Hey harshabongle 

     

    I'm not picking holes here but can you clarify your description?  reason I ask is that the title of the post leads me one way of doing it, but the description leads me another.  The title of the description says you want to extract attribute values from a response in a comma separated string, but the post body states you say you want to map back the content of the Id attributes in response to the datasource.

     

    whats the endpoint you're after - is it just to assert the content of the response against specific values? OR - do you need the values specifically in a comma separated string for some reason?

     

    Also - whats your datasource(s)?  one file? csv? excel? xml? multiple files?  etc.

     

     

    if you have hardcoded values and you know them - you could add a Script Assertion like the following:

     

    (oh the sample response you provided wasn't wellformed json - and the following script assertion uses jsonpath - so I had to alter the structure slightly to get it wellformed - if I've guessed incorrectly you might have to alter the jsonpath a bit)

     

    def json = new groovy.json.JsonSlurper().parseText(context.response)
    def valList = [129,169,152]
    assert json.result.formula.Id.every{it in valList}
    //where jsonpath to Id attribute is x.result.formula[n].Id
    
    //e.g. Response data as follows:
    
    // {"result": { "formula": [{"Id": 129,"formulaName": "MO172-00-001"},
    //			{"Id": 169,"formulaName": "MO172-50-014"},
    //			{"Id": 152,"formulaName": "MO1212-50-024"}]}}

     

    if you dont want hardcoded values in your assertion you could parameterise the script so it sources the comma separated values in the array from the datasource.

     

    OR - if you still want the values in a string - you could extract the response id values to a Properties step and then a tiny bit of groovy will concatenate them into a comma separated string?

     

    nice one,

     

    rich

     

    • sonya_m's avatar
      sonya_m
      SmartBear Alumni (Retired)

      Thank you for helping Rich!

       

      harshabongle please let us know if this advice helps๐Ÿ™‚

    • harshabongle's avatar
      harshabongle
      Occasional Contributor

      Hi richie , Thanks.

      I just want to concatenate all the ids into a comma separated string -"123,145,169" . I will compare this string to the input string that present in the datasource(simple grid)