2

I'd like to save multiple json strings to a file and separate them by a delimiter, such that it will be easy to read this list in, split on the delimiter and work with each json doc separately.

Serializing using a json array is not an option due to external reasons.

I would like to use a delimiter that is illegal in JSON (e.g. delimiting using a comma would be a bad idea since there are commas within the json strings).

Are there any characters that are not considered legal in JSON serialized strings?

SpeksETC
  • 1,003
  • 2
  • 7
  • 13
  • See http://stackoverflow.com/questions/8676011/illegal-characters-in-object-or-json-key - Also see http://json.org, definition of "string" and "character". – Mörre Dec 18 '14 at 14:43
  • not really. json can encapsulate any character. maybe your library would encode some wonky multi-byte char into its \u1234-type representation, so you could use the raw char as your delimeter, but that's not guarateed to work everywhere. – Marc B Dec 18 '14 at 14:43

3 Answers3

1

I know it's not exactly what you needed, but you can use this SO answer to write the json string to a CSV, then read it on the other side by using a good streaming CSV reader such as this one

Community
  • 1
  • 1
rony l
  • 5,798
  • 5
  • 35
  • 56
1

NDJSON

Have a look at NDJSON (Newline delimited JSON).

http://ndjson.org/

It seems to me to be exactly how you should do things, though its not exactly what you asked for. (If you can't flatten your JSON objects into single lines then it's not for you though!) You asked for a delimiter that is not allowed in JSON. Newline is allowed in JSON, but it is not necessary for JSON to contain newlines.

The format is used for log files amongst other things. I discovered it when looking at the Lichess API documentation.

You can start listening in to a broadcast stream of NDJSON data part way through, wait for the next newline character and then start processing objects as and when they arrive.

If you go for NDJSON, you are at least following a standard and I think you'd be hard pressed to find an alternative standard to follow.

Example NDJSON

{"some":"thing"}
{"foo":17,"bar":false,"quux":true}
{"may":{"include":"nested","objects":["and","arrays"]}}
Ivan
  • 4,383
  • 36
  • 27
  • I'm not sure how this differs from JSONL, seems to be an equivalent standard on the surface at least. Worth checking out https://jsonlines.org/ – Vitim.us Jan 04 '23 at 22:27
0

An old question, but hopefully this answer will be useful.

Most JSON readers crash on this character: , which is information separator two. They declare it "unexpected token", so I guess it has to be wrapped to pass or something.

Vadim Berman
  • 1,932
  • 1
  • 20
  • 39