
Security News
Browserslist-rs Gets Major Refactor, Cutting Binary Size by Over 1MB
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
json-expand
Advanced tools
Expands your self referencing json
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
// 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
{
"apiDomain": "api.joshwillik.superhost.foobar",
"baseDomain": "joshwillik.superhost.foobar",
"userAccount": "joshwillik",
"hostingProvider": "superhost.foobar",
"authDomain": "accounts.google.com/login",
"google": {
"accounts": {
"url": "/login",
"base": "accounts.google.com"
}
}
}
var expand = require( 'json-expand' )
var obj = {
foo: "bar",
message: "foo = {{foo}}"
}
expand( obj )
> { foo: 'bar', message: 'foo = bar' }
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.
{
"name": "John, son of {{parent}}",
"parent": "Bill"
}
// becomes
{
"name": "John, son of Bill",
"parent": "Bill"
}
An expression may reference sub-properties of the document it resides in. All expressions are evaluated starting at the top scope.
{
"description": "John drives with {{vehicle.wheels}} wheels",
"vehicle": {
"wheels":4
}
}
// becomes
{
"description": "John drives with 4 wheels",
"vehicle": {
"wheels":4
}
}
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.
{
"description": "I enjoy {{ favoriteFood || foodIKindaLike }}",
"foodIKindaLike": "spaghetti"
}
// becomes
{
"description": "I enjoy spaghetti",
"foodIKindaLike": "spaghetti"
}
// but
{
"description": "I enjoy {{ favoriteFood || foodIKindaLike }}",
"foodIKindaLike": "spaghetti",
"favoriteFood": "meatballs"
}
// becomes
{
"description": "I enjoy meatballs",
"foodIKindaLike": "spaghetti",
"favoriteFood": "meatballs"
}
Environment variables may be substituted by adding a $
before an expression.
Using ||
works just fine with these.
{
"remoteHost": "{{ $REMOTE_HOST || defaultHost }}",
"defaultHost": "production.project.com"
}
// if REMOTE_HOST has been set to 'dev.project.com
{
"remoteHost": "dev.project.com",
"defaultHost": "production.project.com"
}
// otherwise
{
"remoteHost": "production.project.com",
"defaultHost": "production.project.com"
}
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.
{
"baseDomain": "my-project.com",
"apiDomain": "{{ doesNotExist || 'api.' + baseDomain }}"
}
// becomes
{
"baseDomain": "my-project.com",
"apiDomain": "api.my-project.com"
}
FAQs
Expand self referencing json documents
We found that json-expand demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
Research
Security News
Eight new malicious Firefox extensions impersonate games, steal OAuth tokens, hijack sessions, and exploit browser permissions to spy on users.
Security News
The official Go SDK for the Model Context Protocol is in development, with a stable, production-ready release expected by August 2025.