Giter VIP home page Giter VIP logo

go-jsonmatch's Introduction

JSONMatch

JSONMatch is a variant of JSONPath that simplifes the syntax and eliminating to the maximum extent the number of special characters required to express a path.

The result of matching a document using JSONMatch is a set of matched values in the document. The present library provides functions for mutating, setting or deleting the values in the match set.

Examples

Select the first name:

name.first

Select all keys (or array values) in the designated path:

employees.tasks.*

or:

employee.tasks[*]

Select the first and second name (union):

name[first, second]

Select an employee from an array based on index:

employees[5]

Select a subset of employees from an array according to index

employees[1,2,5,9]

Select employees 1 through 3 using the slice operation

employees[1:3]

Union of two ranges of employees

employees[1:3, 9:12]

Union of a range and a number of individual indicies

employees[1:3, 9, 12]

Select a subset of employees using a filter

employees[wage > 50000]

Select a subset of employees filtering based on the existence of the key 'bonus'.

employees[bonus?]

Recursively select any sub-document under 'some.path' in the document matching the filter:

some.path..[key=="4f5xa"]

Select number from an array where the individual numbers match the filter. (@ == this)

numbers[@ > 50]

Union of completely separate paths:

[employee[5].name, company.name, wageTiers[compensation > 10000]]

Enclose attribute names in single quotes (where needed), and literal strings in double quotes:

employees['the name' == "John Smith"]

Currently filters in jsonpath2 do not support boolean operations, although , is synonymous with or/||. You could do:

employees[name == "John Smith", name == "Granny Smith"]

There is no way to express an intersection. The following is invalid syntax, but considered for a future version:

employees[name.first == "John" && name.last == "Smith"]  // Invalid!

Note: Apparently there is a way to do intersections with the current syntax. You can actually do this: employees[name.first == "John"][name.last == "Smith"]. It looks weird, but it works.

Select from start of array:

array[:3]

Select through end of array:

array[3:]

Select every element in array:

array[*]

Select the last element of an array:

array[-1]

Examples

Select the first name:

name.first

Select all keys (or array values) in the designated path:

employees.tasks.*

or:

employee.tasks[*]

Select the first and second name (union):

name[first, second]

Select an employee from an array based on index:

employees[5]

Select a subset of employees from an array according to index

employees[1,2,5,9]

Select employees 1 through 3 using the slice operation

employees[1:3]

Union of two ranges of employees

employees[1:3, 9:12]

Union of a range and a number of individual indicies

employees[1:3, 9, 12]

Select a subset of employees using a filter

employees[wage > 50000]

Select a subset of employees filtering based on the existence of the key 'bonus'.

employees[bonus?]

Recursively select any sub-document under 'some.path' in the document matching the filter:

some.path..[key=="4f5xa"]

Select number from an array where the individual numbers match the filter. (@ == this)

numbers[@ > 50]

Union of completely separate paths:

[employee[5].name, company.name, wageTiers[compensation > 10000]]

Enclose attribute names in single quotes (where needed), and literal strings in double quotes:

employees['the name' == "John Smith"]

Currently filters in jsonpath2 do not support boolean operations, although , is synonymous with or/||. You could do:

employees[name == "John Smith", name == "Granny Smith"]

There is no way to express an intersection. The following is invalid syntax, but considered for a future version:

employees[name.first == "John" && name.last == "Smith"]  // Invalid!

Note: Apparently there is a way to do intersections with the current syntax. You can actually do this: employees[name.first == "John"][name.last == "Smith"]. It looks weird, but it works.

Select from start of array:

array[:3]

Select through end of array:

array[3:]

Select every element in array:

array[*]

Select the last element of an array:

array[-1]

Acknowledgements

The code was originally forked from the Kubernetes JSONPath parser. However, it has since been totally rewritten bit by bit.

go-jsonmatch's People

Contributors

atombender avatar simen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

etsangsplk

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.