UNPKG

simple-json-order

Version:

A lightweight JavaScript utility to preserve the insertion order of top-level keys in JSON objects — ideal for config files, logs, or any use case where key order matters.

27 lines (18 loc) 938 B
# Simple JSON Order This is a lightweight JavaScript utility that allows you to preserve the insertion order of top-level keys when working with JSON. JavaScript objects do not guarantee key order when the keys are numeric or mixed. This can lead to problems in cases where key order matters — such as configuration files, logs, diffs, or structured data that must be rendered predictably. ```js import { JSONOrder } from "simple-json-order"; const jsonOrdered = new JSONOrder(); //This adds values jsonOrdered.add("a", "value a"); jsonOrdered.add("5", "value 5"); jsonOrdered.add("20", "value 10"); //You can delete values using delete //The values keys will be stored as string always jsonOrdered.add(5); // To convert to string run stringify() jsonOrdered.stringify(); // if you want to load a sorted json use parse() jsonOrdered.parse('{"20": "value 20", "5": "value 5", "100": "value 100"}'); ```