Pretty printing JSON response in Debug View (Xcode)

Nabin Shrestha
1 min readMay 5, 2020

Don’t know about you devs but I like my network responses pretty printed. This is because I’m picky about these things and also it helps me debug if my application behaves not the way i intended to.

What we’re trying to achieve:

Printing JSON response in Debug View

Let’s dive into it.

When we make a network request using URLSession, we get Data from server response. So let’s create extensions of Data and Dictionary.

Data + Extensions.swift
Dictionary + Extensions.swift

Now in your APIService class (from where you perform network requests), print the obtained response as: (line no. 18)

What we’ve done is:

  1. First we converted the Data obtained from server to NSDictionary.
  2. Then, we converted the NSDictionary to [String: Any]. (Using Data + Extensions.swift)
  3. Finally, we pretty printed the above value using Dictionary + Extensions.swift.

This is all. Now you’ve got a logger that prints all your GET and POST requests. Happy Coding!

--

--