JSON Manipulation to Python and from Python to Text Preserving Formatting

Hi everyone, how are you?

I need to preserve the exact text formatting as it is in the database, without making any additional manipulations, and just extract the text from the JSON. Can you help me with this procedure?

I have some information that I haven’t fully understood yet on how to do it in n8n:

  1. Load the JSON into a Python object.
  2. Extract the desired text without modifications.
  3. Send the text exactly as it is.

Here’s an example of how to do this:

import json

# Assuming your JSON is stored as a string
json_text = '''
         "text" : "the text here is to be sent\\n\\nthank you"

# Load the JSON into a Python object
data = json.loads(json_text)

# Extract the desired text
text_to_send = data[0]['text']

# Print the text (or send it to WhatsApp)

In this example:

  1. We define the JSON as a string. Note that the line breaks are represented as \\n\\n within the JSON string to be interpreted correctly as line breaks in Python.
  2. We use json.loads to convert the JSON string into a list of Python dictionaries.
  3. We access the text within the first dictionary of the list.
  4. We print the extracted text, which will preserve the original formatting with line breaks.

If the JSON string from the database already comes with properly escaped line breaks (\n\n), you don’t need to make any additional replacements. Python will interpret \n\n as two line breaks when the JSON string is loaded.

The output will be:

the text here is to be sent

thank you

Now you can use text_to_send directly in your automation with n8n to send the message via WhatsApp, preserving the original formatting.

Does anyone have a video explaining this?!

Thanks a lot!

It looks like your topic is missing some important information. Could you provide the following if applicable.

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.