Skip to content

vrcca/json_path_access

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JsonPathAccess

This library converts JSONPath expressions into Access list. This is really useful when you want to use it with get_in/2, drop_in/2, etc.

Example

Assuming we have this JSON:

{
  "store": {
    "book": [
      {
        "category": "reference",
        "author": "Nigel Rees",
        "title": "Sayings of the Century",
        "price": 8.95
      },
      {
        "category": "fiction",
        "author": "Evelyn Waugh",
        "title": "Sword of Honour",
        "price": 12.99
      },
      {
        "category": "fiction",
        "author": "Herman Melville",
        "title": "Moby Dick",
        "isbn": "0-553-21311-3",
        "price": 8.99
      },
      {
        "category": "fiction",
        "author": "J. R. R. Tolkien",
        "title": "The Lord of the Rings",
        "isbn": "0-395-19395-8",
        "price": 22.99
      }
    ],
    "bicycle": {
      "color": "red",
      "price": 19.95
    }
  }
}

You could access some fields using JSONPath selectors:

json_path = JsonPathAccess.to_access("$.store.bicycle.price")
get_in(json, json_path)

json_path = JsonPathAccess.to_access("$.store.book[1]['category']")
get_in(json, json_path)

Installation

If available in Hex, the package can be installed by adding json_path_access to your list of dependencies in mix.exs:

def deps do
  [
    {:json_path_access, "~> 0.1.0"}
  ]
end

Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/json_path_access.

TODO

This library attempts to implement this version of the spec: https://www.ietf.org/archive/id/draft-ietf-jsonpath-base-05.html

  • Root Selector
  • Dot Selector
  • Dot Wildcard Selector
  • Index Selector
  • Index Wildcard Selector
  • Array Slice Selector
    • Note: Currently there is no support for negative steps.
  • Filter Selector
  • Descendant Selector
  • List Selector

About

Converts JSONPath selectors into Access selectors

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages