-2

I have an issue where I have a file which contains my objects in the form

Dog1: {
  information: {
    color: 'red',
    name: 'Tom'
  }
}

and I am building a program which automatically populates my object from a file but the current output has the form

"Dog1": {
  "information": {
    "color": "red",
    "name": "Tom"
  }
}

My issue is that i need single or double quotes around "red" and "Tom" but i don't need it on everything else. I have tried using jscodeshift but cannot seem to get it working. Anybody got any tips for this?

Thank you very much for your time, Joe

  • 2
    The latter one is standard JSON, the former one is a javascript expression. Javascript will parse the latter just as the same. Why do you need the former format? How do you generate the objects? – Tamas Hegedus Jan 11 '16 at 09:53
  • i am working on a larger system and i just want to keep it the same for consistency , but also it is required to pass validation checks. While they are both valid my system requires the first one and I can't think of a way to do it which isn't a dirty hack. – joseph.allen Jan 11 '16 at 10:01
  • So you have these objects in javascript, and tryingto serialize it to a string of the given format (and possibly sending that to a server)? How did you generate the second output? – Tamas Hegedus Jan 11 '16 at 10:20
  • `newText = JSON.stringify(newParameter);` Then a file that is just a concat of newTexts which we use beautify on to format nicely `beautify(newDestFile, { indent_size: 2, unescape_strings: true, jslint_happy: true });` – joseph.allen Jan 11 '16 at 10:56
  • sidenote: `JSON.stringify(newParameter, null, 4)` would do the formatting – Tamas Hegedus Jan 11 '16 at 11:04

2 Answers2

0

Your object is a JavaScript object right? However the object is written into the file, it seems that it´s written in JSON format (JavaScript Object Notation).

In my opinion, thats not wrong. It´s the correct way to store JavaScript objects. If you really don´t want the the quotes, you maybe need to implement your own logic for writing the object into the file.

Update: See JSON.Stringify without quotes on properties?. That guy had the same problem and the solution was to remove the quotes after calling JSON.stringify().

Community
  • 1
  • 1
Franz Deschler
  • 2,456
  • 5
  • 25
  • 39
0

I quickly implemented a custom json serializer for you:

function _serialize_object_key(key) {
  return /^[a-zA-Z_\$][a-zA-Z_\$0-9]*$/.test(key) ? key : JSON.stringify(key);
}
function _serialize_object(obj, opts, current_indent) {
  var arr = [], inner_indent = current_indent + opts.indent_inc;
  for (var key in obj) {
    if (obj.hasOwnProperty(key)) {
      arr.push(inner_indent + _serialize_object_key(key) + ': ' + _json_like_stringify(obj[key], opts, inner_indent));
    }
  }
  return '{\n' + arr.join(',\n') + '\n' + current_indent + '}';
}

function _serialize_array(arr, opts, current_indent) {
  var inner_indent = current_indent + opts.indent_inc;
  return '[\n' + inner_indent + arr.map(inner_map).join(',\n' + inner_indent) + '\n' + current_indent + ']';
  function inner_map(x) {
    return _json_like_stringify(x, opts, inner_indent);
  }
}

function _json_like_stringify(x, opts, current_indent) {
  switch (typeof x) {
    case "number":
    case "string":
    case "boolean":
    default:
      return JSON.stringify(x);
    case "object":
      if (x === null) return 'null';
      if (Array.isArray(x)) return _serialize_array(x, opts, current_indent);
      return _serialize_object(x, opts, current_indent);
  }
}

function json_like_stringify(x, indent) {
  if (indent === undefined) indent = 2;
  var indent_inc = '';
  for (var i = 0; i < indent; i++) indent_inc += ' ';
  return _json_like_stringify(x, {indent_inc:indent_inc}, '');
}

$('pre').text(json_like_stringify({a:[1,2,3],b:{c:{d:[6,7,"asdasda\"fgdfgdfg",true,null, [1, 2, 3, {' ':' '}, 5, 6, 7, 8, 9, 10, 11]]}}}));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<pre></pre>
Tamas Hegedus
  • 28,755
  • 12
  • 63
  • 97