JSON formatter in C

0 votes

I am looking for a function that will enable a string of Json as an input and format it with the line breaks and indentations. Also, the validation would be a bonus, but it however,  isn't necessary and there is no need for it to be  parsed into an object or so. Does anyone have any idea of such a library and how can one get access to it? 

Sample input:

​​{"status":"OK", "results":[ {"types":[ "locality", "political"], "formatted_address":"New York, NY, USA", "address_components":[ {"long_name":"New York", "short_name":"New York", "types":[ "locality", "political"]}, {"long_name":"New York", "short_name":"New York", "types":[ "administrative_area_level_2", "political"]}, {"long_name":"New York", "short_name":"NY", "types":[ "administrative_area_level_1", "political"]}, {"long_name":"United States", "short_name":"US", "types":[ "country", "political"]}], "geometry":{"location":{"lat":40.7143528, "lng":-74.0059731}, "location_type":"APPROXIMATE", "viewport":{"southwest":{"lat":40.5788964, "lng":-74.2620919}, "northeast":{"lat":40.8495342, "lng":-73.7498543}}, "bounds":{"southwest":{"lat":40.4773990, "lng":-74.2590900}, "northeast":{"lat":40.9175770, "lng":-73.7002720}}}}]}
Feb 8, 2022 in Java by Soham
• 9,710 points
2,403 views

1 answer to this question.

0 votes

 In order to do the needful, one could use the Newtonsoft.Json library and call SerializeObject with the Formatting.Indented enum by using the given line -
 

var x = JsonConvert.SerializeObject(jsonString, Formatting.Indented);


Update -

I just tried to use this again and even though it used to work, the updated versions must have made a subsequent change to this and it is not working quite as expected. After testing this in Linqpad and SO, these do work and have shared the code below:-
 

void Main() 
{ 
      //Example 1 
    var t = "{\"x\":57,\"y\":57.0,\"z\":\"Yes\"}"; 
    var obj = Newtonsoft.Json.JsonConvert.DeserializeObject(t); 
    var f = Newtonsoft.Json.JsonConvert.SerializeObject(obj, Newtonsoft.Json.Formatting.Indented); Console.WriteLine(f);
//Example 2 JToken jt = 
JToken.Parse(t); string formatted = jt.To
String(Newtonsoft.Json.Formatting.Indented); 
Console.WriteLine(formatted); 

//Example 2 in one line - Console.WriteLine(JToken.Parse(t).ToString(Newtonsoft.Json.Formatting.Indented)); 
}
answered Feb 8, 2022 by Rahul
• 9,680 points

Related Questions In Java

0 votes
3 answers

How to parse JSON in Java

import org.json.*; JSONObject obj = new JSONObject(" .... ...READ MORE

answered Aug 20, 2018 in Java by Daisy
• 8,140 points
4,049 views
+17 votes
25 answers

How can I convert String to JSON object in Java?

Hi @Daisy You can use Google gson  for more ...READ MORE

answered Feb 7, 2019 in Java by Suresh
• 720 points
253,811 views
0 votes
2 answers

How to convert a JSON String into Object in Java?

You could probably check out Google's Gson: ...READ MORE

answered Aug 21, 2019 in Java by Sirajul
• 59,230 points
3,286 views
0 votes
1 answer

What is the simplest way to read JSON from a URL in java

Read json from url use url.openStream() and read contents ...READ MORE

answered Jun 13, 2018 in Java by samarth295
• 2,220 points
5,202 views
0 votes
0 answers

How to prettyprint a JSON file?

May 27, 2022 in Python by Kichu
• 19,040 points
524 views
0 votes
1 answer

How do I turn a C# object into a JSON string in .NET

Newtonsoft.json can be used to accomplish this. ...READ MORE

answered Jun 13, 2022 in C# by krishna
• 2,820 points
740 views
0 votes
1 answer

How to convert JSON text into objects using C#

To make a class out of a ...READ MORE

answered Jun 13, 2022 in C# by krishna
• 2,820 points
574 views
0 votes
0 answers

How do I turn a C# object into a JSON string in .NET?

I have classes like these: class MyDate { ...READ MORE

Sep 20, 2022 in Android by Edureka
• 13,620 points
473 views
0 votes
1 answer
0 votes
1 answer

How to split a string in Java

By using the appropriate method, this can ...READ MORE

answered Feb 9, 2022 in Java by Rahul
• 9,680 points

edited Jul 6, 2023 by Khan Sarfaraz 758 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP