How to solve encoded HTML entities WordPress REST API?

WordPress REST API is quite standard and heavily used WordPress community. When we get the WordPress REST API response, the HTML entities of the response become encoded. Following is the encoded entity example:

title: {
    rendered: "The hyphen becomes this: #8211;"
}

We can very easily fix the above encode issue using rest_prepare_post hook for WordPress REST API. This hook will allow us to get the entire post and then use html_entity_decode method to fix the decoding issue. Finally, we should return the fixed JSON as the response.

Following is the example code that will fix the above encode issue:

Fixes for post type:

function fix_decode_rest_api($response, $post, $request) {

    if (isset($post)) {
        // $post->post_title might not have the HTML 
        $decodedTitle = html_entity_decode($post->post_title);
        $response->data['title']['rendered'] = $decodedTitle;
 
        // Another way, if $post is not available
        $decodedPostTitle = html_entity_decode($response->data['title']['rendered']);
        $response->data['title']['rendered'] = $decodedPostTitle;
    }
    return $response;
}
add_filter('rest_prepare_post', 'fix_decode_rest_api', 10, 3);

Fixes for media type:

function post_rest_attachment_api($response, $media, $request) {

    if (isset($media)) {
        $decodedPostTitle = html_entity_decode($response->data['title']['rendered']);
        $response->data['title']['rendered']= $decodedPostTitle;
    }
    return $response;
}
add_filter( 'rest_prepare_attachment', 'post_rest_attachment_api', 10, 3 );