Unable to parse nested JSON data from API request.

I'm trying to retrieve the data from an API request, however, I'm having trouble parsing some of the nested parts of the JSON data.

The JSON structure looks like this:

Code Block
["quoteResponse": {
  error = "<null>";
  result =   (
        {
      ask = "607.65";
      gmtOffSetMilliseconds = "-18000000";
      language = "en-US";
      longName = "Tesla, Inc.";
      market = "us_market";
    }
  );
}]


And I am trying to retrieve language, market, ask, etc. from this API response. The problem is, I am having trouble casting result as a dictionary.

Code Block Swift
if let dictionary = jsonData as? [String: Any] {
   
  print(dictionary)
  if let nestedDictionary = dictionary["quoteResponse"] as? [String: Any] {
    if let results = nestedDictionary["result"] as? Any{
      print(results)
    }
  }
}

I cannot convert it to a string nor can I turn it into a usable array. How do I access these values?

The snippet you posted is not valid JSON; specifically, the outer element in an array (denoted by square brackets) but it contains a key-value pair like a dictionary. If you can post a valid snippet, we should be able to advise you further.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"
Unfortunately, serializing the data as JSON yields this incorrect JSON structure.

Doing:
Code Block swift
jsonData = try? JSONSerialization.jsonObject(with: data!, options: [])

with the data retrieved from the API request yields this incorrect structure. However, encoding the data as a string yields the correct JSON structure. However, converting that string back to JSON results in the same problem.

How do I fix this?

Unfortunately, serializing the data as JSON yields this incorrect JSON
structure.

So where does this data originate from?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"
Unable to parse nested JSON data from API request.
 
 
Q