
Product
Introducing Webhook Events for Pull Request Scans
Add real-time Socket webhook events to your workflows to automatically receive pull request scan results and security alerts in real time.
json-origami
Advanced tools
Transform and reshape your JSON objects with the artistry of origami, all without any external dependencies.
json-origami
is a lightweight utility library crafted with the finesse of origami techniques.
Dive into the world of JSON manipulation without the overhead of additional dependencies.
Whether you're looking to flatten a nested structure or revert a flat map into its multi-dimensional glory, json-origami
makes the process seamless.
npm install json-origami
Flatten a multi-layered JSON object into a single-tiered representation.
import { fold } from 'json-origami'
const obj = {
a: 1,
b: {
c: 2,
d: [3, { e: 4 }]
}
}
const result = fold(obj)
console.log(result)
/// { a: 1, 'b.c': 2, 'b.d[0]': 3, 'b.d[1].e': 4 }
Transform a single-layer JSON map back to its original nested structure.
import { unfold } from 'json-origami'
const obj = {
a: 1,
'b.c': 2,
'b.d[0]': 3,
'b.d[1].e': 4
}
const result = unfold(obj)
console.log(result)
/// { a: 1, b: { c: 2, d: [3, { e: 4 }] } }
Reshape and reorganize the keys of your JSON structure, mirroring the intricate adjustments made in origami.
import { twist } from 'json-origami'
const obj = {
a: 1,
b: {
c: 2,
d: [3, { e: 4 }]
}
}
const reshapingMap = {
a: 'foo',
'b.c': 'bar',
'b.d': 'baz'
}
const result = twist(obj, reshapingMap)
console.log(result)
/// { foo: 1, bar: 2, baz: [3, { e: 4 }] }
Select only the specified keys from a JSON object.
import { pick } from 'json-origami'
const obj = {
a: 1,
b: {
c: 2,
d: [3, { e: 4 }]
}
}
const result = pick(obj, ['a', 'b.c'])
console.log(result)
/// { a: 1, b: { c: 2 } }
Remove the specified keys from a JSON object.
import { omit } from 'json-origami'
const obj = {
a: 1,
b: {
c: 2,
d: [3, { e: 4 }]
}
}
const result = omit(obj, ['b.c', 'b.d[1]'])
console.log(result)
/// { a: 1, b: { d: [3] } }
Type: 'dot' | 'bracket'
Default: 'bracket'
Determines the formatting of array indexes when keys are compressed. Choose between dot notation (.0) or bracket notation ([0]).
import { fold } from 'json-origami'
const obj = {
a: 1,
b: {
c: 2,
d: [3, { e: 4 }]
}
}
const result = fold(obj, { arrayIndex: 'dot' })
conole.log(result)
/// { a: 1, 'b.c': 2, 'b.d.0': 3, 'b.d.1.e': 4 }
Options for 'unfold', 'twist', 'pick', 'omit'
Type: boolean
Default: true
Specifies whether to remove the specified array elements or not.
import { twist } from 'json-origami'
const obj = {
a: 1,
b: {
c: 2,
d: [3, 4, 5, 6]
}
}
const result1 = twist(obj, { 'b.d[1]': 'D' }, { pruneArray: true })
console.log(result1)
/// { a: 1, b: { c: 2, d: [3, 5, 6] }, D: 4 }
const result2 = twist(obj, { 'b.d[1]': 'D' }, { pruneArray: false })
console.log(result2)
/// { a: 1, b: { c: 2, d: [3, undefined, 5, 6] }, D: 4 }
Options for 'fold'
Type: string
Default: ''
(empty string)
Adds a specified prefix to the keys in the output.
see CONTRIBUTING.md
FAQs
Transform and reshape your JSON objects with the artistry of origami.
We found that json-origami 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.
Product
Add real-time Socket webhook events to your workflows to automatically receive pull request scan results and security alerts in real time.
Research
The Socket Threat Research Team uncovered malicious NuGet packages typosquatting the popular Nethereum project to steal wallet keys.
Product
A single platform for static analysis, secrets detection, container scanning, and CVE checks—built on trusted open source tools, ready to run out of the box.