Skip to content

JSON Path Support #3

@chris-pardy

Description

@chris-pardy

Currently we use the t.forPath function to create a converter to pull data from various places in the input. This uses an array of path parts including optional t.ParentPath and t.CurrentPath parts. We should instead leverage JSON Path to construct paths.

There are a few addendums to JSON path I'd propose:

  • Allow the current(@) path to be used in place of the root($) path at the start of an expression.
  • Create a parent(^) path that can be used in place of the root($) path at the start of an expression. The parent path can be repeated with dot seperator any number of times. eg. ^.^.^.value.
  • Replace the syntax for predicates ([?(...)]) and scripts ([(...)]) with map and predicate functions.

Using a Tagged template literal

I'd propose the use of a tagged template literal for this feature. These have the advantage of being fairly easily readable while also allowing injection of values into the string for instance the path previously written like this:

const index = 4;
// no leading CurrentPath or ParentPath indicators make this a root path
const converter = t.forPath(['value', index]);

could now be written as:

const index = 4;
const converter = t.path`$.value[${index}]`

New functionality from JSON Path

Json Path expressions have the following functionality that we'd need to support.

  • Slice: [start:end:step]
  • Wildcard: .* and [*]
  • Recursive Descent: ..
  • Unions: [elem1,elem2,...]
  • Predicates: [?${ function }]
  • Map Expressions: [${ function }]

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions