Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert dictionary to json string without space and new line

I am trying to convert a dictionary to json string without space and new line. I tried to use JSONSerialization.jsonObject but I still can see spaces and new lines. Is there any way to have a string result looks something like this

"data": "{\"requests\":[{\"image\":{\"source\":{\"imageUri\":\"https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png\"}},\"features\":[{\"type\":\"LOGO_DETECTION\",\"maxResults\":1}]}]}"

My conversion

var features = [[String: String]]()
for detection in detections {
    features.append(["type": imageDetection[detection]!])
}
let content = ["content": base64Image]
let request = ["image": content, "features": features] as [String : Any]
let requests = ["requests": [request]]

let jsonData = try! JSONSerialization.data(withJSONObject: requests, options: .prettyPrinted)
let decoded = try! JSONSerialization.jsonObject(with: jsonData, options: [])
print(decoded)

Result

{
    requests =     (
                {
            features =             (
                                {
                    type = "LABEL_DETECTION";
                },
                                {
                    type = "WEB_DETECTION";
                },
                                {
                    type = "TEXT_DETECTION";
                }
            );
            image =             {
                content = "iVBO
      ...........
like image 704
Pak Ho Cheung Avatar asked Nov 08 '17 02:11

Pak Ho Cheung


People also ask

Can dictionary be converted to JSON?

You can declare an array to check the keys and values of the dictionary and convert it into json object. The for loop stores the value, and the dumps() method stores the dictionary. Check out the below example for a better understanding of the approach.

How do you represent a dictionary in JSON?

JSON is a way of representing Arrays and Dictionaries of values ( String , Int , Float , Double ) as a text file. In a JSON file, Arrays are denoted by [ ] and dictionaries are denoted by { } .

Is JSON format a dictionary?

JSON FormatJSON at its top-level is a dictionary of attribute/value pairs, or key/value pairs as we've talked about dictionaries in this class. The values are numbers, strings, other dictionaries, and lists.

What is difference between JSON and dictionary?

JSON is a universal (language independent) string-based storage and delivery format that uses a system of nested key/value pairs to encode data. A dictionary (in some languages, including Python) is a data structure that also uses nested key/value pairs.


1 Answers

You are decoding the serialized JSON into an object. When an object is printed into the console, you will see the indentation, and the use of equals symbols and parentheses.

Remove the .prettyPrinted option and use the data to initialize a string with .utf8 encoding.

let jsonData = try! JSONSerialization.data(withJSONObject: requests, options: [])
let decoded = String(data: jsonData!, encoding: .utf8)!
like image 53
Callam Avatar answered Oct 23 '22 20:10

Callam