Why JSON allows only string to be a key? - javascript

Why does JSON only allow a string to be a key of a pair? Why not other types such as null, number, bool, object, array? Considering JSON is tightly related with JavaScript, could I conclude the reason from JavaScript specification (ECMA-262)? I'm totally a newbie to JavaScript, could you help me to point it out.

The JSON format is deliberately based on a subset of JavaScript object literal syntax and array literal syntax, and JavaScript objects can only have strings as keys - thus JSON keys are strings too. (OK, you can sort of use numbers as JavaScript object keys, but really they get converted to strings.)
Note that the point of JSON is that it is a string representation of data to allow easy exchange between programs written in different languages running on different machines in different environments. If you wanted to use an object as a key then that object would in turn have to be somehow represented as a string for transmission, but then the receiving language would need to be able to use objects as keys and that would mean you'd need a limited subset of JSON for those languages which would just be a mess.
"Considering JSON is a part of JavaScript"
No, it isn't. Newer browsers provide methods for creating and parsing JSON, but they're not part of the language as such except that JSON is a string format and JavaScript can do strings. JSON is always a string representation - it has to be parsed to create an object for use within JavaScript (or other languages) and once that happens JavaScript (or the other languages) treat the resulting object the same as any other object.
(Note also that a particular bit of JSON doesn't necessarily have any keys at all: it could just be an array, like '["one","two","three"]'.)

Main reason according to the discoverer of JSON representation is,
while parsing JSON data, there is a chance/possibility that, key
you are using to refer a value might be a reserved word in your
parsing language.
Refer this talk by Douglas Crockford, who is the discoverer of JSON representation.
Example :
{
id: 1234,
name: "foo",
do: "somthing"
}
Since JSON is a cross language compatibility, We can use this data set in many languages. But, the word do is a keyword in Javascript. It will end up in syntax error while parsing.

Because that is the way the specification was written.

Related

How are functions represented internally in Javascript/Node.js?

I recently found out that JSON values can only store string, number, object, array, true, false or null. But from my understanding, JSON is how Javascript represents its objects internally. I don't understand how it is possible to store Javascript objects as JSON if most objects have methods, which are functions? Aren't functions objects? What the heck are functions in my Javascript interpreter's (Node.js) opinion and how does it represent them? Thanks!
JSON is a string interchange format. It stands for JavaScript Object Notation. It was invented long after Javascript and has nothing at all to do with how Javascript stores data internally.
JSON is typically used as an interchange format or storage format. One would take some Javascript data, serialize it to the JSON format and take the resulting string and send it to another process or computer or save it in some sort of storage.
The recipient of the JSON can then parse it back into whatever their local data is. JSON is even used to send data from a Javascript program to a program written in another language (Python, Ruby, C++, etc...).
Functions have no connection at all to JSON. They are not stored in JSON. Their internal storage format inside the JS interpreter is specific to whatever interpreter implementation and is not accessible to the outside world or governed by any standard. It's an implementation detail for any Javascript engine and they can do it however they want and each interpreter likely has it's own implmentation or variation. I don't know of any reason why it would matter to your Javascript code.
I recently found out that JSON values can only store string, number, object, array, true, false or null. But from my understanding, JSON is how Javascript represents its objects internally.
That is not correct. JSON is not something that the Javascript interpreter uses for its objects internally. Internal object formats are specific to a particular Javascript interpreter and are not accessible to Javascript code, nor really relevant when writing code.
I don't understand how it is possible to store Javascript objects as JSON if most objects have methods, which are functions?
Javascript does not use JSON for internal storage so it has nothing at all to do with the internal implementation of Javascript data types.
Aren't functions objects?
Yes, but they have nothing to do with JSON.
What the heck are functions in my Javascript interpreter's (Node.js) opinion and how does it represent them?
Each JS interpreter has its own internal implementation/storage for functions. It is not governed by any standard and is largely irrelevant to how you write code in Javascript.
If you had some reason to want to know how a specific Javascript implementation stores its variables internally, you would have to look into the source code. The V8 implementation from Google (used in Chrome and node.js) and the Firefox implementation from Mozilla are both open source and you could dive into that code (it would be mostly C++ code).
This can get pretty complicated because some data types such as Arrays are stored in a variety of different formats depending upon the structure of the array. I believe V8 has at least three storage formats for arrays depending upon whether the array is compacted or sparse and based on its overall size. This is to optimize for both memory consumption and run-time performance.
Likewise properties on objects may be arranged in highly optimized storage formats if the interpreter has advance information from the code about what is being used and what is not, compared to arbitrary programmatically generated properties.
FYI, you can find the Google repository here: https://chromium.googlesource.com/v8/v8.git and the Mozilla code here: https://hg.mozilla.org/.

QT what are the QML/C++ data types which can be converted to JSON?

According to http://doc.qt.io/qt-5/qtwebchannel-javascript.html
Furthermore keep in mind that only QML/C++ data types which can be converted to JSON will be (de-)serialized properly and thus accessible to HTML clients.
What are those data types which can be converted to JSON?
Is the QJsonObject or QJsonDocument included on it?
You can look into the documentation for classes such as QJsonValue and QJsonObject and see which types and classes can be used by constructors or by from*(...) functions, which are usually static and ask for a QVariant/QVariantHash/QVariantMap.
Given that in Qt JavaScript an array can be converted into a QList<> and an object to a QVariantMap, I would guess those (and basic types such as int, float, string...) should be passed to the C++ side and made into QJson(Value/Object/Array) then.
Depending on what you want, a QJsonObject could be, for example, formatted as a string like this. For further information, JSON support in Qt.

What else can you call a JSON object - is it hash map? - conceptual questions

One thing bothered me quite a bit when learning Javascript while trying to incorporate CS concepts - what exactly is JSON? I heard of people calling it associative arrays, key value pair objects, Javascript Objects.
Correct me if I'm wrong - I know in Java Hash Maps are more or less key value pairs. Since I feel like JSON shares a lot of properties with that, can I safely call JSON objects hash maps without sounding like an idiot in interviews?
Thanks
No. You could call it a lightweight human readable structured text document. Human readable like xml, but not as verbose hence lightweight. You might compare it to other data encoding mechanisms like protocol buffers, yaml or (the previously mentioned) XML.
JSON - JavaScript Object Notation
Data objects consisting of attribute-value pairs.

Quickest way of checking JSON equivalence

I have two web services generating JSON output. Both services are using different technologies but are supposed to generate the exact same output.
I want to check if this output is exactly the same in the browser environment. I would really prefer to simple compare them as string, but the JSON output is not sorted.
I can convert JSON string to objects and then iterate over their keys to check the equivalence but that is basically a n^2 algorithm.
I was wondering if there is any quicker or better way. Something that browser environment already provides.
At the moment there is no such method provided by the browsers to do a deep comparison. WE have to do it ourselves or using some non native library.

How to stringify a whole Javascript Object including __proto__ properties?

I am sorry if this is a duplicate, so far I couldn't find the same question.
I have an Object with various methods in my __proto__ member.
Let's call the type of this object myObjectType.
Later on I have to do a JSON.stringify(myObjectType). The problem is that then when I build my object from the previous obtained JSON string the type of my Object is plain Object, I lost all the methods I had.
Does any one see why ?
search google for javascript object serialization.
GSerializer library
There's no standardized way of incorporating functions into JSON data. You can do something yourself — that is, write your own JSON serializer that incorporates functions according to some convention — but with straight standard JSON you get numbers, strings, booleans, and null, plus of course objects with named properties and arrays. No functions, just data.
I'd highly recommend Douglas Crockford's libraries:
https://github.com/douglascrockford/JSON-js

Categories

Resources