Forum Discussion

Prabaharan's avatar
Prabaharan
Contributor
5 years ago

How to remove empty nodes in JSON request during run time in soapui?

Hi Team,

 

I have implemented a data driven framework for a soap project for which inputs are fetched from an excel file. I have used 'Conditional Inline Property expansion using Ternary Operator' mechanism to remove empty nodes dynamically in soap xml. For example,

Assume, for my first case the request would look like in below format

<Add>

   <intA>10</intA>

   <intB>2</intB>

   <intC>4</intC>

</Add>

and my for second case the request would be in below format

<Add>

   <intA>10</intA>

   <intB>2</intB>

   

</Add> , since my third node is empty in excel file it is automatically removed in the request (only for my second case).

Now I'm trying to achieve the same with JSON. Appreciate if anyone can help me in removing the empty JSON nodes dynamically.

 

Thanks

  • Prabaharan's avatar
    Prabaharan
    5 years ago

    Hi giovanni_favara ,

     

    Appreciate your help. But surprisingly, 'Conditional Inline Property Expansion using Ternary operator' rule itself is working here to remove empty object for REST project in Data-driven testing.

     

    Below syntax for your quick reference

    ${=context.expand("${Properties#initA_Value}") != '' ? '"initA": ' + context.expand("${Properties#initA_Value}") + ',' : ''}

     

    Thanks.

  • Hi Team,

     

    Can anyone help me in resolving this query?Appreciate your help in advance.

     

     

    Thanks,

    Prabaharan


    Prabaharan wrote:

    Hi Team,

     

    I have implemented a data driven framework for a soap project for which inputs are fetched from an excel file. I have used 'Conditional Inline Property expansion using Ternary Operator' mechanism to remove empty nodes dynamically in soap xml. For example,

    Assume, for my first case the request would look like in below format

    <Add>

       <intA>10</intA>

       <intB>2</intB>

       <intC>4</intC>

    </Add>

    and my for second case the request would be in below format

    <Add>

       <intA>10</intA>

       <intB>2</intB>

       

    </Add> , since my third node is empty in excel file it is automatically removed in the request (only for my second case).

    Now I'm trying to achieve the same with JSON. Appreciate if anyone can help me in removing the empty JSON nodes dynamically.

     

    Thanks


     

     

     

    • Prabaharan's avatar
      Prabaharan
      Contributor

      Hi giovanni_favara ,

       

      Thanks for the reply. Guess groovy script will a better option in this since I'm trying to create a Data driven project.

       

      Thanks in advance.

      • giovanni_favara's avatar
        giovanni_favara
        Contributor

        Hi Prabaharan ,

        here is the code that resolve your issue.

        The problem is that this code does not work in SoupUI because the class JsonGenerator is not found. If you run it outside of SoapUI, it works. Maybe someone else can give us a solution for this.

         

         

         

        import groovy.json.*
        
        def jsonSlurper = new JsonSlurper()
        def object = jsonSlurper.parseText('{"initA": 4, "initB": 10, "initC": null } ')
        def objectArray = [] 
        def generator = new JsonGenerator.Options().excludeNulls().build() /* Convert the Collect to Array - handling from nullable values*/ object.each { objectArray.push(it) } // This line is for runnig in a console - uncomment when you want use it //println "JSON without null-values: " + generator.toJson ( object ).toString() // This line is for runnig in a console - uncomment when you want use it
        //log.info "JSON without null-values: " + generator.toJson ( object ).toString()