JSON Stringify to pass API request

Guys,
I am facing an issue. Shopify doesn’t accept meta field (type: JSON) updates, if I provide JSON value which is not stringified - get “Bad Request” response. The problem is that I am providing a JSON in one of JSON’s parameter values.

Non-working JSON format

{
  "preferences": [
    {
      "key": "gender",
      "external_id": "30130450399498",
      "answers": ["Man"]
    },

Stringified JSON that works

{\"preferences\": [{\"key\": \"gender\", \"external_id\": \"30130450399498\", \"answers\": [\"Man\"]}

Any solutions on that?

P.S. There is only a piece of a big file, so it might be a JSON closing issue - ignore it :slight_smile:

You can use the Create JSON module to convert an existing collection into JSON.

2 Likes

The thing is that in my case JSON is not a collection - it’s a simple string… string with a lot of incompatible symbols. As you see in my example, stringification (if it’s the right definition) involves removing incompatible symbols, including replacing “Quotation marks” etc.
I thought maybe there was some function on Make like string(), but unfortunately it’s not :slight_smile:

The other option is to use the built-in replace function to do the replacement as you’ve mentioned.

2 Likes

Yeah, Replace function was in my head, but still, there is quite a complex replace logic - you leave some custom symbol not replaced and the whole request fails.

Alternatively, you can use the Transform to JSON module.

Screenshot_2024-04-30_190443

Screenshot_2024-04-30_190411

Module Export

You can copy and paste this module export into your scenario. This will paste the modules shown in my screenshots above.

  1. Copy the JSON code below by clicking the copy button when you mouseover the top-right of the code block
    Screenshot_2024-01-17_200117

  2. Enter your scenario editor. Press ESC to close any dialogs. Press CTRLV (paste keyboard shortcut for Windows) to paste directly in the canvas.

  3. Click on each imported module and save it for validation. You may be prompted to remap some variables and connections.

View Module Export Code

JSON

{
    "subflows": [
        {
            "flow": [
                {
                    "id": 3,
                    "module": "util:ComposeTransformer",
                    "version": 1,
                    "parameters": {},
                    "mapper": {
                        "value": "{\n  \"preferences\": [\n    {\n      \"key\": \"gender\",\n      \"external_id\": \"30130450399498\",\n      \"answers\": [\n        \"Man\"\n      ]\n    }\n  ]\n}"
                    },
                    "metadata": {
                        "designer": {
                            "x": -46,
                            "y": 0,
                            "name": "Compose JSON String"
                        },
                        "restore": {},
                        "expect": [
                            {
                                "name": "value",
                                "type": "text",
                                "label": "Text"
                            }
                        ]
                    }
                },
                {
                    "id": 5,
                    "module": "json:TransformToJSON",
                    "version": 1,
                    "parameters": {
                        "space": ""
                    },
                    "mapper": {
                        "object": "{{3.value}}"
                    },
                    "metadata": {
                        "designer": {
                            "x": 249,
                            "y": 0,
                            "messages": [
                                {
                                    "category": "last",
                                    "severity": "warning",
                                    "message": "A transformer should not be the last module in the route."
                                }
                            ]
                        },
                        "restore": {
                            "parameters": {
                                "space": {
                                    "label": "Empty"
                                }
                            }
                        },
                        "parameters": [
                            {
                                "name": "space",
                                "type": "select",
                                "label": "Indentation",
                                "validate": {
                                    "enum": [
                                        "tab",
                                        "2",
                                        "4"
                                    ]
                                }
                            }
                        ],
                        "expect": [
                            {
                                "name": "object",
                                "type": "any",
                                "label": "Object"
                            }
                        ]
                    }
                }
            ]
        }
    ],
    "metadata": {
        "version": 1
    }
}
1 Like