Modules@robinpath/json
json

@robinpath/json

0.1.1Node.jsPublic

JSON manipulation: parse, stringify, deep merge, flatten, unflatten, diff, query by path, pick, and omit

JSON

JSON manipulation: parse, stringify, deep merge, flatten, unflatten, diff, query by path, pick, and omit

Package: @robinpath/json | Category: Utility | Type: Utility

Authentication

No authentication required. All functions are available immediately.

Use Cases

Use the json module when you need to:

  • Parse a JSON string into an object -- Use json.parse to perform this operation
  • Convert a value to a JSON string -- Use json.stringify to perform this operation
  • Get a nested value by dot-separated path -- Use json.get to perform this operation
  • Deep merge two or more objects -- Use json.merge to perform this operation
  • Flatten a nested object to dot-notation keys -- Use json.flatten to perform this operation

Quick Reference

FunctionDescriptionReturns
parseParse a JSON string into an objectParsed JavaScript value
stringifyConvert a value to a JSON stringJSON string
getGet a nested value by dot-separated pathValue at the path
setSet a nested value by dot-separated path, returning a new objectNew object with the value set
mergeDeep merge two or more objectsMerged object
flattenFlatten a nested object to dot-notation keysFlat object with dot-notation keys
unflattenUnflatten dot-notation keys back to a nested objectNested object
diffCompare two objects and return differencesArray of {path, oldValue, newValue}
cloneDeep clone an objectDeep cloned value
isValidCheck if a string is valid JSONTrue if valid JSON
keysGet all keys including nested paths with dot notationArray of all key paths
pickPick specific keys from an objectObject with only picked keys
omitOmit specific keys from an objectObject without omitted keys

Functions

parse

Parse a JSON string into an object

Module: json | Returns: object -- Parsed JavaScript value

json.parse '{"name":"Alice"}'
ParameterTypeRequiredDescription
jsonStringstringYesJSON string to parse

stringify

Convert a value to a JSON string

Module: json | Returns: string -- JSON string

json.stringify $obj
ParameterTypeRequiredDescription
valueanyYesValue to stringify
indentnumberNoIndentation spaces (default: 2)

get

Get a nested value by dot-separated path

Module: json | Returns: any -- Value at the path

json.get $obj "user.name"
ParameterTypeRequiredDescription
objobjectYesSource object
pathstringYesDot-separated path (e.g. user.name)

set

Set a nested value by dot-separated path, returning a new object

Module: json | Returns: object -- New object with the value set

json.set $obj "user.name" "Bob"
ParameterTypeRequiredDescription
objobjectYesSource object
pathstringYesDot-separated path
valueanyYesValue to set

merge

Deep merge two or more objects

Module: json | Returns: object -- Merged object

json.merge $obj1 $obj2
ParameterTypeRequiredDescription
objectsobjectYesObjects to merge (pass multiple args)

flatten

Flatten a nested object to dot-notation keys

Module: json | Returns: object -- Flat object with dot-notation keys

json.flatten $obj
ParameterTypeRequiredDescription
objobjectYesObject to flatten

unflatten

Unflatten dot-notation keys back to a nested object

Module: json | Returns: object -- Nested object

json.unflatten $flat
ParameterTypeRequiredDescription
objobjectYesFlat object with dot-notation keys

diff

Compare two objects and return differences

Module: json | Returns: array -- Array of {path, oldValue, newValue}

json.diff $obj1 $obj2
ParameterTypeRequiredDescription
aobjectYesFirst object
bobjectYesSecond object

clone

Deep clone an object

Module: json | Returns: any -- Deep cloned value

json.clone $obj
ParameterTypeRequiredDescription
objanyYesValue to clone

isValid

Check if a string is valid JSON

Module: json | Returns: boolean -- True if valid JSON

json.isValid '{"a":1}'
ParameterTypeRequiredDescription
strstringYesString to check

keys

Get all keys including nested paths with dot notation

Module: json | Returns: array -- Array of all key paths

json.keys $obj
ParameterTypeRequiredDescription
objobjectYesSource object

pick

Pick specific keys from an object

Module: json | Returns: object -- Object with only picked keys

json.pick $obj ["name", "age"]
ParameterTypeRequiredDescription
objobjectYesSource object
keysarrayYesArray of keys to pick

omit

Omit specific keys from an object

Module: json | Returns: object -- Object without omitted keys

json.omit $obj ["password"]
ParameterTypeRequiredDescription
objobjectYesSource object
keysarrayYesArray of keys to omit

Error Handling

All functions throw on failure. Common errors:

ErrorCause
(standard errors)Check function parameters and authentication
@desc "Parse and validate result"
do
  set $result as json.parse '{"name":"Alice"}'
  if $result != null
    print "Success"
  else
    print "No result"
  end
enddo

Recipes

1. List and iterate

Retrieve all items and loop through them.

@desc "Get and iterate results"
do
  set $result as json.get $obj "user.name"
  each $item in $result
    print $item
  end
enddo

2. Multi-step JSON workflow

Chain multiple json operations together.

@desc "Parse, stringify, and more"
do
  set $r_parse as json.parse '{"name":"Alice"}'
  set $r_stringify as json.stringify $obj
  set $r_get as json.get $obj "user.name"
  print "All operations complete"
enddo

3. Safe parse with validation

Check results before proceeding.

@desc "Parse and validate result"
do
  set $result as json.parse '{"name":"Alice"}'
  if $result != null
    print "Success: " + $result
  else
    print "Operation returned no data"
  end
enddo

Related Modules

Versions (1)

VersionTagPublished
0.1.1latest1 months ago
Install
$ robinpath add @robinpath/json

Collaborators

Dumitru Balaban
Dumitru Balaban
@dumitru
View all @robinpath modules
Version0.1.1
LicenseMIT
Unpacked Size5.3 KB
Versions1
Weekly Downloads26
Total Downloads26
Stars0
Last Publish1 months ago
Created1 months ago

Keywords

Category

utilities