Save JSON object attributes to custom metabox

I’ve found a possible solution. Not sure if it is safe though

jQuery(function(jQuery) {

    // Uploading files
    var file_frame;
    var wp_media_post_id = wp.media.model.settings.post.id; // Store the old id
    var set_to_post_id = 5; // Set this

    formfield = jQuery(this).siblings('.custom_upload_image');
    preview = jQuery(this).siblings('.custom_preview_image');

    // Create the media frame.
    file_frame = wp.media.frames.file_frame = wp.media({
        multiple: false  // Set to true to allow multiple files to be selected
    });

    // When an image is selected, run a callback.
    file_frame.on( 'select', function() {
        // We set multiple to false so only get one image from the uploader
        attachment = file_frame.state().get('selection').first().toJSON();

        imgurl = attachment.url;
        id = attachment.id;

        console.log(attachment);

        var inputfield =  wp.media.model.settings.post.id;

        jQuery('input.custom_upload_image[name=" + inputfield + "]').val(id);
        jQuery('input.custom_upload_image[name=" + inputfield + "]').parents('td').find('.custom_preview_image').attr('src', imgurl);


        // Restore the main post ID
        wp.media.model.settings.post.id = wp_media_post_id;
    });

    jQuery('.custom_upload_image_button').click(function( e ){

        e.preventDefault();

        var set_to_post_id = jQuery(this).parents('td').find('.custom_upload_image').attr('name');

        wp.media.model.settings.post.id = set_to_post_id;

        // Open frame
        file_frame.open();

        // Finally, open the modal
        file_frame.open();
    });

    // Restore the main ID when the add media button is pressed
    jQuery('a.add_media').on('click', function() {
        wp.media.model.settings.post.id = wp_media_post_id;
    });

    jQuery('.custom_clear_image_button').click(function(e){
        e.preventDefault();
        jQuery(this).parents('td').find('.custom_upload_image').val('');
        jQuery(this).parents('td').find('.custom_preview_image').attr('src', '/wp-content/themes/YESpeopleandprojects/images/image.png');
    });

});