- JSON5 – JSON for Humans
- Summary of Features
- Objects
- Arrays
- Strings
- Numbers
- Comments
- White Space
- Example
- Specification
- Installation and Usage
- Node.js
- Javascript JSON data manipulation library
- 3 Answers 3
- Saved searches
- Use saved searches to filter your results more quickly
- License
- confetti/yayson
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
- About
JSON5 – JSON for Humans
JSON5 is an extension to the popular JSON file format that aims to be easier to write and maintain by hand (e.g. for config files). It is not intended to be used for machine-to-machine communication. (Keep using JSON or other file formats for that. 🙂)
JSON5 was started in 2012, and as of 2022, now gets >65M downloads/week, ranks in the top 0.1% of the most depended-upon packages on npm, and has been adopted by major projects like Chromium, Next.js, Babel, Retool, WebStorm, and more. It’s also natively supported on Apple platforms like MacOS and iOS.
Formally, the JSON5 Data Interchange Format is a superset of JSON (so valid JSON files will always be valid JSON5 files) that expands its syntax to include some productions from ECMAScript 5.1 (ES5). It’s also a subset of ES5, so valid JSON5 files will always be valid ES5.*
This JavaScript library is a reference implementation for JSON5 parsing and serialization, and is directly used in many of the popular projects mentioned above (where e.g. extreme performance isn’t necessary), but others have created many other libraries across many other platforms.
Summary of Features
The following ECMAScript 5.1 features, which are not supported in JSON, have been extended to JSON5.
Objects
Arrays
Strings
- Strings may be single quoted.
- Strings may span multiple lines by escaping new line characters.
- Strings may include character escapes.
Numbers
- Numbers may be hexadecimal.
- Numbers may have a leading or trailing decimal point.
- Numbers may be IEEE 754 positive infinity, negative infinity, and NaN.
- Numbers may begin with an explicit plus sign.
Comments
White Space
Example
// comments unquoted: 'and you can quote me on that', singleQuotes: 'I can use "double quotes" here', lineBreaks: "Look, Mom! \ No \\n's!", hexadecimal: 0xdecaf, leadingDecimalPoint: .8675309, andTrailing: 8675309., positiveSign: +1, trailingComma: 'in objects', andIn: ['arrays',], "backwardsCompatible": "with JSON", >
A more real-world example is this config file from the Chromium/Blink project.
Specification
For a detailed explanation of the JSON5 format, please read the official specification.
Installation and Usage
Node.js
Javascript JSON data manipulation library
I’m currently working on a project where I’m dealing with a fair amount of JSON data being transmitted backwards and forwards and stored by the browser as lists of javascript objects. For example:
person: < // Primary Key key: "id", // The actual records table: < "1": , "2": , // etc.. >, indexes: < // Arrays of pointers to records defined above "name": [ , // etc.. ] >
- Can store,
- retrieve,
- sort,
- and iterate through JSON data,
- with a clean API,
- minimal performance drag (Mobiles dont have a lot of computing power)
- and a small payload that is ideally
I might be asking for too much, but hopefully someone’s used something like this. The kind of thing I’m looking for the is the JSON equivalent of jQuery, maybe its not so outlandish.
3 Answers 3
It fullfill all the requirement pointed on question.
Provide traversing (like find,siblings, parent etc) and manipulation method like(value, append, prepend);
Provide a direct array sort method and sort method which run on jsonQ object. (Both sort method run recursively)
Its a trial to have same API for JSON as jQuery DOM APIs . So if you are familiar with jquery. Its easy to catch up. More over complete documentation of apis is available.
It create a new format on initialization of jsonQ for a JSON data which is used internally to traverse data which are more efficient. (Its like having all the loops at once, so you don’t have to make loop over loop to iterate each time that json).
minified version is 11.7 kb.
Actually your question is not good, I suppose. From your example one could see, that you’re trying to emulate SQL-like storage with JSON. Maybe, you just need to take IndexedDB?
jsonpath matches points 4-7 (and maybe 3) of your exact requirements and JSON global object allows 1 and 2 with just once call for each.
Also IMHO requirements are unreal, especially with the last one about size.
Hi Kirilloid, thanks for your response. IndexedDB has a very complex API, and it requires disk IO which is too much of a performance issue. The app is a single-page web app, so in-memory JSON provides by far the best performance. I agree the size requirement is quite limiting, but I dont think its impossible given that I’m only after the methods .get(), .set(), .index(), .each() and .data property for keeping track of the JSON.. I’ll take a look at jsonpath and let you know.
jsonpath is interesting, I like the idea of trying to emulate XPath, but I’m not sure its what I’m after since XPath is for un-structured documents and I’m more after in-memory SQL-like storage like you mentioned (where there is a data model and distinct entities).
I think Lawnchair is something you’re looking for. Its homepage says it is made with mobile in mind, however I’ve not used it before so I cannot comment on that.
It is simple key-value store, although you can define your own indexes, something like with CouchDB. I’ve not seen support for selectors, however there is a query plugin available, promising easy selection.
Something like jQuery relies on sizzle, which is CSS selector library, that isn’t applicable in your case. XPath is in my opinion your best bet, since it’s used primarily with XML, a tree structure. Since JSON object can be represented as a tree, I’ve found JSONSelect, a small library that supports JSON Xpath selectors in ‘CSS-ish’ way.
If you’d be able somehow to plug JSONSelect into Lawnchair, I think you’ve found a great combination 🙂
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
A library for serializing and reading JSON API data in JavaScript.
License
confetti/yayson
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
A library for serializing and reading JSON API data in JavaScript.
From version 3 we now support native JavaScript classes. YAYSON has zero dependencies and works in the browser and in node 14 and up.
Install yayson by running:
A basic Presenter can look like this:
const yayson = require('yayson') const Presenter > = yayson() class BikePresenter extends Presenter static type = 'bikes' > const bike = id: 5, name: 'Monark' >; BikePresenter.render(bike);
data: id: 5, type: 'bikes', attributes: id: 5, name: 'Monark' > > >
It also works with arrays, so if you send an array to render, «data» will be an array.
A bit more advanced example:
const yayson = require('yayson') const Presenter > = yayson() class WheelPresenter extends Presenter static type = 'wheels' relationships() return bike: BikePresenter > > > class BikePresenter extends Presenter static type = 'bikes' relationships() return wheels: WheelPresenter > > >
By default it is set up to handle standard JS objects. You can also make it handle Sequelize.js models like this:
const yayson = require('yayson') const Presenter > = yayson(adapter: 'sequelize'>)
You can also define your own adapter globally:
const yayson = require('yayson') const Presenter > = yayson(adapter: id: function(model) return 'omg' + model.id>, get: function(model, key) return model[key] > >>)
Take a look at the SequelizeAdapter if you want to extend YAYSON to your ORM. Pull requests are welcome. 🙂
You can add metadata to the top level object.
ItemsPresenter.render(items, meta: count: 10>)
meta: count: 10 > data: id: 5, type: 'items', attributes: id: 5, name: 'First' > > >
You can use a Store can like this:
const Store> = require('yayson')(); const store = new Store(); const data = await adapter.get(path: '/events/' + id>); const event = store.sync(data);
This will give you the parsed event with all its relationships.
Its also possible to find in the synched data:
const event = this.store.find('events', id) const images = this.store.findAll('images')
Recommended way is to use it via webpack or similar build system wich lets you just require the package as usual.
If you just want to try it out, copy the file dist/yayson.js to your project. Then simply include it:
script src pl-s">./lib/yayson.js">script>
Then you can var yayson = window.yayson() use the yayson.Presenter and yayson.Store as usual.
Earlier versions of JSON API worked a bit different from 1.0. Therefore YAYSON provides legacy presenters and stores in order to have interoperability between the versions. Its used similar to the standard presenters:
const yayson = require('yayson/legacy') const Presenter > = yayson()
About
A library for serializing and reading JSON API data in JavaScript.