Build error: 'control characters are not allowed' (JSON data file)

I’ve written a plugin which writes JSON output to a file in the _data directory:

while current_page <= total_pages do

  url = 'https://web.consonance.app/api/v2/products.json'
  query = {
    'page' => "#{current_page}",
    'q[publishing_status_eq]' => '04' 
  }
  headers = {
    Authorization: "Token token=*************************"
  }
  request = HTTParty.get(url, query: query, headers: headers)
    
  hash = JSON.parse(request.body)
    
  hash['products'].each do |item|
    product_array.push(item)
  end

  current_page += 1

end

# open products.json in data dir and write array output converted from hash back to JSON

File.open("./_data/products.json", "w") { |file| 
  file.puts JSON.pretty_generate(product_array)
}

which puts the desired output as a JSON array in the _data directory with the following format:

[
  {
    "id": 1,
    ...
  },
  {
    "id": 2,
    ...
  },
  ...
]

When I try to build my site, I get the error:

jekyll 3.8.5 | Error:  (/Users/jamiebowman/Documents/web dev/jekyll/press/_data/products.json): control characters are not allowed at line 1 column 1

When I remove the square brackets are the beginning and the end of the JSON file, then the site builds, but I cannot properly access the data without it being an array.

Has anyone come across this before?

Thanks in advance.