简体   繁体   中英

How to produce JSON Lines files with known C# frameworks?

I usually use Newtonsoft's Json.NET for producing JSON files. Currently, I'm looking into importing a JSON-formatted data file into Google BigQuery. However, BigQuery requires a JSON file with newline as delimiter (probably known as JSON Lines too) which is a bit different than the usual tree style output from Json.NET. I've been searching the Internet on how to do this but have been unsuccesful in finding anything useful. Is this possible with Newtonsoft's or any other popular JSON serializers?

Have no idea how I wasn't able to find the links as provided by dbc in his comment:

Take a look at Serialize as NDJSON using Json.NET for serialization and Line delimited json serializing and de-serializing for deserialization. – dbc 3 hours ago

I did the naïve version where I built up a StringBuilder object but it's naturally not suitable for large amount of data.

However, I agree with Panagiotis Kanavos on the following comment:

That grab is polluting search results which makes finding answers harder than it was just 1 year ago

NDJSON, JSON Lines, newline delimited JSON... Not so sure which name to go by. :)

As seen in the JSON spec on www.json.org, newlines in a string are encoded as \\n when in JSON.

Railroad Diagram for JSON String

JSON.Net is not introducing newline characters into the output arbitrarily, but rather your original string already has newline characters, so they have to be encoded. Can also try

JsonConvert.SerializeObject(retPair.publicKey.Replace("\\n",""))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM