Skip to content

cheminfo/get-jpaths

Repository files navigation

get-jpaths

NPM version npm download test coverage license

A small, fast utility to extract all JSON paths to primitive values from an object, with flexible control over traversal depth, array length limits, and regex-based path inclusion/exclusion filters.

Features

  • Recursively extracts all primitive leaf values with their JSON paths (dot notation).
  • Limits recursion depth and maximum array elements processed.
  • Filter included and excluded JSON paths via RegExp arrays.
  • Supports plain objects, arrays, and typed arrays seamlessly.

Installation

npm install get-jpaths

Usage

import { getJPaths } from 'get-jpaths';

const data = {
  user: {
    name: 'Alice',
    password: 'secret',
  },
  settings: {
    theme: 'dark',
    notifications: true,
  },
};

const allPaths = getJPaths(data);
// Returns all primitive value paths in the object with defaults.

const filteredPaths = getJPaths(data, {
  maxArrayElements: 10,
  maxDepth: 5,
  includeJPathRegexps: [/^user./, /^settings.theme$/], // Include specified paths only
  excludeJPathRegexps: [/password/], // Exclude sensitive 'password' path
});

// filteredPaths:
// [
// { jpath: 'user.name', value: 'Alice' },
// { jpath: 'settings.theme', value: 'dark' }
// ]
console.log(filteredPaths);

Alternatively you can return an object mapping jpaths to their corresponding primitive value:

import { getJPathsAsObject } from 'get-jpaths';

const data = {
  user: {
    name: 'Alice',
    password: 'secret',
  },
  settings: {
    theme: 'dark',
    notifications: true,
  },
};

const allPaths = getJPathsAsObject(data);

const filteredPaths = getJPathsAsObject(data, {
  maxArrayElements: 10,
  maxDepth: 5,
  includeJPathRegexps: [/^user./, /^settings.theme$/], // Include specified paths only
  excludeJPathRegexps: [/password/], // Exclude sensitive 'password' path
});

// filteredPaths:
// {
//   'user.name': 'Alice',
//   'settings.theme': 'dark'
// }
console.log(filteredPaths);

License

MIT