Socket
Socket
Sign inDemoInstall

comment-json

Package Overview
Dependencies
Maintainers
1
Versions
55
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

comment-json

Parse and stringify JSON file with comments


Version published
Weekly downloads
3.1M
increased by1.89%
Maintainers
1
Weekly downloads
 
Created

What is comment-json?

The comment-json npm package allows you to parse, modify, and stringify JSON files while preserving comments. This is particularly useful for configuration files where comments are often used to provide context or instructions.

What are comment-json's main functionalities?

Parsing JSON with comments

This feature allows you to parse JSON strings that include comments. The comments are ignored in the resulting JavaScript object.

const commentJson = require('comment-json');
const jsonWithComments = `{
  // This is a comment
  "key": "value"
}`;
const parsed = commentJson.parse(jsonWithComments);
console.log(parsed); // { key: 'value' }

Stringifying JSON with comments

This feature allows you to convert a JavaScript object back into a JSON string, optionally including comments.

const commentJson = require('comment-json');
const obj = { key: 'value' };
const jsonString = commentJson.stringify(obj, null, 2);
console.log(jsonString); // {
// This is a comment
  "key": "value"
}

Modifying JSON while preserving comments

This feature allows you to modify a parsed JSON object and then convert it back to a JSON string, preserving the original comments.

const commentJson = require('comment-json');
const jsonWithComments = `{
  // This is a comment
  "key": "value"
}`;
let parsed = commentJson.parse(jsonWithComments);
parsed.newKey = 'newValue';
const modifiedJsonString = commentJson.stringify(parsed, null, 2);
console.log(modifiedJsonString); // {
// This is a comment
  "key": "value",
  "newKey": "newValue"
}

Other packages similar to comment-json

Keywords

FAQs

Package last updated on 11 Sep 2015

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc