title: Trying to return specific value from API call based on user input
body: <p>I'm trying to pull data from an API based on the word a user inputs. Right now, I can run this</p>
  <pre><code>        let postData = parameters.data(using: .utf8)
          
          var request = URLRequest(url: URL(string: &quot;https://trackapi.nutritionix.com/v2/natural/nutrients&quot;)!,timeoutInterval: Double.infinity)
          request.addValue(&quot;application/json&quot;, forHTTPHeaderField: &quot;Content-Type&quot;)
          request.addValue(&quot;APPID&quot;, forHTTPHeaderField: &quot;x-app-id&quot;)
          request.addValue(&quot;APPKEY&quot;, forHTTPHeaderField: &quot;x-app-key&quot;)
          
          request.httpMethod = &quot;POST&quot;
          request.httpBody = postData
          
          let task = URLSession.shared.dataTask(with: request) { data, response, error in
              guard let data = data, error == nil else {
                  return
              }
                  do {
                      let macros = try JSONDecoder().decode(Instruction.self, from: data)
                      
                      print(macros)
                      
                  }
                  catch {
                      print(error)
                  }
  </code></pre>
  <p>And if the user inputs &quot;banana&quot;, it prints the below result. I only want the macro numbers (protein, carbs, fats, calories). Is there a way to pull only those from the API call? Below the print are my Codable structs as well if that helps.</p>
  <p><code>Instruction(foods: [Multiple_Views.Food(foodName: &quot;banana&quot;, brandName: nil, servingQty: 1, servingUnit: &quot;medium (7\&quot; to 7-7/8\&quot; long)&quot;, servingWeightGrams: 118.0, nfCalories: 105.02, nfTotalFat: 0.39, nfSaturatedFat: 0.13, nfCholesterol: 0.0, nfSodium: 1.18, nfTotalCarbohydrate: 26.95, nfDietaryFiber: 3.07, nfSugars: 14.43, nfProtein: 1.29, nfPotassium: 422.44, nfP: 25.96, fullNutrients: [Multiple_Views.FullNutrient(attrID: 203, value: 1.2862), Multiple_Views.FullNutrient(attrID: 204, value: 0.3894), Multiple_Views.FullNutrient(attrID: 205, value: 26.9512), Multiple_Views.FullNutrient(attrID: 207, value: 0.9676), Multiple_Views.FullNutrient(attrID: 208, value: 105.02), Multiple_Views.FullNutrient(attrID: 209, value: 6.3484), Multiple_Views.FullNutrient(attrID: 210, value: 2.8202), Multiple_Views.FullNutrient(attrID: 211, value: 5.8764), Multiple_Views.FullNutrient(attrID: 212, value: 5.723), Multiple_Views.FullNutrient(attrID: 213, value: 0.0), Multiple_Views.FullNutrient(attrID: 214, value: 0.0118), Multiple_Views.FullNutrient(attrID: 221, value: 0.0), Multiple_Views.FullNutrient(attrID: 255, value: 88.3938), Multiple_Views.FullNutrient(attrID: 262, value: 0.0), Multiple_Views.FullNutrient(attrID: 263, value: 0.0), Multiple_Views.FullNutrient(attrID: 268, value: 437.78), Multiple_Views.FullNutrient(attrID: 269, value: 14.4314), Multiple_Views.FullNutrient(attrID: 287, value: 0.0), Multiple_Views.FullNutrient(attrID: 291, value: 3.068), Multiple_Views.FullNutrient(attrID: 301, value: 5.9), Multiple_Views.FullNutrient(attrID: 303, value: 0.3068), Multiple_Views.FullNutrient(attrID: 304, value: 31.86), Multiple_Views.FullNutrient(attrID: 305, value: 25.96), Multiple_Views.FullNutrient(attrID: 306, value: 422.44), Multiple_Views.FullNutrient(attrID: 307, value: 1.18), Multiple_Views.FullNutrient(attrID: 309, value: 0.177), Multiple_Views.FullNutrient(attrID: 312, value: 0.092), Multiple_Views.FullNutrient(attrID: 313, value: 2.596), Multiple_Views.FullNutrient(attrID: 315, value: 0.3186), Multiple_Views.FullNutrient(attrID: 317, value: 1.18), Multiple_Views.FullNutrient(attrID: 318, value: 75.52), Multiple_Views.FullNutrient(attrID: 319, value: 0.0), Multiple_Views.FullNutrient(attrID: 320, value: 3.54), Multiple_Views.FullNutrient(attrID: 321, value: 30.68), Multiple_Views.FullNutrient(attrID: 322, value: 29.5), Multiple_Views.FullNutrient(attrID: 323, value: 0.118), Multiple_Views.FullNutrient(attrID: 324, value: 0.0), Multiple_Views.FullNutrient(attrID: 328, value: 0.0), Multiple_Views.FullNutrient(attrID: 334, value: 0.0), Multiple_Views.FullNutrient(attrID: 337, value: 0.0), Multiple_Views.FullNutrient(attrID: 338, value: 25.96), Multiple_Views.FullNutrient(attrID: 341, value: 0.0), Multiple_Views.FullNutrient(attrID: 342, value: 0.0236), Multiple_Views.FullNutrient(attrID: 343, value: 0.0118), Multiple_Views.FullNutrient(attrID: 344, value: 0.0708), Multiple_Views.FullNutrient(attrID: 345, value: 0.0), Multiple_Views.FullNutrient(attrID: 346, value: 0.0), Multiple_Views.FullNutrient(attrID: 347, value: 0.0), Multiple_Views.FullNutrient(attrID: 401, value: 10.266), Multiple_Views.FullNutrient(attrID: 404, value: 0.0366), Multiple_Views.FullNutrient(attrID: 405, value: 0.0861), Multiple_Views.FullNutrient(attrID: 406, value: 0.7847), Multiple_Views.FullNutrient(attrID: 410, value: 0.3941), Multiple_Views.FullNutrient(attrID: 415, value: 0.4331), Multiple_Views.FullNutrient(attrID: 417, value: 23.6), Multiple_Views.FullNutrient(attrID: 418, value: 0.0), Multiple_Views.FullNutrient(attrID: 421, value: 11.564), Multiple_Views.FullNutrient(attrID: 429, value: 0.0), Multiple_Views.FullNutrient(attrID: 430, value: 0.59), Multiple_Views.FullNutrient(attrID: 431, value: 0.0), Multiple_Views.FullNutrient(attrID: 432, value: 23.6), Multiple_Views.FullNutrient(attrID: 435, value: 23.6), Multiple_Views.FullNutrient(attrID: 454, value: 0.118), Multiple_Views.FullNutrient(attrID: 501, value: 0.0106), Multiple_Views.FullNutrient(attrID: 502, value: 0.033), Multiple_Views.FullNutrient(attrID: 503, value: 0.033), Multiple_Views.FullNutrient(attrID: 504, value: 0.0802), Multiple_Views.FullNutrient(attrID: 505, value: 0.059), Multiple_Views.FullNutrient(attrID: 506, value: 0.0094), Multiple_Views.FullNutrient(attrID: 507, value: 0.0106), Multiple_Views.FullNutrient(attrID: 508, value: 0.0578), Multiple_Views.FullNutrient(attrID: 509, value: 0.0106), Multiple_Views.FullNutrient(attrID: 510, value: 0.0555), Multiple_Views.FullNutrient(attrID: 511, value: 0.0578), Multiple_Views.FullNutrient(attrID: 512, value: 0.0909), Multiple_Views.FullNutrient(attrID: 513, value: 0.0472), Multiple_Views.FullNutrient(attrID: 514, value: 0.1463), Multiple_Views.FullNutrient(attrID: 515, value: 0.1794), Multiple_Views.FullNutrient(attrID: 516, value: 0.0448), Multiple_Views.FullNutrient(attrID: 517, value: 0.033), Multiple_Views.FullNutrient(attrID: 518, value: 0.0472), Multiple_Views.FullNutrient(attrID: 601, value: 0.0), Multiple_Views.FullNutrient(attrID: 605, value: 0.0), Multiple_Views.FullNutrient(attrID: 606, value: 0.1322), Multiple_Views.FullNutrient(attrID: 607, value: 0.0), Multiple_Views.FullNutrient(attrID: 608, value: 0.0), Multiple_Views.FullNutrient(attrID: 609, value: 0.0), Multiple_Views.FullNutrient(attrID: 610, value: 0.0012), Multiple_Views.FullNutrient(attrID: 611, value: 0.0024), Multiple_Views.FullNutrient(attrID: 612, value: 0.0024), Multiple_Views.FullNutrient(attrID: 613, value: 0.1204), Multiple_Views.FullNutrient(attrID: 614, value: 0.0059), Multiple_Views.FullNutrient(attrID: 617, value: 0.026), Multiple_Views.FullNutrient(attrID: 618, value: 0.0543), Multiple_Views.FullNutrient(attrID: 619, value: 0.0319), Multiple_Views.FullNutrient(attrID: 620, value: 0.0), Multiple_Views.FullNutrient(attrID: 621, value: 0.0), Multiple_Views.FullNutrient(attrID: 626, value: 0.0118), Multiple_Views.FullNutrient(attrID: 627, value: 0.0), Multiple_Views.FullNutrient(attrID: 628, value: 0.0), Multiple_Views.FullNutrient(attrID: 629, value: 0.0), Multiple_Views.FullNutrient(attrID: 630, value: 0.0), Multiple_Views.FullNutrient(attrID: 631, value: 0.0), Multiple_Views.FullNutrient(attrID: 636, value: 18.88), Multiple_Views.FullNutrient(attrID: 645, value: 0.0378), Multiple_Views.FullNutrient(attrID: 646, value: 0.0861)], nixBrandName: nil, nixBrandID: nil, nixItemName: nil, nixItemID: nil, upc: nil, consumedAt: &quot;2022-03-31T17:14:17+00:00&quot;, metadata: Multiple_Views.Metadata(isRawFood: false), source: 1, ndbNo: 9040, tags: Multiple_Views.Tags(item: &quot;banana&quot;, measure: nil, quantity: &quot;1.0&quot;, foodGroup: 3, tagID: 399), altMeasures: [Multiple_Views.AltMeasure(servingWeight: 225.0, measure: &quot;cup, mashed&quot;, seq: Optional(1), qty: 1), Multiple_Views.AltMeasure(servingWeight: 150.0, measure: &quot;cup, sliced&quot;, seq: Optional(2), qty: 1), Multiple_Views.AltMeasure(servingWeight: 81.0, measure: &quot;extra small (less than 6\&quot; long)&quot;, seq: Optional(3), qty: 1), Multiple_Views.AltMeasure(servingWeight: 101.0, measure: &quot;small (6\&quot; to 6-7/8\&quot; long)&quot;, seq: Optional(4), qty: 1), Multiple_Views.AltMeasure(servingWeight: 118.0, measure: &quot;medium (7\&quot; to 7-7/8\&quot; long)&quot;, seq: Optional(5), qty: 1), Multiple_Views.AltMeasure(servingWeight: 136.0, measure: &quot;large (8\&quot; to 8-7/8\&quot; long)&quot;, seq: Optional(6), qty: 1), Multiple_Views.AltMeasure(servingWeight: 152.0, measure: &quot;extra large (9\&quot; or longer)&quot;, seq: Optional(7), qty: 1), Multiple_Views.AltMeasure(servingWeight: 126.0, measure: &quot;NLEA serving&quot;, seq: Optional(8), qty: 1), Multiple_Views.AltMeasure(servingWeight: 28.35, measure: &quot;oz&quot;, seq: Optional(80), qty: 1), Multiple_Views.AltMeasure(servingWeight: 100.0, measure: &quot;g&quot;, seq: nil, qty: 100)], lat: nil, lng: nil, mealType: 3, photo: Multiple_Views.Photo(thumb: &quot;https://nix-tag-images.s3.amazonaws.com/399_thumb.jpg&quot;, highres: &quot;https://nix-tag-images.s3.amazonaws.com/399_highres.jpg&quot;, isUserUploaded: false), subRecipe: nil, classCode: nil, brickCode: nil, tagID: nil)])</code></p>
  <p>Codable Structs here:</p>
  <pre><code>struct Welcome: Codable {
      let greeting: String
      let instructions: [Instruction]
  }
  
  // MARK: - Instruction
  struct Instruction: Codable {
      let foods: [Food]
  }
  
  // MARK: - Food
  struct Food: Codable {
      let foodName: String
      let brandName: JSONNull?
      let servingQty: Int
      let servingUnit: String
      let servingWeightGrams: Double
      let nfCalories: Double
      let nfTotalFat: Double
      let nfSaturatedFat: Double
      let nfCholesterol: Double
      let nfSodium: Double
      let nfTotalCarbohydrate: Double
      let nfDietaryFiber: Double
      let nfSugars: Double
      let nfProtein: Double
      let nfPotassium: Double
      let nfP: Double
      let fullNutrients: [FullNutrient]
      let nixBrandName, nixBrandID, nixItemName, nixItemID: JSONNull?
      let upc: JSONNull?
      let consumedAt: String
      let metadata: Metadata
      let source, ndbNo: Int
      let tags: Tags
      let altMeasures: [AltMeasure]
      let lat, lng: JSONNull?
      let mealType: Int
      let photo: Photo
      let subRecipe, classCode, brickCode, tagID: JSONNull?
  
      enum CodingKeys: String, CodingKey {
          case foodName = &quot;food_name&quot;
          case brandName = &quot;brand_name&quot;
          case servingQty = &quot;serving_qty&quot;
          case servingUnit = &quot;serving_unit&quot;
          case servingWeightGrams = &quot;serving_weight_grams&quot;
          case nfCalories = &quot;nf_calories&quot;
          case nfTotalFat = &quot;nf_total_fat&quot;
          case nfSaturatedFat = &quot;nf_saturated_fat&quot;
          case nfCholesterol = &quot;nf_cholesterol&quot;
          case nfSodium = &quot;nf_sodium&quot;
          case nfTotalCarbohydrate = &quot;nf_total_carbohydrate&quot;
          case nfDietaryFiber = &quot;nf_dietary_fiber&quot;
          case nfSugars = &quot;nf_sugars&quot;
          case nfProtein = &quot;nf_protein&quot;
          case nfPotassium = &quot;nf_potassium&quot;
          case nfP = &quot;nf_p&quot;
          case fullNutrients = &quot;full_nutrients&quot;
          case nixBrandName = &quot;nix_brand_name&quot;
          case nixBrandID = &quot;nix_brand_id&quot;
          case nixItemName = &quot;nix_item_name&quot;
          case nixItemID = &quot;nix_item_id&quot;
          case upc
          case consumedAt = &quot;consumed_at&quot;
          case metadata, source
          case ndbNo = &quot;ndb_no&quot;
          case tags
          case altMeasures = &quot;alt_measures&quot;
          case lat, lng
          case mealType = &quot;meal_type&quot;
          case photo
          case subRecipe = &quot;sub_recipe&quot;
          case classCode = &quot;class_code&quot;
          case brickCode = &quot;brick_code&quot;
          case tagID = &quot;tag_id&quot;
      }
  }
  
  // MARK: - AltMeasure
  struct AltMeasure: Codable {
      let servingWeight: Double
      let measure: String
      let seq: Int?
      let qty: Int
  
      enum CodingKeys: String, CodingKey {
          case servingWeight = &quot;serving_weight&quot;
          case measure, seq, qty
      }
  }
  
  // MARK: - FullNutrient
  struct FullNutrient: Codable {
      let attrID: Int
      let value: Double
  
      enum CodingKeys: String, CodingKey {
          case attrID = &quot;attr_id&quot;
          case value
      }
  }
  
  // MARK: - Metadata
  struct Metadata: Codable {
      let isRawFood: Bool
  
      enum CodingKeys: String, CodingKey {
          case isRawFood = &quot;is_raw_food&quot;
      }
  }
  
  // MARK: - Photo
  struct Photo: Codable {
      let thumb, highres: String
      let isUserUploaded: Bool
  
      enum CodingKeys: String, CodingKey {
          case thumb, highres
          case isUserUploaded = &quot;is_user_uploaded&quot;
      }
  }
  
  // MARK: - Tags
  struct Tags: Codable {
      let item: String
      let measure: JSONNull?
      let quantity: String
      let foodGroup, tagID: Int
  
      enum CodingKeys: String, CodingKey {
          case item, measure, quantity
          case foodGroup = &quot;food_group&quot;
          case tagID = &quot;tag_id&quot;
      }
  }
  
  // MARK: - Encode/decode helpers
  
  class JSONNull: Codable, Hashable {
      
  
      static func == (lhs: JSONNull, rhs: JSONNull) -&gt; Bool {
          return true
      }
  
      var hashValue: Int {
          return 0
      }
  
      func hash(into hasher: inout Hasher) {
          // No-op
      }
  
      init() {}
  
      required init(from decoder: Decoder) throws {
          let container = try decoder.singleValueContainer()
          if !container.decodeNil() {
              throw DecodingError.typeMismatch(JSONNull.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: &quot;Wrong type for JSONNull&quot;))
          }
      }
  
      func encode(to encoder: Encoder) throws {
          var container = encoder.singleValueContainer()
          try container.encodeNil()
      }
  }
  
  </code></pre>