What software is it? You can get the category in a few ways without changing URLs (in the HTML and JSON).
For JSON, instead of scraping this:
https://meta.discourse.org/t/topic-url-to-include-its-category/39996
scrape this:
https://meta.discourse.org/t/topic-url-to-include-its-category/39996.json
and look for the category_id field.
Another possibility is using Data Explorer to extract what you need.