JamesNK / Newtonsoft.Json

Json.NET is a popular high-performance JSON framework for .NET
https://www.newtonsoft.com/json
MIT License
10.74k stars 3.25k forks source link

Removing comments from Json #2803

Closed Coder3333 closed 1 year ago

Coder3333 commented 1 year ago

This may be a feature request. I cannot find a way to easily remove comments from a json string. Is this a feature of Newtonsoft.Json or could it be added?

elgonzo commented 1 year ago

Just load/parse your json into a Newtonsoft.Json.Linq.JToken representation and then write the JToken back to a file/TextWriter or get the json string from it. JToken and its derived types JObject/JArray/etc... ignore json comments (unless explicitly told otherwise), so this seems to be the quickest way to achieve what you want with the least amount of effort.

(Please close this issue if this resolves your problem. If my suggestion does not resolve your issue, i would recommend to ask on stackoverflow.com instead.)

Coder3333 commented 1 year ago

@elgonzo , thank you for the tip. I don't know how this solution didn't show up in my online searches, which included stackoverflow.