-2

I have a JSON object like this

{
    "name": "Test Name",
    "age": 24
}

Is there a way I can convert this to a String in a format like

{
    name: "Test Name",
    age: 24
}

The JSON will be of varying lengths with different properties.

Right now, I am doing this as shown below. This can get too long and messy for larger and more complex JSON objects. I need to know if there is an easier and cleaner solution for this.

let cypherQueryObject = '{';
cypherQueryObject += ` name: "${user.name}";
if (user.age) { cypherQueryObject += `, age: "${user.age}"` };
cypherQueryObject = '}';
Ajil O.
  • 6,562
  • 5
  • 40
  • 72
  • Basically, you want to remove the quotes wrapping the keys? – 31piy Mar 27 '19 at 04:18
  • Can probably do as a string operation with Regex (see [JSON regex from other answer](https://stackoverflow.com/a/3845829/2064544)) – Tobias Mühl Mar 27 '19 at 04:18
  • @31piy Yeah. Basically. – Ajil O. Mar 27 '19 at 04:19
  • This is a vague requirement. Why do you want to do this? AFAIK, there is no _easier and cleaner_ solution for this, unless you convert the JSON to string, and do a RegExp replace everywhere, but that isn't fullproof either. – 31piy Mar 27 '19 at 04:20
  • @31piy I am guessing this should have been part of the post as well. I am writing a Cypher query for Neo4j GraphDB. To add node properties this is the format that is required as part of the query. – Ajil O. Mar 27 '19 at 04:22
  • 1
    @RobbyCornelissen The code here is an attempt to provide an example of how I am dealing with it right now. In my real code, most things like this have been taken care of, like booleans, numbers, other JSON or arrays. – Ajil O. Mar 27 '19 at 04:25
  • 2
    Parse with an AST parser and then re-assemble with a grammar slightly different from real JSON. [Popular parser](https://github.com/rse/json-asty) – Tobias Mühl Mar 27 '19 at 04:28
  • Either use @TobiasMühl's suggestion, or write your own logic to recursively visit and stringify all properties/elements of the object/array. – Robby Cornelissen Mar 27 '19 at 04:32
  • 2
    Apologies for the confusion with my answer, clearly I didn't read the question deeply enough. After some quick googling, I believe it's a possible duplicate of [JSON.stringify without quotes on properties?](https://stackoverflow.com/questions/11233498/json-stringify-without-quotes-on-properties) – JBallin Mar 27 '19 at 04:38
  • @JBallin Good find. – Robby Cornelissen Mar 27 '19 at 04:40

1 Answers1

1

The solution that you are looking for is little different than what someone expect. JavaScript's JSON.stringify() generates JSON string and a valid JSON contains " (double quotes only) around keys.

In your case, you are trying to use the JSON string without " around keys. So here is a little simple process to do that. Here I am assuming that you are going to use this in simple kind of JSON strings where the value part of any key don't have key: kind of things then it will work fine of bigger JSONs too.

If it is not like that then you will need to improve the find & replace utility in more efficient form. Regular expressions are great for this work.

Here I have tried to solve your problem like this.

I have used NODE REPL to execute statements so please ignore undefined returned by default.

> 
> let o = {
...     "name": "Test Name",
...     "age": 24
... }
undefined
> 
> s = JSON.stringify(o)
'{"name":"Test Name","age":24}'
> 
> s = JSON.stringify(o, undefined, 4)
'{\n    "name": "Test Name",\n    "age": 24\n}'
> 
> console.log(s)
{
    "name": "Test Name",
    "age": 24
}
undefined
> 
> for(k in o) {
... s = s.replace("\"" + k + "\":", k + ':')
... }
'{\n    name: "Test Name",\n    age: 24\n}'
> 
> console.log(s)
{
    name: "Test Name",
    age: 24
}
undefined
> 

you can have a look at this as well.

enter image description here

hygull
  • 8,464
  • 2
  • 43
  • 52
  • 1
    Sadly only works when used with objects as simple as the example. For example, this will fail if key or value include a `"` or if there are nested objects. – Tobias Mühl Mar 27 '19 at 04:38
  • 1
    Also note that some input JSON might have quotes around the `24` integer value – Tobias Mühl Mar 27 '19 at 04:39
  • 1
    Yeah, you are right, I have already mentioned that if @Ajil has any complex JSON then he/she will need to improve the code's find & replace utility. I just presented based on current i/p provided. Thank you. – hygull Mar 27 '19 at 04:41