Can we write compose file in json file instead of yaml?

Yes. Yaml format is a superset JSON format. There fore any JSON file is also a valid Yamlfile.
If we use a JSON file then we have to specify in docker command that weare using a JSON file as follows:
%docker-compose-f docker-compose.json up