Skip to content

Instantly share code, notes, and snippets.

@mackoj
Last active May 26, 2023 08:09
Show Gist options
  • Save mackoj/5786f8b95da0a82e8e003f444c4295bf to your computer and use it in GitHub Desktop.
Save mackoj/5786f8b95da0a82e8e003f444c4295bf to your computer and use it in GitHub Desktop.

Playground

https://www.swift-linux.com/sextant/

Syntax

Depending on the client used JSONPath expressions do start with $. indicating the root element. Some clients omit the leading $..

 $.store.book[0].title              
 store.book[0].title                # With implicit "$."
 
 $['store']['book'][0]['title']     # Alternative notation similar to scripting languages

Tree Traversal

 $.parentNode.childNode.field       # XPath: /parentNode/childNode/@field (content of "field" of all "childNode"s of "parentNode")
 $..anyChildNode                    # XPath: //anyChildNode (all children at any depth named "anyChildNode"
 $.parentNode.*                     # XPath: /parentNode/* (all children below)

Array Access

 $.myList[0]            # first element
 $.myList[-1]           # last element
 $.myList[2:3]          # range
 $.myList[0,4,5]        # selection

Filtering

 $.customer[?(@.car)]                      # Only "customer"s that have attribute "car"
 $.customer[?(@.car == 'Ford Fiesta')]     # Only "customer"s with "Ford Fiesta"s
 $.customer[?(@.age > 18)]                 # Only adults

Complex Conditions

 $.customer[?(@.age > 18 || @.car == 'Ford Fiesta')]     # logical or
 $.customer[?(@.age < 18 && @.hobby == 'Biking' )]       # logical and

Output Mapping

 $.[].{Name:name, Age:age, Hobbies:details.hobbies}        # Mapping fields/nested fields to new set
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment