Migrate Content from HTML RTE to JSON RTE Using the CLI

The JSON Rich Text Editor (RTE) field stores and returns the data in the JSON format. You can add the JSON RTE field to an existing content type by editing it or by creating a new content type.

If you already have data in the HTML RTE and want to migrate to JSON RTE, you can use the Contentstack CLI. By using a few commands in our CLI, you can easily perform the required migration. 

In this guide, we will discuss the steps required to perform this migration by using the following two methods:

  1. Using a config file to pass the commands
  2. Using flags to migrate the content

Note: The JSON RTE can be accessed and used only within the new Contentstack interface. To perform this migration, you should switch to the new Contentstack interface from the Classic one.

Prerequisites

Note: For European region, you first need to set the region accordingly in the CLI session. Refer to the Set Region command for more details.

Firstly, open your terminal and add a management token to the CLI session. To do this, use the following single-line command:

csdx auth:tokens:add -m -a <<alias>> -k <<api_key>> -t <<mgmt_token>>
Options:
  • -a, --alias=alias: Alias (name) you want to assign to the token.
  • -k, --api-key=api-key: API key of the stack where the token exists.
  • -t, --stack-management-token=token: Value of your stack management token.

Make note of the alias (token-name) you assign for the management token, we will require it in the next step.
After successfully adding the management token, let's proceed to migrate the content from HTML RTE to JSON RTE.

Using a Config File to Pass the Commands

Let's learn how to use this method through the following steps:

  1. Log in to your Contentstack account and go to your stack. Switch to the new Contentstack UI using the switch button.
    New_UI_switch.png
  2. Then, create a content type in your stack and add the HTML Rich Text Editor (RTE) field in it. Save and close the content type and create an entry in this newly created content type.
  3. Add some content inside the RTE field that you have added. Then, save the entry and close it.
  4. Edit the same content type and add a new JSON RTE field under the existing RTE content editor field and save your changes.
  5. Now let's migrate the content from the existing HTML RTE to the new JSON RTE using the config file.
    For this exercise, we have created a sample config code to help you add various parameters to it and easily migrate your content.
  6. {
        "alias": "management-token-alias",
        "content_type": "content-type",
        "isGlobalField": false,
        "paths": [
            {
                "from": "html-rte-field-path",
                "to": "json-rte-field-path"
            }
        ]
    }
  7. Copy the above config code, open any code editor, paste the code and add the required parameters as shown below:
    • alias: Enter the management token you created in the previous step.
    • content_type: Specify the UID of the content type of which you want to migrate the HTML RTE content.
    • IsGlobalField: Set this flag to true if the content type is a global field. It is set to false by default.

      Note: Remember to set the IsGlobalField flag to true in your config file if you are migrating RTE content present in the global field.

    • htmlPath: Enter the path to the HTML RTE field of which you want to migrate content.
    • jsonPath: Enter the path to the JSON RTE field to which you want to migrate the HTML RTE content.
  8. Note: The path to the JSON or HTML RTE is usually the UID of that specific field. However, if the HTML and JSON RTE fields are nested within another field, then specify the path to your RTE in the following format:
    {parent_field_UID}.{rte_field_UID}
    For example, if your HTML RTE field is nested within a Modular Blocks field, provide the RTE path as follows:
    {modular_block_field_UID}.{block_UID}.{rte_field_UID}

  9. After adding the above parameters, save the config file and run the following command in the CLI.
    csdx cm:migrate-rte -p <<path_of_the_config_file>>
    
    Option:
    -p: Specify the path where your config file is located.
    Example:
    csdx cm:migrate-rte -p “/home/admin/Desktop/config.json"

    Tip: Enclose the config file path in inverted commas if you are using a Windows system and the path to your respective RTE field contains a "space".

  10. After running the above command, the CLI will display the config file you specified and ask if you would like to continue with the configuration. Type "yes" to initiate the migration process.
    image.png
  11. Now go to your stack and refresh the page; the content from your existing RTE field will reflect in the JSON RTE field as shown below:JSON_RTE_Migration_Output_GIF.gif

    This completes your content migration process using the CLI. Your content is migrated to the new JSON RTE field by using the parameters you specified in the config file.

    Note: Using the config file to migrate content allows developers to add joins to the existing parameters and perform multiple HTML RTE to JSON RTE migrations at the same time.

    Jrte_Code_for_multiple_Migrations.png

Alternatively, you can avoid downloading and editing the config file and use flags to migrate the content. Let's discuss this next.

Using Flags to Migrate Content

In this method, we will discuss how to migrate content from your existing HTML RTE to the new JSON RTE using flags. 

After successfully creating the management token in step 1, run the following command to start the data migration process. 

Several parameters/flags are combined in the following command:

Command if RTE is in the content type.

csdx cm:migrate-rte -a <alias> -c <content_type_uid> -h <html_path> -j <json_path> -y(optional)

OR

Command if RTE is in the global field.

csdx cm:migrate-rte -a <alias> -c <global_field_uid> -g -h <html_path> -j <json_path> -y(optional)
  • -a, alias: Enter the management token you created in the previous step.
  • -c, content_type_uid: Specify the UID of the content type of which you want to migrate HTML RTE content.
  • -g, isGlobalField: Checks whether the specified UID belongs to a content type or a global field.
  • -h, htmlPath: Enter the path to the HTML RTE of which you want to migrate content.
  • -j, jsonPath: Enter the path to the JSON RTE to which you want to migrate the HTML RTE content.
  • -y (optional): To avoid reconfirmation of your configuration, add this flag.

Note: The path to the JSON or HTML RTE is usually the UID of that specific field. However, if the HTML and JSON RTE fields are nested within another field, then specify the path to your RTE in the following format:
{parent_field_UID}.{rte_field_UID}
For example, if your HTML RTE field is nested within a Modular Blocks field, provide the RTE path as follows:
{modular_block_field_UID}.{block_UID}.{rte_field_UID}

The above command will initiate the migration process. Now, go to your stack and refresh the entry page. You will see that the JSON RTE field reflects the same data as present in your HTML RTE

JSON_RTE_Migration_Output_GIF.gif

Points to Remember

To successfully perform the JSON RTE migration, remember the following rules:

  1. The migration only works when both the HTML and the JSON RTE fields are at the same field depth level.
  2. Both the RTE fields should have the same type of instances. If the JSON RTE is marked as multiple and HTML RTE is of single type, then you won't be able to migrate content.
  3. To run a successful migration, the HTML RTE and JSON RTE should allow embedded entries from the same set of referenced content types. If the referenced content types differ, you cannot migrate content between the rich text editors.
  4. After running the command, RTE content is migrated across each entry of the specified content type.

Was this article helpful?

Thanks for your feedbackSmile-icon

On This Page

^