How do I assign a variable to the definitions in JSON?
Categories
- 362 All Categories
- 2 Member guidelines
- 63 Ask the Community: Technical and operational questions
- 68 General
- 10 Ask the Community: Other
- 60 Report a bug
- 12 Suggest an improvement
- 7 How to get useful technical help
- 92 Frequently asked questions
- 6 Tutorials and presentations
- 26 API endpoints
- 9 News and updates
- 3 Language datasets
- 18 Review my code
[Action required] API changes announcement for existing customers
Note: This announcement applies to API customers that are registered to a Prototype and Developer plans.
Hello all!
We are writing to inform you that a new set of plans is replacing our current application plans (Prototype and Developer plans) and prices.
As of 10th May 2022, the Prototype and Developer plans will no longer be operational. Oxford Dictionaries API will offer two new plans:
- Introductory plan
- Unlimited plan
For Prototype and Developer plan customers, changes are happening between 12th April 2022 and 10th May 2022. So do not worry! You will have time to check the options and choose how you wish to proceed using our data by selecting the Introductory or Unlimited plan.
For further instructions on how to change plans, and for more information, please visit our new plans announcement page.
Link: https://developer.oxforddictionaries.com/new-plans-announcement
If you have any questions, please feel free to leave us a comment or reach us at our Contact Us page. (https://developer.oxforddictionaries.com/contact-us)
The Oxford dictionaries API team
Note: This announcement applies to API customers that are registered to a Prototype and Developer plans.
Hello all!
We are writing to inform you that a new set of plans is replacing our current application plans (Prototype and Developer plans) and prices.
As of 10th May 2022, the Prototype and Developer plans will no longer be operational. Oxford Dictionaries API will offer two new plans:
- Introductory plan
- Unlimited plan
For Prototype and Developer plan customers, changes are happening between 12th April 2022 and 10th May 2022. So do not worry! You will have time to check the options and choose how you wish to proceed using our data by selecting the Introductory or Unlimited plan.
For further instructions on how to change plans, and for more information, please visit our new plans announcement page.
Link: https://developer.oxforddictionaries.com/new-plans-announcement
If you have any questions, please feel free to leave us a comment or reach us at our Contact Us page. (https://developer.oxforddictionaries.com/contact-us)
The Oxford dictionaries API team
How do I assign a variable to the definitions in JSON?

Hello,
I am successfully using the following code to play the audio from the JSON. Now I want to assign a definition (any one of them) to a string. How do I pull the definition from the JSON?
func speakRandomWord() { selectRandomWord() appId = "secret" appKey = "secret" language = "en" word = randomWord //getting random word from another service regions = "us" word_id = word.lowercased() //word id is case sensitive and lowercase is required url = URL(string: "https://od-api.oxforddictionaries.com:443/api/v1/entries/\(language)/\(word_id)/regions=\(regions)")! var request = URLRequest(url: url) request.addValue("application/json", forHTTPHeaderField: "Accept") request.addValue(appId, forHTTPHeaderField: "app_id") request.addValue(appKey, forHTTPHeaderField: "app_key") let session = URLSession.shared _ = session.dataTask(with: request, completionHandler: { data, response, error in if let response = response, let data = data, let jsonData = try? JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String:AnyObject] { if let dictionary = jsonData as? [String: Any], let results = dictionary["results"] as? [[String: Any]], //You need to choose one from "results" !results.isEmpty, case let result = results[0], let lexicalEntries = result["lexicalEntries"] as? [[String: Any]], //You need to choose one from "lexicalEntries" !lexicalEntries.isEmpty, case let lexicalEntry = lexicalEntries[0], let pronunciations = lexicalEntry["pronunciations"] as? [[String: Any]], //You need to choose one from "lexicalEntries" !pronunciations.isEmpty, case let pronunciation = pronunciations[0] //This is where I am stuck. How do I proceed? { //Here you can use `pronunciation` as a Dictionary containing "audioFile" if let audioPath = pronunciation["audioFile"] as? String { print("This is \(audioPath)") let path = String(describing:audioPath) let url = URL.init(string: path) self.player = AVPlayer.init(url: url!) self.player.play() } else { print ("No data for this word") self.speakRandomWord() } } } else { print(error) print(NSString.init(data: data!, encoding: String.Encoding.utf8.rawValue)) print("Failed to load: \(error?.localizedDescription)") self.speakRandomWord() } }).resume() }
Tagged:
Best Answer
-
AmosDuveen Member, Administrator, Moderator admin
I'm not an expert, but you seem to have pulled one element successfully (audioFile); what is stopping you using the same method for definitions?
Answers
Yes, you're right...still getting the hang of JSON. Thank you.