Skip to main content

REST Assured: Groovy's GPATH vs JayWay's JSONPath

Groovy's Gpath

  • Apache Groovy is a powerful, optionally typed dynamic language with a multi-faceted java platform. 
  • Groovy comes as integrated support for converting between groovy objects and JSON. 
  • JSON serialization classes and parsing come under this groovy.json package.
  • By default, rest assured, used groovy's gpath syntax.

The GPath is a path expression language integrated into the groovy language. It has similar scope that of Xpath and XML. It used dot-object notation to perform object navigation in JSON. 

Example

  • x.y.z -> For XML yields all the z elements inside x and y.
  • x.y.z -> In POJO yields z properties for all y properties of x.

Jayway's JSONPath

  • Unlike gpath, this one is not directly workable rest assured as we need to add this below dependency in the maven repo.
  • The expression in JSON path is different from gpath in that it solely refers to the JSON structure in the same way of XPath.
  • The root member object in the JSON path is referred to as $ regardless of whether it is an array or an object.
  • We have to add a separate dependency and use it in java programming.

It is pretty popular with java and widely used and expressed in dot notation navigation.

$.head.name[0].age

Or it can be notated in brackets too.

$[head][name][0][age]

Groovy used gpath expressions in the syntax, but here JSON path uses operators.

Conclusion

  • It is essential to know about these differences.
  • Depending on our choice, we can use the proper JSON implementation.
  • Groovy's gpath is available as default syntax in rest assured.
  • Jayway's JSON path is used by adding its dependency.
Submitted by arilio666 on January 19, 2022

Ashwin is working with iVagus as a Technical Lead. He has experience in various technologies like Cypress, JavaScript, TypeScript, Rest Assured, Selenium, Cucumber, and Java.

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.