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 in Java by Soham
• 8,730 points
236 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 by Rahul
• 8,980 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,120 points
3,003 views
+15 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
234,581 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,170 points
2,365 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
4,140 views
0 votes
0 answers

How to prettyprint a JSON file?

May 27 in Python by Kichu
• 19,040 points
43 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 in C# by krishna
• 1,540 points
10 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 in C# by krishna
• 1,540 points
11 views
0 votes
3 answers

Trying to upload files using Selenium(C#)

You can try using Javascript Executor to ...READ MORE

answered Aug 23, 2019 in Selenium by Abha
• 28,100 points
4,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 in Java by Rahul
• 8,980 points
37 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP