Convert JSON To String + Replace Chars For PDF.Co API

Describe the problem/error/question

I need to take the JSON response from a Shopify API call, convert it to a string for the entire JSON payload, then do a find/replace in the string using the replaceAll() method.

I need to replace the double quote character (") with it having an escape character of (), so all instances of " in the JSON will now be "

I am in a Code node but I can’t figure out how to either correctly access the string returned from the API call directly, or what I need to reference to stringify it first then do the replaceAll() on it.

This pr

What is the error message (if any)?

Please share your workflow

(Select the nodes on your canvas and use the keyboard shortcuts CMD+C/CTRL+C and CMD+V/CTRL+V to copy and paste the workflow.)

Share the output returned by the last node

Information on your n8n setup

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

If you provide a sample of your input data, and what you want it to look like that might help.

Intuitively I’d assume your API would accept normal json so you could just try {{ JSON.stringify ($json.myField) }}

1 Like

Thanks for replying. :slight_smile:

It’s Shopify’s API that is returning the JSON data that I need to a string version of with a character find/replace. Here’s a snippet of what was returned:

[
  {
    "id": 5605126242465,
    "admin_graphql_api_id": "gid://shopify/Order/5605126242422",
    "app_id": 580111,
    "browser_ip": "28.32.101.26",
    "buyer_accepts_marketing": true,
    "cancel_reason": null,
    "cancelled_at": null,
    "cart_token": "c1-9df49e8268f30928fbe092eedf60ed9f",
    "checkout_id": 29838134247585,
    "checkout_token": "1495c5f35f45cf7efb8adb20890d5344",
    "client_details": {
      "accept_language": "en-US",
      "browser_height": null,
      "browser_ip": "28.32.101.26",
      "browser_width": null,
      "session_hash": null,
      "user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36"
    },
    "closed_at": "2023-08-08T17:19:09-05:00",
    "confirmed": true,
    "contact_email": "[email protected]",
    "created_at": "2023-08-08T14:26:06-05:00",
    "currency": "USD",
    "current_subtotal_price": "200.00",
    "current_subtotal_price_set": {
      "shop_money": {
        "amount": "200.00",
        "currency_code": "USD"
      },
      "presentment_money": {
        "amount": "200.00",
        "currency_code": "USD"
      }
    },
    "current_total_discounts": "0.00",
    "current_total_discounts_set": {
      "shop_money": {
        "amount": "0.00",
        "currency_code": "USD"
      },
      "presentment_money": {
        "amount": "0.00",
        "currency_code": "USD"
      }
    },
    "current_total_duties_set": null,
    "current_total_price": "240.00",
    "current_total_price_set": {
      "shop_money": {
        "amount": "240.00",
        "currency_code": "USD"
      },
      "presentment_money": {
        "amount": "240.00",
        "currency_code": "USD"
      }
    },
    "current_total_tax": "30.00",
    "current_total_tax_set": {
      "shop_money": {
        "amount": "30.00",
        "currency_code": "USD"
      },
      "presentment_money": {
        "amount": "30.00",
        "currency_code": "USD"
      }
    },
    "customer_locale": "en-US",
    "device_id": null,
    "discount_codes": [],
    "email": "[email protected]",
    "estimated_taxes": false,
    "financial_status": "paid",
    "fulfillment_status": "fulfilled",
    "gateway": "Stripe",
    "landing_site": "/?utm_source=GMB",
    "landing_site_ref": null,
    "location_id": null,
    "merchant_of_record_app_id": null,
    "name": "ECOMM2124",
    "note": null,
    "note_attributes": [],
    "number": 1124,
    "order_number": 2124,
    "presentment_currency": "USD",
    "processed_at": "2023-08-08T14:26:04-05:00",
    "processing_method": "offsite",
    }
  }
]
  1. This needs to become (or access) an inline string of the Shopify API JSON payload, a snippet would look like this:
[{"id": 5605126222465,"admin_graphql_api_id": "gid://shopify/Order/5605126342465","app_id": 580111,"browser_ip": "28.32.101.26","buyer_accepts_marketing": true,"cancel_reason": null,"cancelled_at": null,"cart_token": "c1-9df49e8268f30928fbe082eedf60fd9d","checkout_id": 29338134247585,"checkout_token": "1494c5f25f45cf7efb8adb50890d5244"}]

And then do a replace all (likely using Javascript replaceAll method) to prepend the (") characters with an escape character (\). So the formatted JSON string would now look like this:

[{\"id\": 5605126222465,\"admin_graphql_api_id\": \"gid://shopify/Order/5605126342465\",\"app_id\": 580111,\"browser_ip\": \"28.32.101.26\",\"buyer_accepts_marketing\": true,\"cancel_reason\": null,\"cancelled_at\": null,\"cart_token\": \"c1-9df49e8268f30928fbe082eedf60fd9d\",\"checkout_id\": 29338134247585,\"checkout_token\": \"1494c5f25f45cf7efb8adb50890d5244\"}]

Pretty sure JSON escaping is what you’re looking for. Test out this workflow

When you look at the actual raw output of the final node not in the GUI, it looks like what you want

1 Like

Got it. The answer was just to run json.stringify twice, like this:

retVal = JSON.stringify(JSON.stringify($('Shopify API Get Order').item.json));
return $retVal;
3 Likes

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