Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
circular-to-json
Advanced tools
Convert your Circular structure to a JSON string very easily and very accurate
You can install using NPM or Yarn:
npm i circular-to-json
yarn add circular-to-json
CircularJSON
object with two methods: stringify
, parse
and stringifyAndParse
. Here's how you can use them:import { CircularJSON } from "circular-to-json";
const obj = { a: 1 };
obj.b = obj; // adding circular reference
const jsonString = CircularJSON.stringify(obj);
console.log(jsonString);
'{"a":1,"b":{"[Circular]":true}}';
const jsonString = CircularJSON.stringify(obj);
const parsedObj = CircularJSON.parse(jsonString);
console.log(parsedObj);
{ a: 1, b: [Circular] }
stringifyAndParse
method that combines the stringify
and parse
methods into a single operation.const obj = { a: 1 };
obj.b = obj;
const parsedObj = CircularJSON.stringifyAndParse(obj);
{ a: 1, b: [Circular] }
stringify
method takes an optional replacer
function and an optional space
parameter, which work the same way as the corresponding parameters in JSON.stringify
.const obj = {
name: "John",
age: 30,
hobbies: ["reading", "running", "cooking"],
address: {
street: "123 Main St",
city: "Anytown",
state: "CA",
zip: "12345"
}
};
// Set the space parameter to 2 to use 2 spaces for indentation
const jsonStrWith2Spaces = CircularJSON.stringify(obj, null, 2);
console.log(jsonStrWith2Spaces);
// Set the space parameter to '\t' to use a tab for indentation
const jsonStrWithTab = CircularJSON.stringify(obj, null, '\t');
console.log(jsonStrWithTab);
{
"name": "John",
"age": 30,
"hobbies": [
"reading",
"running",
"cooking"
],
"address": {
"street": "123 Main St",
"city": "Anytown",
"state": "CA",
"zip": "12345"
}
}
const obj = {
name: "Alice",
age: 30,
children: [
{ name: "Bob", age: 5 },
{ name: "Charlie", age: 3 },
],
};
// Define a replacer function that converts all numbers to strings
const replacer = (key, value) => {
if (typeof value === "number") {
return value.toString();
}
return value;
};
const jsonString = CircularJSON.stringify(obj, replacer);
console.log(jsonString);
{
"name": "Alice",
"age": "30",
"children": [
{
"name": "Bob",
"age": "5"
},
{
"name": "Charlie",
"age": "3"
}
]
}
parse
method takes an optional reviver
function, which works the same way as the corresponding parameter in JSON.parse
.const jsonString =
'{"name":"John Smith","age":30,"car":{"model":"Tesla","year":2022}}';
const reviver = (key, value) => {
if (typeof value === "string" && key !== "") {
return value.toUpperCase(); // convert all string values (except the root object) to uppercase
}
return value; // otherwise return the original value
};
const obj = JSON.parse(jsonString, reviver);
console.log(obj);
{
"name": "JOHN SMITH",
"age": 30,
"car": {
"model": "TESLA",
"year": 2022
}
}
MIT License
.FAQs
Convert your Circular structure to a JSON string very easily and very accurate
The npm package circular-to-json receives a total of 0 weekly downloads. As such, circular-to-json popularity was classified as not popular.
We found that circular-to-json demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.