Read a file in a macOS Command Line Tool project

You can create a separate bundle, place the files there, and then load them without having to worry about their individual URLs.

Let's do it:

  1. In Xcode, click on File --> New --> Target...
  2. Click on the macOS tab at the top
  3. Scroll down to the Framework & Library, select Bundle, click Next. Give a name to the new bundle (let's say JSONMocks).
  4. Add files to this bundle. Add the file to Xcode, then make sure that the bundle is ticked in its target membership section:

    target membership for file in bundle

  5. Add the bundle to your target. In the target's Build Phases, open the Copy Files phase and click the + button. Select the bundle and click Add:

    enter image description here

  6. Programmatically access the contents of your bundle thus:

let currentDirectoryURL = URL(fileURLWithPath: FileManager.default.currentDirectoryPath)
let bundleURL = URL(fileURLWithPath: "JSONMocks.bundle", relativeTo: currentDirectoryURL)
let bundle = Bundle(url: bundleURL)
let jsonFileURL = bundle!.url(forResource: jsonFileName, withExtension: "json")!

There is no app bundle for a command-line tool. Just a binary.

You need to put the JSON file in a known location (current directory?) and construct the path yourself