Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Michael Z
Michael Z

Posted on • Originally published atmichaelzanggl.com

     

JSON Extensions for writing config files and parsing data

JSON is a rather simple format, this makes it easy to use, understand, and parse.
But sometimes you need a bit more than that, let's look at some common JSON extensions.

EJSON

Extended JSON is exactly what it sounds like. This dialect of JSON supports additional data types like dates and even lets you add your own types.

Here's an example showing the difference between regular JSON and EJSON when it comes to serializing and re-parsing objects containing dates.

constEJSON=require('ejson')constevent={name:'test',created_at:newDate()}conststingifiedJSON=JSON.stringify(event)conststingifiedEJSON=EJSON.stringify(event)// stringifiedconsole.log(stingifiedJSON)// {"name":"test","created_at":"2024-11-06T00:00:00Z"}console.log(stingifiedEJSON)// {"name":"test","created_at":{"$date":1730851200000}}// re-parsedconsole.log(typeofJSON.parse(stingifiedJSON).created_at)// stringconsole.log(EJSON.parse(stingifiedEJSON).created_atinstanceofDate)// true
Enter fullscreen modeExit fullscreen mode

This is extremely useful for re-parsing serialized data, for example when sending it over the network.
EJSON is what MongoDB uses under the hood (in addition to BSON, a binary format of JSON).

Among the JSON extensions listed in this article, this is the only one that regular JSON is able to parse as well!

NDJSON

Newline-Delimited JSON is commonly used for streaming one JSON object at a time.

This is how it looks like:

{"name":"test","created_at":"2024-11-02T00:00:00Z"}{"name":"test 2","created_at":"2024-11-04T00:00:00Z"}{"name":"test 3","created_at":"2024-11-06T00:00:00Z"}
Enter fullscreen modeExit fullscreen mode

What's different to regular JSON is the lack of wrapping the data in an array and the lack of commas after each line (since it's newline delimited). This makes it a perfect candidate for streaming.

Apart from streaming JSON, I've also seen this format being used (together with EJSON) in NoSQL clients to export and import documents.

JSONC / JSON5

For the next one there's a variety of implementations each adding its own flavor. Let's look atJSON5 as it's the most complete one.

They all try to achieve the same goal: a human-readable, loosened JSON format for managing config files.

JSON5 allows you to add comments, trailing commas, line breaks, unquotes keys, among many other things.

{// You can add comments!!unquoted:'< unquoted keys!',singleQuotes:'I can use "double quotes" here',lineBreaks:"Supports line breaks!\No\\n's!",}
Enter fullscreen modeExit fullscreen mode

As you can see, JSON extensions come in all shapes and sizes. Some meant to be used by applications, others allow devs to more easily create config files.
Next time you need to serialize/parse JSON, see if one of those extensions makes sense for your use case!

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Software writer
  • Location
    Tokyo
  • Joined

More fromMichael Z

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp