# JsonPathBuilder Aims to provide a declarative JSON based mapper ## Console run `irb -r ./dev/setup` for an interactive prompt. ## Installation Add this line to your application's Gemfile: ```ruby gem 'json-path-builder' ``` And then execute: $ bundle install Or install it yourself as: $ gem install json-path-builder ## JsonPath::Builder ### .build_for > maps input based fields to be mapped via `.from` method For advance usage check out `.from` documentation ```ruby input = { key: "some-value" } JsonPath::Builder.new.from(:key, to: :another_key).build_for(input) #=> {:another_key=>"some-value"} ``` ### .from > defines the json path(s) to locate the value(s) to be mapped for each field #### Required arguments: - `json_path` ~ JSON path supporting dot notation which contains the value(s) relating to the field to be mapped e.g. `account.profile.name` #### Optional arguments: - `to:` ~ field name for mapped value (defaults to `json_path`) - `transform:` - can be one of the following - `Proc` e.g. `->(val) { val.to_s.upcase }` to convert value located at `json_path` to uppercase - Built in Transforms - `:iso8601` e.g. `Date.new(2022,1,1)` => `2022-01-01` - `:date` e.g. `2022-01-01` => `` - `defaults` - (Hash) e.g. `{user_id: 1}` - `fallback` - (Proc) used when mapped value is `blank` Example: ```ruby # Example 1 - Mapping subset of fields input = { key: "some-value", other_key: "some-other-value", list: %w[some-list-value-1 some-list-value-2] }.as_json builder = JsonPath::Builder.new builder.from(:key) builder.from(:other_key) builder.build_for(input) #=> {:key=>"some-value", :other_key=>"some-other-value"} # Example 2 - Mapping nested field to non nested input = { profile: {email: 'email@domain.com'} }.as_json JsonPath::Builder.new.from('profile.email', to: :email).build_for(input) #=> {:email=>"email@domain.com"} # Example 3 - mapping field to another field name input = { key: "some-value" } JsonPath::Builder.new.from(:key, to: :another_key).build_for(input) #=> {:another_key=>"some-value"} # Example 4 - mapping field to nested field input = { key: "some-value" } JsonPath::Builder.new.from(:key, to: "root.key").build_for(input) #=> {:root=>{:key=>"some-value"}} # Example 5 - transforming value to uppercase input = { key: "some-value" } transform = ->(val) { val.to_s.upcase } JsonPath::Builder.new.from(:key, transform: transform).build_for(input) #=> {:key=>"SOME-VALUE"} # Example 6 - transforming value to iso8601 format input = { created_at: Date.new(2022,1,2) } transform = :iso8601 JsonPath::Builder.new.from(:created_at, transform: transform).build_for(input) #=> {:created_at=>"2022-01-02"} # Example 6 - transforming value to iso8601 format input = { created_at: '2023-02-27 16:24:02' } transform = :date JsonPath::Builder.new.from(:created_at, transform: transform).build_for(input) #=> {:created_at=>} # Example 7 - fallback when value to be mapped is `nil` or not present input = { } fallback = -> { Time.now } JsonPath::Builder.new.from(:created_at, fallback: fallback).build_for(input) # => {:created_at=>