Skip to content

Add easier bindings interface #38

@mcrumiller

Description

@mcrumiller

Right now, binding variables requires a bit of investigation into the internals:

from jsonata import Jsonata as JS

data = {
    "products": [
        {"name": "Apple", "price": 1.20},
        {"name": "Banana", "price": 0.50},
        {"name": "Cherry", "price": 2.50}
    ]
}

# It would be nice to be able to just input this dictionary.
bindings = {
    "maxPrice": 1.50,
    "currencySymbol": "$"
}

# Instead we have to create an empty Frame object and bind each item.
binding_frame = JS.Frame(None)
  for k, v in input.items():
      binding_frame.bind(k, v)

expression = jsonata.Jsonata("products[price <= $maxPrice].(name & ' costs ' & $currencySymbol & $string(price))")
result = expression.evaluate(data, bindings=binding_frame)
print(result)
# ['Apple costs $1.2', 'Banana costs $0.5']

It would be nice to simply be able to pass a dictionary as the bindings parameter to evaluate().

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions