Use structured output / response format with JSON schema feature if your models provide it (openai for sure does, I think it's json_schema instead of json_object and provide a schema).
Otherwise there are libraries like instructor (python) which can help to generate json in markdown code blocks.
Otherwise there are libraries like instructor (python) which can help to generate json in markdown code blocks.