title: SwiftUi Load Local Json Data
body: <p>I actually learning SwiftUi, and at the moment i don't get into my problem.</p>
  <p>I try to load Json Data from my local Json File !</p>
  <p>I have searched many many many Post but nothing really helped me, that is why i have chosen to make this post.</p>
  <p>I show you how i possible tried it.</p>
  <p>Path: Data/test.json</p>
  <pre><code>[
      {
      &quot;id&quot;: 1,
      &quot;name&quot;: &quot;&quot;,
      &quot;trade_name&quot;: &quot;&quot;,
      &quot;short_name&quot;: &quot;&quot;,
      &quot;test&quot;:  &quot;&quot;,
      &quot;test1&quot;: &quot;&quot;,
      &quot;test2&quot;: &quot;&quot;,
      &quot;test3&quot;: &quot;&quot;,
      &quot;test4&quot;: &quot;&quot;,
      &quot;test5&quot;: &quot;&quot;,
      &quot;test6&quot;: &quot;&quot;,
      &quot;test7&quot;: &quot;&quot;,
      &quot;test8&quot;: [],
      &quot;test9&quot;: &quot;&quot;,
      &quot;test10&quot;: &quot;&quot;,
      &quot;test11&quot;: &quot;&quot;,
      &quot;test12&quot;: &quot;&quot;
      },
  ]
  </code></pre>
  <p>Path: App/DataLoader.swift</p>
  <pre><code>import Foundation
  
  public class DataLoader {
      
      @Published var contentData = [JSONData]()
      
      init(){
          load()
          sort()
      }
      
      func load(){
          if let fileLocation = Bundle.main.url(forResource: &quot;test&quot;, withExtension: &quot;json&quot;){
              do {
                  let data = try Data(contentsOf: fileLocation)
                  let jsonDecoder = JSONDecoder()
                  let dataFromJson = try jsonDecoder.decode([JSONData].self, from: data)
                  
                  self.contentData = dataFromJson
              } catch {
                  print(error)
              }
          }
      }
      
      func sort(){
          self.contentData = self.contentData.sorted(by: { $0.id &lt; $1.id})
      }
  }
  </code></pre>
  <p>Here in this File i add the variables.
  Path: App/JsonData.swift</p>
  <pre><code>import Foundation
  
  struct JSONData: Codable {
      
      var id: Int
      var name: String
      var trade_name: String
      var short_name: String
      var test: String
      var test1: String
      var test2: [String:String]
      var test3: [String:String]
      var test4: String
      var test5: String
      var test6: [String:String]
      var test7: String
      var test8: [String:String]
      var test9: String
      var test10: String
      var test11: String
      var test12: String
      
  }
  </code></pre>
  <p>I didn't set the Display yet but i want it in a List.
  In my ContentView.swift File i would load the Data from that Json File !</p>
  <p>Path: App/ContentView.swift</p>
  <pre><code>import SwiftUI
  
  struct ContentView: View {
      var body: some View {
          NavigationView {
              Home()
                  .navigationTitle(&quot;Test Interface&quot;)
                  .navigationBarTitleDisplayMode(.inline)
          }
      }
  }
  
  struct ContentView_Previews: PreviewProvider {
      static var previews: some View {
          ContentView()
      }
  }
  
  struct Home : View {
      let data = DataLoader().contentData
      
      var body: some View {
          data[IndexPath.row].id
      }
  }
  </code></pre>
  <p>When i did it like that, it shows some Errors.</p>
  <p>Return type of property 'body' requires that 'Int' conform to 'View'</p>
  <p>Instance member 'row' cannot be used on type 'IndexPath'; did you mean to use a value of this type instead?</p>
  <p>Regards</p>
  <p>CreatingBytes</p>