huginn icon indicating copy to clipboard operation
huginn copied to clipboard

Javascript Agent JSON.parse is throwing an exception, but the same string can be parsed correctly in the browser

Open tempppabx1 opened this issue 1 year ago • 1 comments

Because I need on_change and extract, I used the Website Agent and get the following events (The original event was too long, so I manually condensed it to ensure readability)

{
    "content":"{\"id\":1000,\"des\":\"Here is the introduction!\\n1.Here is the introduction2!\",\"title\":\"Here is the title\"}"
}

In the browser enviroment, using JSON.parse(json.content) can correctly parse image In the Javascript Agent, JSON.parse(event['payload'].content) will get JavaScript error: SyntaxError: Unexpected token

I guess this might be related to escape characters, and the V8 engine in the browser handles this more effectively. So I tried using .replace(/\n/g,"\\n") which worked, but simply using "replace" still doesn't seem good enough. Are there any other methods to better parse a string?

tempppabx1 avatar Feb 05 '24 05:02 tempppabx1

The only thing I got to work was the Jq Agent. It would be something like this:

{
    "filter": "[ .content | fromjson ]"
}

virtadpt avatar Feb 05 '24 17:02 virtadpt

The only thing I got to work was the Jq Agent. It would be something like this:

{
    "filter": "[ .content | fromjson ]"
}

Thank you for your response, but this still won't work. Jq agent displaying an error "Invalid string: control characters from U+0000 through U+001F must be escaped" The issue might be related to escaped characters. \\n

tempppabx1 avatar Feb 22 '24 15:02 tempppabx1