6

I want to store some sql strings in a JSON file. This is how the JSON file looks in my ideal world

[
  "select t.index1, t.title, t.description, t.insertDate
  from myTable t
  join anotherTable t2 on t.index1 = t2.fIndex
  where t2.active = 1",
  ...
]

The obvious limitation is that JSON and javascript don't support multiline strings.

I want to avoid doing this:

[
  "select t.index1, t.title, t.description, t.insertDate\nfrom myTable t\njoin anotherTable t2 on t.index1 = t2.fIndex\nwhere t2.active = 1",
  ...
]    

I saw a guy that did this, which is not ideal either:

[
  ["select t.index1, t.title, t.description, t.insertDate",
  "from myTable t",
  "join anotherTable t2 on t.index1 = t2.fIndex",
  "where t2.active = 1"],
  ...
]

And then in his interpreter program he used myMultilineStringInArrayForm.join("\n").

I was wondering if anyone has any information about Template strings working (or planned to work in the future) for such purpose, like this:

[
  `select t.index1, t.title, t.description, t.insertDate
  from myTable t
  join anotherTable t2 on t.index1 = t2.fIndex
  where t2.active = 1`,
  ...
]

Notice I iused backticks (`) like in Template Literals from ES6, obviously the interpolation of variables would not work for stored JSON, but the multiline feature of this literals is what I am interested in.

Notice that this question is similar to this 6 year old question: Multiline strings in JSON

I asked again because I wanted to know if the ` syntax was planned or already worked since that time.

I appreciate the help with this

Community
  • 1
  • 1
santiago arizti
  • 4,175
  • 3
  • 37
  • 50
  • I would also like to avoid using XML. If there is no logical way of doing this in JSON then maybe my last resort would be XML, but in this question I want to explore only JSON alternatives. – santiago arizti Oct 05 '16 at 21:38
  • Possible duplicate of [Multiline strings in JSON](http://stackoverflow.com/questions/2392766/multiline-strings-in-json) – Michał Perłakowski Oct 05 '16 at 21:46
  • 2
    Template literals are purely part of JavaScript, not JSON. – Felix Kling Oct 05 '16 at 22:36
  • "*I want to store some sql strings in a JSON file.*" - what for? Why JSON? Where do the sql strings come from, are they really just *stored* in that file? – Bergi Oct 06 '16 at 01:35
  • it really is a more complicated schema, the JSON decision was not mine. it is a framework that stores its ui in JSON format and some elements in the framework (grids, etc) have sql statements to fill their rows. The sql statements currently are stored with `\n` and are pretty hard to edit without having the IDE opened, and git has a hard time reporting changes since one small change in the query causes the entire line to appear changed. Multiple reasons really, but that is irrelevant to the question itself, storing multiline strings in json, maybe with help of new technologies, like tpl lits – santiago arizti Oct 07 '16 at 15:05
  • @santiagoarizti I was interested in these new technologies. It took me way too long to figure out "tpl lits" was an abbreviation for template literals, not Tracking Protection Lists (as my web search adamantly informed me). – Ben J Feb 23 '17 at 15:46

2 Answers2

2

Looks like my best choice for having a readable storage format is XML. I was trying to stay away from XML and thought that JSON had multiline strings in its future due to the ` syntax, but no

santiago arizti
  • 4,175
  • 3
  • 37
  • 50
  • 2
    looks like yaml is the best fit for this. I will try to convince my team to switch to yaml. if you are interested and have no idea what yaml is, start here: https://www.json2yaml.com/ – santiago arizti May 05 '17 at 20:03
  • 1
    Was running into the same issues and YAML ended up being the perfect fit – Zachary Raineri Aug 27 '20 at 16:55
1

You may use JSON6 which supports backticks.

It seems to be a small one-person-project, so maybe the better supported JSON5 with support for Strings spanning multiple lines by escaping new line characters may be a better choice.

Sonson123
  • 10,879
  • 12
  • 54
  • 72
  • I found that YML was a better solution, I pitched the idea and it worked, so now we changed the parser to parse YML files instead of json files, and we didn't have to modify older files because yaml can parse json just fine – santiago arizti Sep 14 '18 at 16:30