Bulk changes to posts inside JSON import file

Hi Guys

I’m running version 3.26.1 on this domain: https://www.blog.carnextdoor.com.au.

I’m trying to make changes to multiple posts at once by exporting everything into a JSON file, making changes to the html entity for the relevant articles, then importing the file back into Ghost.

The changes I have made to the JSON file involve turning specific keywords into links. For example, replacing the words ‘car rental’ with '<a href="https://www.carnextdoor.com.au/car-rental">car rental</a>. I’ve spent the past few days building a Python script to successfully make these changes in the html entity for each article within the JSON file.

Unfortunately, though, the new links aren’t appearing on the blog.

I noticed that there is a ‘plaintext’ entity for each post in the JSON file as well as the ‘html’ entity. Do I need to make the changes to that too? What is the relationship between the ‘plaintext’ entity and ‘html’ entity for each article?

Any help would be much appreciated! (I’ve spent so long creating the ‘find and replace’ Python script and it’s so annoying to be stuck on this final step :sweat_smile:)

Cheers

Sam

I noticed that there is a ‘plaintext’ entity for each post in the JSON file as well as the ‘html’ entity. Do I need to make the changes to that too? What is the relationship between the ‘plaintext’ entity and ‘html’ entity for each article?

Both the HTML and plaintext fields are generated from the mobiledoc field, which is Ghosts base content format. If you’re doing a full export, editing the html and uploading the full JSON back, the changes to the HTML field won’t be picked up.

For what you’re doing it would make way more sense and be simpler to use the API, not the importer.

https://ghost.org/docs/api/v3/admin/#posts

The importer expects to be working from a blank slate, so it’ll duplicate content.

Using the API with our SDKs for doing quick find and replace tasks like this is super quick - but that’s all written in JS.

If you prefer python, there isn’t an SDK but there is an example for how to do the trickiest part which is auth, here: https://ghost.org/docs/api/v3/admin/#authentication

FYI there is a demo of a find and replace API script in our api demos repo.

The first section shows how to do a find and replace in the mobiledoc field:

The second (commented) section shows how you can do a find and replace in HTML and tell the API to treat the HTML as the source for the content, rather than mobiledoc:

I hope this helps!

Hi Hannah, thanks for the speedy response!

Given I’m not that experienced with these technical things (APIs etc), and it took me a long time to build the Python script, I think it might be faster for me to stick with the exporting and importing JSON method.

FYI, it’s not a straightforward ‘find and replace’ for a single string. I’m checking the contents of each post against a list of keywords with accompanying URLs, and then updating the keyword in the article with the respective link.

If I amend the code to do the replacement on the mobiledoc object, will that work okay? Or would I need to do the same replacement on the mobiledoc and HTML?

Thanks
Sam