@interaktiv/json

1.0.0 • Public • Published

@interaktiv/json

JSON related utilities for Javascript at DIA

Commitizen friendly Conventional Commits Semantic Release Code of Conduct MIT License

npm latest version npm next version

This Solution

This library includes a high level support for parsing and working with JSON data.

Table of Contents

Installation

This module can be installed via npm which is bundled with Node.js and should be installed as one of your project's dependencies:

npm install --save @interaktiv/json

API

cloneJson

Perform a deep clone of an object or array compatible with JSON stringification. Object fields that are not compatible with stringification will be omitted. Array entries that are not compatible with stringification will be censored as null.

throws JsonStringifyError If the object contains circular references or causes other JSON stringification errors.

cloneJson(obj): obj

getJsonValuesByName

Finds all elements with a given name in a JsonMap. Not suitable for use with object graphs containing circular references. The specification of an appropriate type that will satisfy all matching element values is the responsibility of the caller.

getJsonValuesByName(json: obj, name: string): obj[]

isJson

Tests wether the given string is a JSON stringified string or not.

isJson(str: string): boolean

jsonIncludes

Tests whether an any JSON value contains another any JSON value. This is a shallow check only and does not recurse deeply into collections.

jsonIncludes(json: obj, value: obj): boolean

parseJson

Parse JSON string data.

throws JsonParseError If the data contents are empty or the data is invalid.

parseJson(data: string, jsonPath?: undefined | string, throwOnEmpty?: boolean): AnyJson

parseJsonMap

Parse JSON string data, expecting the result to be an object.

throws JsonParseError If the data contents are empty or the data is invalid.

throws JsonDataFormatError If the data contents are not a JsonMap.

parseJsonMap(data: string, jsonPath?: undefined | string, throwOnEmpty?: undefined | false | true): obj

References

This library is using custom error types and type narrowing from @interaktiv/errors and @interaktiv/types.

Other Use Cases

If you lack some use cases, you are welcome to open a pull request and add it. We'll come back to you and see how we can support your use case and present it to all devs.

Please consult the contribution guides before contributing.

Acknowledgements

This library is inspired by @salesforce/kit. Thank you 💙

License

MIT Copyright © 2019-present die.interaktiven GmbH & Co. KG

Readme

Keywords

Package Sidebar

Install

npm i @interaktiv/json

Weekly Downloads

2

Version

1.0.0

License

MIT

Unpacked Size

18.5 kB

Total Files

6

Last publish

Collaborators

  • kbshl
  • dia-bot