Registry / serialization / mojangson

mojangson

JSON →
library0.2.1jsnpmunverified

Mojangson is a JavaScript library for parsing, simplifying, stringifying, and normalizing Mojangson (Minecraft's data tag format). It provides functionality to convert Mojangson strings into JavaScript objects, and vice-versa, handling its unique deviations from standard JSON. Key differences include support for indexed arrays, trailing commas in arrays and objects, unquoted strings (for keys and values), and numbers with type suffixes (e.g., `5b`, `100l`). The current stable version is 2.0.4. Releases are infrequent, focusing on bug fixes and grammar improvements. This library differentiates itself by accurately implementing the specific Mojangson grammar, making it essential for applications interacting with Minecraft's NBT data tags or command syntax, where standard JSON parsers would fail due to the non-standard syntax.

npm install mojangson
INSTALL
IMPORT
SIG · MOJANGSON
M
mojangson
serializationjavascriptv0.2.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

mojangson
import mojangson from 'mojangson';
const { parse } = require('mojangson');
The package exports a default object containing all functions for both CJS and ESM. Destructuring directly from `require` or `import` the top-level object is the intended pattern.
parse
import { parse } from 'mojangson';
import mojangson from 'mojangson'; const data = mojangson.parse();
While the default export pattern `mojangson.parse` works, named imports are also supported for individual functions.
stringify
const { stringify } = require('mojangson');
const stringify = require('mojangson').stringify;
CommonJS `require` supports direct destructuring for named exports. `stringify` expects a specific object format including 'type' and 'value' properties, not a plain JavaScript object.

Demonstrates parsing, simplifying, stringifying, and normalizing Mojangson strings, highlighting the library's core functionalities.

const mojangson = require('mojangson'); // Parsing a Mojangson string const parsedData = mojangson.parse('{mykey:myvalue, number:5b, list:[0:"item1",1:"item2"]}'); console.log('Parsed Data:', JSON.stringify(parsedData, null, 2)); // Expected output: { type: 'compound', value: { mykey: { type: 'string', value: 'myvalue' }, number: { type: 'byte', value: 5 }, list: { type: 'list', value: { type: 'string', value: [ 'item1', 'item2' ] } } } } // Simplifying the parsed data (loses type information) const simplifiedData = mojangson.simplify(parsedData); console.log('Simplified Data:', JSON.stringify(simplifiedData, null, 2)); // Expected output: { mykey: 'myvalue', number: 5, list: [ 'item1', 'item2' ] } // Stringifying a JavaScript object (with types) back to Mojangson const dataToConvert = { type: 'compound', value: { message: { type: 'string', value: 'Hello' }, count: { type: 'int', value: 123 } } }; const mojangsonString = mojangson.stringify(dataToConvert); console.log('Stringified Mojangson:', mojangsonString); // Expected output: {message:"Hello",count:123} // Normalizing Mojangson to its shortest equivalent form const originalMojangson = '[0:"z1",1:"z2",]'; const normalizedMojangson = mojangson.normalize(originalMojangson); console.log('Normalized Mojangson:', normalizedMojangson); // Expected output: [z1,z2]
Debug
Known issues
breakingThe output format of the `parse` function changed significantly in version 2.0.0. It now returns an object including 'type' properties for each value, rather than a plain JavaScript object. This allows for round-tripping data with the `stringify` function.
fix
Update consuming code to expect the new structured output (e.g., `data.value.mykey.value` instead of `data.mykey`). If a simplified representation is needed, use `mojangson.simplify(parsedData)`.
affects: >=2.0.0
gotchaThe `mojangson.simplify` function removes type information (e.g., `5b` becomes `5`). The simplified output cannot be directly used with `mojangson.stringify` to recreate the original Mojangson string with its specific types.
fix
Use `mojangson.parse` for initial parsing and work with the structured output if type fidelity is required for stringification. Only use `simplify` when a plain JavaScript object is sufficient and type information is not critical.
affects: >=2.0.0
gotchaThe `mojangson.stringify` function expects an input JavaScript object that mimics the structured output of `mojangson.parse`, meaning each value must be wrapped in an object containing `type` and `value` properties. It does not accept plain JavaScript objects.
fix
Construct your input objects with explicit `type` properties (e.g., `{ type: 'string', value: 'hello' }` or `{ type: 'compound', value: { key: { type: 'int', value: 123 } } }`) to match the expected format for `stringify`.
affects: >=2.0.0
gotchaMojangson deviates from JSON in several ways, including unquoted keys/values, typed numbers (e.g., `100s`, `5f`), indexed arrays (e.g., `[0:"item"]`), and trailing commas. Standard JSON parsers will fail on these constructs.
fix
Always use `mojangson.parse` when dealing with Mojangson strings. Do not attempt to use `JSON.parse()` as it is incompatible.
affects: *
Errors
Common errors & fixes
Error: Unexpected character at position X in NBT string
The input string is not valid Mojangson according to the defined grammar. This could be due to malformed syntax, incorrect type suffixes, or invalid characters.
fix
Review the Mojangson string for compliance with the Mojangson specification. Check for correct curly braces, square brackets, colons, commas, and ensure values are properly formatted. Refer to the Mojangson specification (e.g., Minecraft Wiki Data tags) for correct syntax.
TypeError: Cannot read properties of undefined (reading 'value') during simplification
Attempting to simplify an array that was not correctly parsed or structured, potentially due to an internal parser error or an unexpected input format.
fix
Ensure the input to `mojangson.simplify` is the direct output of `mojangson.parse`. This error was specifically addressed in version 2.0.3, so upgrading to 2.0.3 or higher should resolve issues related to simplifying arrays.
Input string starting with a number parsed incorrectly as a number type, when it should be a string.
Prior to version 2.0.2, the parser had a bug where strings that began with numeric characters would be incorrectly identified and parsed as a number type, rather than a string.
fix
Upgrade the `mojangson` package to version 2.0.2 or newer to ensure correct parsing of strings that may begin with numeric characters.
Upgrade
Version history
0.2.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
6 hits · last 30 days
node
6
Resources
mojangson — npm install mojangson · libregistry