Issue Parsing JSON and Using Variables in Snippets

Hi Text Blaze Community,

I’ve been working on a snippet to parse JSON (created from a LLM chat window) from my clipboard and automatically fill fields in Apple Mail. However, I keep encountering errors, and I’m hoping someone can help me troubleshoot. Here’s what I’ve tried so far:

Context:

I want to copy JSON like the following example to my clipboard and use a Text Blaze snippet to fill in the "To," "Subject," and "Body" fields in Apple Mail:

{
"email": "example@example.com",
"subject": "Follow-up on Project Proposal",
"body": "Dear Team,\n\nI hope this email finds you well. I wanted to follow up on our discussion regarding the project proposal. Please let me know if you need any additional information.\n\nBest regards,\n[Your Name]"
}

Steps I’ve Tried:

  1. Basic Parsing Attempt
    Here’s the snippet I initially wrote:
    {=email_data = fromjson({clipboard})}
    To: {=email_data.email}{key: tab}
    Subject: {=email_data.subject}{key: tab}
    Body: {=email_data.body}

Issue: I get an error saying Unknown name "email_data".

  1. Adding Validation with {if}
    I updated the snippet to validate the clipboard content before parsing:

{if: testregex({clipboard}, "^\{.*\}$")}
{=email_data = fromjson({clipboard})}
To: {=email_data.email}{key: tab}
Subject: {=email_data.subject}{key: tab}
Body: {=email_data.body}
{else}
[Error: Invalid JSON structure in clipboard]
{endif}

Issues Encountered:

  1. "Invalid formula for {if}" – The condition doesn’t seem to work properly.

  2. "Unknown name 'email_data'" – Even after validation, the variable isn’t recognized.

  3. Error Handling Attempt
    I tried adding more robust error handling:

{if: testregex({clipboard}, "^\{.*\}$")}
{=email_data = fromjson({clipboard})}
{if: typeof(email_data) != "list"}
To: {=email_data.email}{key: tab}
Subject: {=email_data.subject}{key: tab}
Body: {=email_data.body}
{else}
[Error: Invalid JSON structure]
{endif}
{else}
[Error: No JSON found in clipboard]
{endif}

Issues Encountered:

  • The {typeof} validation doesn’t seem to work as expected.
  • The regex validation fails with an error about "lone quantifier brackets."
  1. How can I properly initialize and use variables like email_data after parsing JSON?
  2. What’s the correct way to validate clipboard content before parsing it as JSON?
  3. Are there better approaches for handling errors or ensuring smooth parsing?
  • I’ve verified that my JSON is valid using external tools like JSONLint.
  • My Text Blaze version is up-to-date.
  • I’ve tried using extractregex() for simpler parsing but couldn’t get it to work with nested fields.

Any advice or guidance would be greatly appreciated! :folded_hands:

Or is there maybe a better way to create recipient, subject and mail body from an LLM and parse it using text blaze?

Hi @Blazingfast
I believe that the issue is simply replacing:
{=email_data = fromjson({clipboard})}
which is a statement (true if email_data is equal to formjson({clipboard}))

with

{email_data= fromjson({clipboard})}
(The position of the equal sign matters.)
which defines the variable email_data

Let me know if that works.

1 Like

Hi @Dan_Barak1 , thanks a LOT! You are fantastic! :slight_smile:

Thank you for all the help so far with my JSON parsing issue! I've got the email content extraction working correctly.
However, I've noticed something strange with the formatting in Apple Mail:
The email body text that gets inserted from the parsed JSON (plain text) appears in grey color
My signature, which I added manually using Text Blaze in a separate snippet, appears in black as expected
Both are plain text being inserted by Text Blaze, but they're rendering with different colors. This is confusing since they're both being inserted using the same tool into the same email.
I could set my whole email to "plain text" but then I loose bold formatting of my signature.
Has anyone encountered this issue before? Is there something about how parsed JSON content is inserted that affects text formatting or color? Any suggestions for ensuring consistent text color when inserting content from JSON?
Can I set the font and colour of the parsed text that gets inserted manually in text blaze?
Thank you!

Could you please try to clear the styling of both your JSON insertion and also the signature? You can do this by selecting the text (or in the case of your JSON insertion, the command chip) and clicking this icon on the toolbar: