JSON.stringify key without quotes - javascript

Is it possible to remove the quotes from keys in JSON.stringify? Normally it will have quotes:
const object = { name: "Foo Bar", birthdate: { date: "2000-01-01", time: "12:34" } };
console.log(JSON.stringify(object, null, " "));
Output:
{
"name": "Foo Bar",
"birthdate": {
"date": "2000-01-01",
"time": "12:34"
}
}
What I want is something like this:
{
name: "Foo Bar",
birthdate: {
date: "2000-01-01",
time: "12:34"
}
}
Is this even possible, or do I have to create my own JSON serializer?

It sounds like you are looking for a data-serialization format that is human-readable and version-control-friendly but not as strict about quotes as JSON.
Such formats include:
Relaxed JSON (RJSON) (simple keys and simple values generally do not require quotes)
Hjson (simple keys and simple values generally do not require quotes)
YAML (keys and values generally do not require quotes)
JavaScript object literal (also printed out by many implementations of "console.dir()" when passed a JavaScript object; simple keys generally not required to be quoted, but string values must be quoted by either single quotes or double quotes)
for completeness:
JSON (requires double-quotes around keys, also called property names, and requires double-quotes around string data values).

I've used the below NPM package to achieve this.
https://www.npmjs.com/package/stringify-object

Yes, it is possible to remove the quotes from the keys. Doing so will render it invalid as JSON, but still valid when used directly in JavaScript code, only if the keys you use are also valid variable names. When you paste a JSON object in JavaScript code, it may be useful to have the quotes removed, as they can in some cases take up a lot of data. If that's relevant to your project, then this is the answer for you.
The function below works for any JavaScript variable and works fine with objects/arrays containing objects/arrays. I've added comments to explain what's going on. Please note that this code does not check for possible reserved keywords that may not be allowed as JavaScript keys, such as "for".
function toUnquotedJSON(param){ // Implemented by Frostbolt Games 2022
if(Array.isArray(param)){ // In case of an array, recursively call our function on each element.
let results = [];
for(let elem of param){
results.push(toUnquotedJSON(elem));
}
return "[" + results.join(",") + "]";
}
else if(typeof param === "object"){ // In case of an object, loop over its keys and only add quotes around keys that aren't valid JavaScript variable names. Recursively call our function on each value.
let props = Object
.keys(param)
.map(function(key){
// A valid JavaScript variable name starts with a dollar sign (?), underscore (_) or letter (a-zA-Z), followed by zero or more dollar signs, underscores or alphanumeric (a-zA-Z\d) characters.
if(key.match(/^[a-zA-Z_$][a-zA-Z\d_$]*$/) === null) // If the key isn't a valid JavaScript variable name, we need to add quotes.
return `"${key}":${toUnquotedJSON(param[key])}`;
else
return `${key}:${toUnquotedJSON(param[key])}`;
})
.join(",");
return `{${props}}`;
}
else{ // For every other value, simply use the native JSON.stringify() function.
return JSON.stringify(param);
}
}

A JSON string is a string type basically, a common language that Servers understand, that's why we need to send JSON to the Servers for further processing. What you are trying to get is an object in javascript.
But you already have that object to work with.
const object = { name: "Foo Bar", birthdate: { date: "2000-01-01", time: "12:34" } };

Related

single quotes for string keys in javascript object

In nodejs I was creating a list of objects with each object supposed to have a string key and a number as value. I am able to create this without issues but while trying to log this with console.log() . I could see some keys did not have a single quotes surrounding the key while some did.
eg
[
{ '2d22f294': 0 },
{ b6d108da: 0 },
{ b17562ff: 0 },
{ '0e4a0beb': 0 }
]
Could anyone explain what does it mean to have a single quotes surrounding the key , I was of the assumption this meant the key was a string but even the ones without single quotes around the keys are strings and act like strings.
If it's starts with a number, then printing must show you valid javascript and quotes tell you that the key is a string (not a number).
If you have an object like this
let object = {
name: 'Jim',
25: 'Age'
}
You can access the properties in the following way:
for the namekey ... object.name or object['name'].
for the key 25 ... object[25] or object['25']
This is because properties cannot start with a number in js. Therefore you have to access them with brackets.

