npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

flex-json

v0.0.5

Published

This is a library that makes each 'Node' in the JSON Object/Array a JSON Object with a Key/Value pair.

Downloads

4

Readme

flex-json

Flexible JSON manipulation library for JavaScript

Maintainers

| Maintainer | GitHub | LinkedIn | | --------------- | ------------------------------------------- | ------------------------------------------------------ | | Ted Tyree | GitHub |LinkedIn |
| Michael Njuguna | GitHub |LinkedIn|

Table of Contents

Why flex-json

It is simply Json with comments! FlexJson was written to make JSON config files easy to manage and allow for comments. The library also makes it super easy to read a json file (often a config file), modify a single value, and save the file back to the file system without messing up the comments.

  • Easy config file formatting
  • Includes comments in both /* */ and // notation
  • Simple to edit Json files
  • Allows for other JavaScript like features such as using either single quotes or double quotes.
  • Can also be used within Node.js apps for other uses such as reading/writing JSON to/from database records and parsing loosely formatted Json in web page content.

How the library works

Flex-json syntax

BTW flex-json as a standard of syntax is not really all that new - it is very much in existence within JavaScript and other syntax standards. Here we just make it available in a library and to facilitate config file parsing and editing.

Strict Mode

When in strict mode, the flex-json library reads JSON files in standard JSON format. Comments are not valid and double quotes are required around strings.

Note: If the library is flagged to preserve spacing, Json that has been read in from a file will be written with the same formatting. In other words, the carriage returns and white space are captured during the parsing process and used to re-format the output during the write process.

Flex Mode

When in flex mode, the flex-json library has the following features:

  • Like JavaScript, comments can be surrounded by /* (start of comment) and */ (end of comment)

  • Like JavaScript, when a "//" is encountered, the remainder of the line is considered to be a comment

  • Strings do not require quotes unless they contain special characters

  • Strings can be quoted using double quotes or single quotes

When in flex mode, all of the following examples of Json are valid:

example 1:

{apple: red, banana: yellow, 'sky': 'blue'}

example 2:

{"apple": "red"
 ,'banana': 'yellow'
 // ,'sky': 'blue'  - this line is commented out
}

example 3:

[ "one, is first"
  ,'two, is next'
  /* comment out remainder of array
  ,"three, is third"
  ,'four', is last"
  */
]

Note that {number:"2"} is not the same as {number:2} because flex-json will see that the 2 without quotes is a valid number and load it as a numeric.

Install

# NPM
npm install flex-json

#PNPM
pnpm install flex-json

# Yarn
Yarn install flex-json

Usage

const  FlexJson  = require('flex-json');

// Create a FlexJson object
const myJson = new FlexJson('{"key": "value"}', true);

// Access properties
console.log(myJson.jsonString); // Get JSON string representation

// Modify properties
myJson.keepSpacing = true; // Preserve spacing during deserialization

// Check status and type
console.log(myJson.Status); // Get status
console.log(myJson.jsonType); // Get JSON type

// Manipulate JSON object
myJson.i('key').thisValue = 'new value'; // Set a new value for a key

// Convert JSON object to array
myJson.ConvertToArray();

// Access array elements
console.log(myJson.item(0).thisValue); // Access first element in the array

// Use new methods
myJson.forEach(item => {
    console.log(item.jsonString); // Iterate through each item and log JSON string
});

myJson.add('new item', 'newKey'); // Add a new item to the JSON object

console.log(myJson.indexOfKey('newKey')); // Get the index of a key in the JSON object

console.log(myJson.contains('key')); // Check if a key exists in the JSON object

console.log(myJson.getStr('key', 'default')); // Get string value by key with a default value

console.log(myJson.getNum('count', 0)); // Get numeric value by key with a default value

console.log(myJson.getBool('flag', false)); // Get boolean value by key with a default value

Serialization and Deserialization examples

const FlexJson = require("flex-json");

// Create an instance of FlexJson
const flexJson = new FlexJson();

// Example: Deserialize JSON
const jsonString = '{"name": "John", "age": 30, "city": "New York"}';
flexJson.Deserialize(jsonString);

// Example: Serialize to JSON
flexJson.SerializeMe();

// Access serialized JSON string
const serializedJson = flexJson.SerializeMe();
console.log(serializedJson);

Config file example (this is the best part!)

First create a json config file for example a text file c:/temp/my-config.json containing the following text…

/* my-config
** this is an example of parsing
** and updating a json config file
*/
{
ParameterA: 'Apple',
ParameterB:'Banana'
}

In your node.js app run these commands…

// setup parameters
let defaultCounter = 0;
let myConfigPath = "c:/temp/my-config.json";

// read json config file
let myConfig = new FlexJson();
myConfig.DeserializeFlexFile(myConfigPath);

// read CounterA and increment it by 1
// use default value to create CounterA if it does not exist
let counter = myConfig.getNum("CounterA",defaultCounter);
counter = counter + 1;
myConfig.add(counter,"CounterA");

// write config file back to file system
myConfig.WriteToFile(myConfigPath);

The first time this is run the output my-config.json will have a new parameter… CounterA:1

And each new time this is run the counter will increase… CounterA:2

Contributing

  1. Fork this repository.
  2. Create new branch with feature name.
  3. Create your feature.
  4. Commit and set commit message with feature name.
  5. Push your code to your fork repository.
  6. Create pull request.

Support

If you like this project, You can support us with starring ⭐ this repository or donate to uO.heartofkenya.com.

Acknowledgements

Special thanks to u0.heartofKenya.com and ebiashararahisi for their work in Machakos, Kenya.

License

MIT

Made with 💙