A version fallback for key lookup on a single json object


Retrieve values from a specified key or fallback to a default index

var Lookup = require("json-lookup")
var config = {
    "default": {
        "port": "4125"    // by default we should use this config 
    "ci": "default",      // ci should result to default 
    "test": "production", // Test index should be the same as production 
    "production": {       // Production has it's own port config 
        "port": "8085"
// Lookup(json<Object> [, specifiedKey<Maybe<String>, defaultKey<Maybe<String>>]) -> Object 
var lookup = Lookup(config, process.env.NODE_ENV)
// Equivalent to Lookup(config, process.env.NODE_ENV, "default") 
var port = lookup.port
// if process.env.NODE_ENV === "production" 
// port -> "8085" 
// if process.env.NODE_ENV === "test" 
// port -> "8085" 
// if process.env.NODE_ENV === "ci" 
// port -> "4125" 
// if process.env.NODE_ENV === null, undefined, "default" or otherwise not a string 
// port -> "4125" 
  • By default the key "default" is used as a fallback. You can change this by passing in a third argument for the name of this default key
  • If the specified key and default key exists, the default value is extended with the specified value
  • If the specified key exists and the associated value is a string, we take this string to be the key with a recursive lookup
  • If specified key doesn't exist, then only the default value is used
  • If the default value doesn't exist, then only the specified value is used
  • If both the default and specified keys don't exist, an exception is thrown

npm install json-lookup

  • Matt-Esch