How to create an object from a string containing initializer

I'm reading Object initializer strings from a database but can't see any easy way to turn them back into objects.
For example, given the following string, how would you turn it into an object?
var initializer = "{type: car, colour: red, engine: 2.0L}";
I ended up decoding them by just looping through piece by piece but felt that there must be a better way.
since your data isn't quoted there won't be a difference between datatypes everything must be parsed as string.
Instead of trying to parse this shit i would try to create valid parsable data in the first place.
In the unlikely case that, instead of returning JSON from the server as you should, you do end up parsing this yourself, here's how you might do that:
initializer
.replace(/^\{|\}$/g, '') // remove {} at beginning and end
.split(',') // break apart into key: val pairs
.reduce(function(result, keyval) { // build object
var parts = keyval.split(':').map(''.trim);
result[parts[0]] = parts[1];
return result;
}, {});
Of course, you'd probably want to add a bunch of bullet-proofing to that.
If you're using Underscore, you could use its ability to create an object from an array of [key, val] pairs:
_.object(initializer
.replace((/^\{|\}$/g, '')
.split(',')
.map(function(keyval) {
return keyval.split(':').map(''.trim);
})
);

Is there any difference between specifying a json's key with and without quotes?

Is there any difference between the following title keys?
jsonObj = {
title: "hello"
}
and
jsonObj = {
"title": "world"
}
As a Javascript object literal (as Javascript code), they're identical.
As the JSON data format, only with quotes is valid JSON.
JSON is a strict subset of Javascript syntax, they're not the same thing.
Both are valid javascript objects, but only second one is a valid JSON object.
Preferably use the second one, that also helps when property name is a keyword, or has special characters in it .e.g ":" or space.
jsonObj = {
"my second key::second range":99
}

Javascript object Vs JSON

I want to understand the basic differences clearly between Javascript object and JSON string.
Let's say I create the following JS variable:
var testObject = {one: 1,"two":2,"three":3};
Q1. Is the key/property name valid both with/without quotes? (e.g. "one" : 1)
If yes, what is the difference?
Q2: If I convert the above object using JSON.stringify(testObject), what’s the difference between the original JS object and the JSON?
I feel they are almost the same. Please elaborate on this.
Q3: For parsing a JSON string, is the method below recommended?
var javascriptObj = JSON.parse(jSonString);
Is the key/property name valid both with/without quotes ?
The only time you need to enclose a key in quotes when using Object Literal notation is where the key is a reserved word or contains a special character (if, :, - etc). It is worth noting that a key in JSON must be enclosed in double quotes.
If I convert the above object to JSON using var jSonString = JSON.stringify(testObject);, what is the difference between the 2 (JS obj and JSON)?
JSON is a data interchange format. It's a standard which describes how ordered lists and unordered maps, strings, booleans and numbers can be represented in a string. Just like XML and YAML is a way to pass structured information between languages, JSON is the same. A JavaScript object on the other hand is a physical type. Just like a PHP array, a C++ class/ struct, a JavaScript object is a type internal to JavaScript.
Here's a story. Let's imagine you've purchased some furniture from a store, and you want it delivered. However the only one left in stock is the display model, but you agree to buy it.
In the shop, the chest-of-drawers you've purchased is a living object:
var chestOfDrawers = {
color: "red",
numberOfDrawers: 4
}
However, you can't send a chest-of-drawers in the post, so you dismantle it (read, stringify it). It's now useless in terms of furniture. It is now JSON. Its in flat pack form.
{"color":"red","numberOfDrawers":4}
When you receive it, you then rebuild the chest-of-drawers (read, parse it). Its now back in object form.
The reason behind JSON, XML and YAML is to enable data to be transferred between programming languages in a format both participating languages can understand; you can't give PHP or C++ your JavaScript object directly; because each language represents an object differently under-the-hood. However, because we've stringified the object into JSON notation; i.e. a standardised way to represent data, we can transmit the JSON representation of the object to another language (C++, PHP), they can recreate the JavaScript object we had into their own object based on the JSON representation of the object.
It is important to note that JSON cannot represent functions or dates. If you attempt to stringify an object with a function member, the function will be omitted from the JSON representation. A date will be converted to a string;
JSON.stringify({
foo: new Date(),
blah: function () {
alert('hello');
}
}); // returns the string "{"foo":"2011-11-28T10:21:33.939Z"}"
For parsing a JSON string, is the method below recommended? var javascriptObj = JSON.parse(jSonString);
Yes, but older browsers don't support JSON natively (IE <8). To support these, you should include json2.js.
If you're using jQuery, you can call jQuery.parseJSON(), which will use JSON.parse() under the hood if it's supported and will otherwise fallback to a custom implementation to parse the input.
Q1: When defining object literals in javascript, the keys may include quotes or not. There is no difference except that quotes allow you to specify certain keys that would cause the interpreter to fail to parse if you tried them bare. For example, if you wanted a key that was just an exclamation point, you would need quotes:
a = { "!": 1234 } // Valid
a = { !: 1234 } // Syntax error
In most cases though, you can omit the quotes around keys on object literals.
Q2: JSON is literally a string representation. It is just a string. So, consider this:
var testObject = { hello: "world" }
var jSonString = JSON.stringify(testObject);
Since testObject is a real object, you can call properties on it and do anything else you can do with objects:
testObject.hello => "world"
On the other hand, jsonString is just a string:
jsonString.hello => undefined
Note one other difference: In JSON, all keys must be quoted. That contrasts with object literals, where the quotes can usually be omitted as per my explanation in Q1.
Q3. You can parse a JSON string by using JSON.parse, and this is generally the best way to do it (if the browser or a framework provides it). You can also just use eval since JSON is valid javascript code, but the former method is recommended for a number of reasons (eval has a lot of nasty problems associated with it).
Problems solved by JSON
Let's say you want to exchange regular JavaScript objects between two computers, and you set two rules:
The transmitted data must be a regular string.
Only attributes can be exchanged, methods are not transmitted.
Now you create two objects on the first host:
var obj1 = { one: 1,"two":2,"three":3 }; // your example
var obj2 = { one: obj1.one, two: 2, three: obj1.one + obj1.two };
How can you convert those objects into strings for transmission to the second host?
For the first object, you could send this string obtained form the literal definition '{ one: 1,"two":2,"three":3 }', but actually you can't read the literal in the script portion of the document (at least not easily). So obj1 and obj2 must actually be processed the same way.
You need to enumerate all attributes and their value, and build a string similar to the object literal.
JSON has been created as a solution to the needs just discussed: It is a set of rules to create a string equivalent to an object by listing all attributes and values (methods are ignored).
JSON normalizes the use of double-quotes for attribute names and values.
Remember that JSON is a set of rules only (a standard).
How many JSON objects are created?
Only one, it is automatically created by the JS engine.
Modern JavaScript engines found in browsers have a native object, also named JSON. This JSON object is able to:
Decode a string built using JSON standard, using JSON.parse(string). The result is a regular JS object with attributes and values found in the JSON string.
Encode attributes / values of a regular JS object using JSON.stringify(). The result is a string compliant with the JSON set of rules.
The (single) JSON object is similar to a codec, it's function is to encode and decode.
Note that:
JSON.parse() doesn't create a JSON object, it creates a regular JS object, there is no difference between an object created using an object literal and an object created by JSON.parse() from a JSON-compliant string.
There is only one JSON object, which is used for all conversions.
Going back to the questions:
Q1: The use of single of double quotes is allowed for object literals. Note that the quotes are used optionally for attributes names, and are mandatory for string values. The object literal itself is not surrounded by quotes.
Q2: Objects created from literals and using JSON.parse() are strictly the same. These two objects are equivalent after creation:
var obj1 = { one: 1, "two": 2, "three": 3 };
var obj2 = JSON.parse('{ "one": "1", "two": "2", "three": "3" }');
Q3: On modern browsers JSON.parse() is used to create a JS object from a JSON-compliant string. (jQuery has also an equivalent method that can be used for all browsers).
Q1 - in JS you only need to use quotes if the key is a reserved word or if it would otherwise be an illegal token. In JSON you MUST always use double quotes on key names.
Q2 - the jsonString is a serialised version of the input object ...
Q3 - which may be deserialised to an identical looking object using JSON.parse()
Question already has good answers posted, I am adding a small example below, which will make it more easy to understand the explanations given in previous answers.
Copy paste below snippet to your IDE for better understanding and comment the
line containing invalid_javascript_object_no_quotes object declaration to avoid compile time error.
// Valid JSON strings(Observe quotes)
valid_json = '{"key":"value"}'
valid_json_2 = '{"key 1":"value 1"}' // Observe the space(special character) in key - still valid
//Valid Javascript object
valid_javascript_object_no_quotes = {
key: "value" //No special character in key, hence it is valid without quotes for key
}
//Valid Javascript object
valid_javascript_object_quotes = {
key:"value", //No special character in key, hence it is valid without quotes for key
"key 1": "value 1" // Space (special character) present in key, therefore key must be contained in double quotes - Valid
}
console.log(typeof valid_json) // string
console.log(typeof valid_javascript_object_no_quotes) // object
console.log(typeof valid_javascript_object_quotes) // object
//Invalid Javascript object
invalid_javascript_object_no_quotes = {
key 1: "value"//Space (special character) present in key, since key is not enclosed with double quotes "Invalid Javascript Object"
}

JSON syntax for property names

What is the correct syntax to create objects in javascript that will work across the majority of web browsers (by that I mean : IE 6+, Firefox 2+, Opera 9+ )
Is this valid
var a = {
"class": "Person",
"name": "William Shakespeare",
"birthday": -12802392000000,
"nickname": "Bill"
};
Or is this:
var a = {
class: "Person",
name: "William Shakespeare",
birthday: -12802392000000,
nickname: "Bill"
};
And what is the difference between the two?
#AndreasN is correct: the JSON specification dictates the use of quotes in order for it to actually be JSON. If you don't use quotes, it may be a valid object literal in Javascript, but it is not JSON. Other services besides browser-side Javascript use JSON (e.g. webservices using php, Java, etc.) and if you construct a string that lacks the quotes, there is no guarantee that it will be parsed correctly -- although I suspect most implementations would be robust enough to do so.
FYI it is dangerous in Javascript to directly use eval() on JSON strings from sources which you cannot prevent malicious attacks. Again, see the JSON site which gives more of an explanation as well as a very short javascript file which safely parses JSON strings into Javascript objects.
edit: I guess technically your original question is not about JSON but rather the Javascript's syntax for object literals. The difference is that objects constructable from a JSON string will exclude many other possible object literals, e.g.:
var a = {cat: "meow", dog: "woof"};
var aname = {cat: "Garfield", dog: "Odie"};
var b = {
counter: 0,
pow: function(x) { return x+1; },
zap: function(y) { return (counter += y); }
};
var c = {
all: [a,aname],
animals: a,
names: aname,
};
Object literals "a" and "aname" can be expressed in JSON (by adding quotes to the property names). But object literals "b" and "c" cannot. Object literal "b" contains functions (not allowed in JSON). Object literal "c" above contains references to other variables in a way that is not representable in JSON because some of the references are shared. If you make a change to c.names it will also change c.all[1] since they share a reference to the same variable. JSON can only express objects that have a tree structure (e.g. each sub-element of the overall object is independent).
The spec say to use "".
Firefox accept without, but IE doesn't.
Pair is defined as
string : value
Value can be a string
string is defined as
" chars "
If IE fails with your second example it's because 'Class' is a reserved word (only in IE). Generally speaking it's always best to enclose your property names with quotes - doing this means it will ALWAYS work, whatever the circumstance.
You have to use "quotes" around properties that contain anything other than word characters
{foo-bar: 3} // invalid
{"foo-bar": 3} // valid
Other than that there is no difference between the two and I'd imagine both ways work across all browsers

Categories