How to convert a field type while preserving content?

You can do that via $page->update(), fetching the value from the text field and creating an array, that you store yml encoded in the structure field.

Check out the addToStructure() function here to get a head start: