json-expand
Expands your self referencing json
Installation
Standalone Script
$ git clone https://github.com/joshwillik/json-expand.git$ chmod +x json-expand/json-expand.js$ mv json-expand/json-expand.js ~/my-scripts/json-expand# Assuming ~/my-scripts is in $PATH
NPM module
$ npm install -g json-expand
CLI Usage
// config.json "apiDomain": "api.{{baseDomain}}" "baseDomain": "{{userAccount}}.{{hostingProvider}}" "userAccount": "joshwillik" "hostingProvider": "superhost.foobar" "authDomain": "{{google.accounts.base}}{{google.accounts.url}}" "google": "accounts": "url": "/login" "base": "accounts.google.com"
$ json-expand < config.json
Node.js usage
var expand = var obj = foo: "bar" message: "foo = {{foo}}"> foo: 'bar' message: 'foo = bar'
Rules
Any expression wrapped in {{ }}
will be expanded to the correct value or an empty string if none exists.
The expression to expand may come before the thing it will expand to.
// becomes
An expression may reference sub-properties of the document it resides in. All expressions are evaluated starting at the top scope.
// becomes
Using ||
operator will make json-expand attempt to expand each option, in order, until one works. Once one value is valid, the rest of the options are ignored.
// becomes// but// becomes
Environment variables may be substituted by adding a $
before an expression.
Using ||
works just fine with these.
// if REMOTE_HOST has been set to 'dev.project.com// otherwise
Expressions can be concatinated together if desired. The +
has higher precedence than ||
, so
{{ foo || bar + ' test'}}
will result in either (foo) or (bar + ' test'), never (foo + ' test').
Using parenthesis to override the order of operations will not work in this version.
// becomes