Search code examples
c#jsonrestwebrequest

API returns json containing linebreaks etc. how can i clean my json?


I am trying to talk to an api (isbndb.com) and i can succesfully get a response. Using a streamreader i can even get a string containing json. However that string will also contain special characters for linebreaks etc. Therefore i cant deserialize the json. The string content looks like this :

"{\n    \"book\":\n                         \n                        {\n                                    \"publisher\":\"Pearson\",\n                                                                    \"language\":\"Eng\",\n                                                                                    \"image\":\"https:\\/\\/images.isbndb.com\\/covers\\/34\\/13\\/9780134093413.jpg\",\n                                                    \"title_long\":\"Campbell Biology (11th Edition)\",\n                                                    \"edition\":\"11\",\n                                                                    \"pages\":1488,\n                                                    \"date_published\":\"2017\",\n                                                                    \"subjects\":[\"Biology\"],\n                                                    \"authors\":[\"Lisa A. Urry\",\"Michael L. Cain\",\"Steven A. Wasserman\",\"Peter V. Minorsky\",\"Jane B. Reece\"],\n                                                    \"title\":\"Campbell Biology (11th Edition)\",\n                                                    \"isbn13\":\"9780134093413\",\n                                                    \"msrp\":\"259.99\",\n                                                    \"binding\":\"Hardcover\",\n                                                    \"publish_date\":\"2017\",\n                            \n                            \n                                    \"isbn\":\"0134093410\"\n                            }\n                    }"

The method that gets me this result looks like this :

public bool TryGetBook(string isbn)
        {
            bool rtnValue = false;

            string WEBSERVICE_URL = Globals.WEBSERVICE_URL + isbn;

            try
            {
                var webRequest = WebRequest.Create(WEBSERVICE_URL);

                if (webRequest != null)
                {
                    webRequest.Method = "GET";
                    webRequest.ContentType = "application/json";
                    webRequest.Headers["Authorization"] = Globals.REST_KEY;

                    //Get the response 
                    WebResponse wr = webRequest.GetResponseAsync().Result;
                    Stream receiveStream = wr.GetResponseStream();
                    StreamReader reader = new StreamReader(receiveStream);

                    string content = reader.ReadToEnd();

                    if (content != null)
                    {
                        Book book = JsonConvert.DeserializeObject<Book>(content);

                        if (book != null)
                        {
                            Books.Add(book);
                            rtnValue = true;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return rtnValue;
        }

I am not really sure what to do here, i could write a regex expression to clean up my sample data but that just seems like the wrong way to go about it.

any help is much appreciated.


Solution

  • You can replace all line breaks by using the following snippet

    string content = json.Replace("\n", "");
    

    Environment.NewLine doesn't work here. There is a formatted content below

    {
        "book": 
        {
            "publisher": "Pearson",
            "language": "Eng",
            "image": "https:\/\/images.isbndb.com\/covers\/34\/13\/9780134093413.jpg",
            "title_long": "Campbell Biology (11th Edition)",
            "edition": "11",
            "pages": 1488,
            "date_published": "2017",
            "subjects": ["Biology"],
            "authors": ["Lisa A. Urry", "Michael L. Cain", "Steven A. Wasserman", "Peter V. Minorsky", "Jane B. Reece"],
            "title": "Campbell Biology (11th Edition)",
            "isbn13": "9780134093413",
            "msrp": "259.99",
            "binding": "Hardcover",
            "publish_date": "2017",
            "isbn": "0134093410"
        }
    }
    

    Then copy the updated string and use Edit-Paste Special-Paste JSON as classes. There is generated classes

    public class BookResponse
    {
        public Book book { get; set; }
    }
    
    public class Book
    {
        public string publisher { get; set; }
        public string language { get; set; }
        public string image { get; set; }
        public string title_long { get; set; }
        public string edition { get; set; }
        public int pages { get; set; }
        public string date_published { get; set; }
        public string[] subjects { get; set; }
        public string[] authors { get; set; }
        public string title { get; set; }
        public string isbn13 { get; set; }
        public string msrp { get; set; }
        public string binding { get; set; }
        public string publish_date { get; set; }
        public string isbn { get; set; }
    }
    

    Actually, the Book instance is nested in a different class. So, for parsing you have to use something like that

    var response = JsonConvert.DeserializeObject<BookResponse>(content);
    
    if (response.book != null)
    {
    }