Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON.stringify without quotes on properties?

I'm using a service which uses incorrect JSON format (no double quotes around properties). So I need to send

{ name: "John Smith" } instead of { "name": "John Smith" }

This format cannot be changed as this is not my service.

Anyone know of a stringify routing to format an JavaScript object like above?

like image 660
jadent Avatar asked Jun 27 '12 19:06

jadent


People also ask

How do I remove quotes from JSON Stringify?

That makes the regex to remove the quotes from the keys MUCH easier. Start your solution with this: var cleaned = JSON. stringify(x, null, 2);

Are quotes necessary in JSON?

JavaScript object literals do not require quotes around a key name if the key is a valid identifier and not a reserved word. However, JSON always requires quotes around key names.

Does JSON Stringify escape quotes?

JSON. stringify does not act like an "identity" function when called on data that has already been converted to JSON. By design, it will escape quote marks, backslashes, etc. You need to call JSON.

Is it bad to use JSON Stringify?

It`s ok to use it with some primitives like Numbers, Strings or Booleans. As you can see, you can just lose unsupported some data when copying your object in such a way. Moreover, JavaScript won`t even warn you about that, because calling JSON. stringify() with such data types does not throw any error.


2 Answers

This simple regular expression solution works to unquote JSON property names in most cases:

const object = { name: 'John Smith' };  const json = JSON.stringify(object);  // {"name":"John Smith"}  console.log(json);  const unquoted = json.replace(/"([^"]+)":/g, '$1:');  console.log(unquoted);  // {name:"John Smith"}

Extreme case:

var json = '{ "name": "J\\":ohn Smith" }' json.replace(/\\"/g,"\uFFFF");  // U+ FFFF json = json.replace(/"([^"]+)":/g, '$1:').replace(/\uFFFF/g, '\\\"'); // '{ name: "J\":ohn Smith" }' 

Special thanks to Rob W for fixing it.

Limitations

In normal cases the aforementioned regexp will work, but mathematically it is impossible to describe the JSON format with a regular expression such that it will work in every single cases (counting the same number of curly brackets is impossible with regexp.) Therefore, I have create a new function to remove quotes by formally parsing the JSON string via native function and reserialize it:

function stringify(obj_from_json) {     if (typeof obj_from_json !== "object" || Array.isArray(obj_from_json)){         // not an object, stringify using native function         return JSON.stringify(obj_from_json);     }     // Implements recursive object serialization according to JSON spec     // but without quotes around the keys.     let props = Object         .keys(obj_from_json)         .map(key => `${key}:${stringify(obj_from_json[key])}`)         .join(",");     return `{${props}}`; } 

Example: https://jsfiddle.net/DerekL/mssybp3k/

like image 90
Derek 朕會功夫 Avatar answered Oct 09 '22 03:10

Derek 朕會功夫


It looks like this is a simple Object toString method that you are looking for.

In Node.js this is solved by using the util object and calling util.inspect(yourObject). This will give you all that you want. follow this link for more options including depth of the application of method. http://nodejs.org/api/util.html#util_util_inspect_object_options

So, what you are looking for is basically an object inspector not a JSON converter. JSON format specifies that all properties must be enclosed in double quotes. Hence there will not be JSON converters to do what you want as that is simply not a JSON format.Specs here: https://developer.mozilla.org/en-US/docs/Using_native_JSON

Object to string or inspection is what you need depending on the language of your server.

like image 32
fino Avatar answered Oct 09 '22 04:10

fino