Get the Key Name from JSON

Hi, I have a JSON with values:

From all those keys, I want to extract the KEY Name, of the key that is not null. For example, in this case, I want to get “Datos de tarjeta de crédito ($2500)”. I do NOT need the value, I need the KEY Name.

Any idea? Thank you!

Welcome to the Make community!

You can use the module Parse JSON, then in an iterator module you can use the built-in function toArray

e.g.:

toArray(collection)

Then you can aggregate back into an array, with a filter that checks that the value is not null.

samliewrequest private consultation

Join the unofficial Make Discord server to chat with other makers!

1 Like

Thank you! But I need to get the KEY NAME, not the result

For example, in this case:

Screenshot_3

I want to get as result: Datos de tarjeta de crédito ($2500)

For further assistance, please provide the following:

Please provide the output bundles of the modules by running the scenario, then click the white speech bubble on the top-right of each module and select “Download output bundles”.
Screenshot_2023-10-06_141025

A.

Save the bundle contents in your text editor as a bundle.txt file, and upload it here into this discussion thread.

Uploading it here will look like this:

bundle.txt (12.3 KB)

B.

If you are unable to upload files on this forum, alternatively you can paste the formatted output bundle in this manner:

  • Either add three backticks ``` before and after the code, like this:

    ```
    input/output bundle content goes here
    ```

  • Or use the format code button in the editor:
    Screenshot_2023-10-02_191027

Providing the output bundles will allow others to replicate what is going on in the scenario even if they do not use the external service.

Following these steps will allow others to assist you here. Thanks!

samliewrequest private consultation

Join the unofficial Make Discord server to chat with us!

1 Like

Here’s the bundle!

bundle.txt (1.8 KB)

Welcome to the Make community!

(Assuming you have the JSON string)

Yes, that is possible. You’ll need a minimum of three modules:

Screenshot_2024-02-20_151445

You can use a Text Parser “Match Pattern” module with this Pattern (regular expression):

"(?<key>[^"]+)":\s+"",

Proof https://regex101.com/r/Cp8S04

Important Info

  • :warning: Global match must be set to YES!

Screenshot

Output


For more information, see Text Parser in the Make Help Center:

Match Pattern
The Match pattern module enables you to find and extract string elements matching a search pattern from a given text. The search pattern is a regular expression (aka regex or regexp), which is a sequence of characters in which each character is either a metacharacter, having a special meaning, or a regular character that has a literal meaning.

Hope this helps!

Give it a go and let us know if you have any issues!

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": 62,
                    "module": "util:ComposeTransformer",
                    "version": 1,
                    "parameters": {},
                    "mapper": {
                        "value": "[\n    {\n        \"Tipo de boleto\": \"Oro ($4000)\",\n        \"Nombre\": \"Carmina\",\n        \"Apellido\": \"Hello\",\n        \"Email\": \"hola@hello.com\",\n        \"Teléfono\": \"+52564498\",\n        \"Nombre de marca\": \"Hello ma\",\n        \"Pais de origen\": \"México\",\n        \"Instagram\": \"@Hello\",\n        \"¿Es tu primer evento?\": \"Si\",\n        \"Como te enteraste del evento\": \"BSuper\",\n        \"Tipo de comida\": \"Regular\",\n        \"Tipo de mesa\": \"Espacio unicamente\",\n        \"Necesitas mantel\": \"\",\n        \"¿Tienes cupón de descuento?\": \"\",\n        \"Monto a pagar: $247\": \"\",\n        \"Monto a pagar: $0\": \"\",\n        \"Monto a pagar: $187\": \"\",\n        \"Monto a pagar: $177\": \"\",\n        \"Monto a pagar: $157\": \"\",\n        \"Monto a pagar: $227\": \"\",\n        \"Monto a pagar: $150\": \"\",\n        \"Monto a pagar: $225\": \"\",\n        \"Monto a pagar: $275\": \"\",\n        \"Monto a pagar: $500\": \"\",\n        \"Monto a pagar: $2500\": \"\",\n        \"Monto a pagar: $4000\": \"\",\n        \"Monto a pagar: $6000\": \"\",\n        \"Datos de tarjeta de crédito ($247)\": \"\",\n        \"Datos de tarjeta de crédito ($177)\": \"\",\n        \"Datos de tarjeta de crédito ($187)\": \"\",\n        \"Datos de tarjeta de crédito ($157)\": \"\",\n        \"Datos de tarjeta de crédito ($227)\": \"\",\n        \"Datos de tarjeta de crédito ($150)\": \"\",\n        \"Datos de tarjeta de crédito ($225)\": \"\",\n        \"Datos de tarjeta de crédito ($275)\": \"\",\n        \"Datos de tarjeta de crédito ($500)\": \"\",\n        \"Datos de tarjeta de crédito ($2500)\": \"\",\n        \"Datos de tarjeta de crédito ($4000)\": \"pi_3PMEhNAAw3EQaeeA1X9eK1qB\",\n        \"Datos de tarjeta de crédito ($6000)\": \"\",\n        \"No Label field_c7c22bd\": \"on\",\n        \"No Label field_ba8bf0b\": \"on\",\n        \"form_id\": \"bd5f57f\",\n        \"form_name\": \"New Form\"\n    }\n]"
                    },
                    "metadata": {
                        "designer": {
                            "x": -724,
                            "y": -1776
                        },
                        "restore": {},
                        "expect": [
                            {
                                "name": "value",
                                "type": "text",
                                "label": "Text"
                            }
                        ]
                    }
                },
                {
                    "id": 63,
                    "module": "regexp:Parser",
                    "version": 1,
                    "parameters": {
                        "pattern": "\"(?<key>[^\"]+)\":\\s+\"\",",
                        "global": true,
                        "sensitive": false,
                        "multiline": false,
                        "singleline": false,
                        "continueWhenNoRes": false,
                        "ignoreInfiniteLoopsWhenGlobal": false
                    },
                    "mapper": {
                        "text": "{{62.value}}"
                    },
                    "metadata": {
                        "designer": {
                            "x": -483,
                            "y": -1777
                        },
                        "restore": {},
                        "parameters": [
                            {
                                "name": "pattern",
                                "type": "text",
                                "label": "Pattern",
                                "required": true
                            },
                            {
                                "name": "global",
                                "type": "boolean",
                                "label": "Global match",
                                "required": true
                            },
                            {
                                "name": "sensitive",
                                "type": "boolean",
                                "label": "Case sensitive",
                                "required": true
                            },
                            {
                                "name": "multiline",
                                "type": "boolean",
                                "label": "Multiline",
                                "required": true
                            },
                            {
                                "name": "singleline",
                                "type": "boolean",
                                "label": "Singleline",
                                "required": true
                            },
                            {
                                "name": "continueWhenNoRes",
                                "type": "boolean",
                                "label": "Continue the execution of the route even if the module finds no matches",
                                "required": true
                            },
                            {
                                "name": "ignoreInfiniteLoopsWhenGlobal",
                                "type": "boolean",
                                "label": "Ignore errors when there is an infinite search loop",
                                "required": true
                            }
                        ],
                        "expect": [
                            {
                                "name": "text",
                                "type": "text",
                                "label": "Text"
                            }
                        ],
                        "interface": [
                            {
                                "type": "text",
                                "name": "key",
                                "label": "key"
                            },
                            {
                                "type": "uinteger",
                                "name": "i",
                                "label": "i"
                            },
                            {
                                "type": "any",
                                "name": "__IMTMATCH__",
                                "label": "Fallback Match"
                            }
                        ]
                    }
                },
                {
                    "id": 64,
                    "module": "builtin:BasicAggregator",
                    "version": 1,
                    "parameters": {
                        "feeder": 63
                    },
                    "mapper": {
                        "key": "{{63.key}}"
                    },
                    "metadata": {
                        "designer": {
                            "x": -236,
                            "y": -1778
                        },
                        "restore": {
                            "extra": {
                                "feeder": {
                                    "label": "Text parser - Match pattern [63]"
                                },
                                "target": {
                                    "label": "Custom"
                                }
                            }
                        }
                    }
                },
                {
                    "id": 65,
                    "module": "util:SetVariable2",
                    "version": 1,
                    "parameters": {},
                    "mapper": {
                        "name": "keys",
                        "scope": "roundtrip",
                        "value": "{{map(64.array; \"key\")}}"
                    },
                    "metadata": {
                        "designer": {
                            "x": 7,
                            "y": -1779
                        },
                        "restore": {
                            "expect": {
                                "scope": {
                                    "label": "One cycle"
                                }
                            }
                        },
                        "expect": [
                            {
                                "name": "name",
                                "type": "text",
                                "label": "Variable name",
                                "required": true
                            },
                            {
                                "name": "scope",
                                "type": "select",
                                "label": "Variable lifetime",
                                "required": true,
                                "validate": {
                                    "enum": [
                                        "roundtrip",
                                        "execution"
                                    ]
                                }
                            },
                            {
                                "name": "value",
                                "type": "any",
                                "label": "Variable value"
                            }
                        ],
                        "interface": [
                            {
                                "name": "keys",
                                "label": "keys",
                                "type": "any"
                            }
                        ]
                    }
                }
            ]
        }
    ],
    "metadata": {
        "version": 1
    }
}

samliewrequest private consultation

Join the unofficial Make Discord server to chat with other makers!

1 Like

Hello, you are not answering my question.

The key name referes to name of the field.

Let’s say that I have this JSON:

    {
        "Datos de tarjeta de crédito ($247)": "",
        "Datos de tarjeta de crédito ($177)": "",
        "Datos de tarjeta de crédito ($187)": "",
        "Datos de tarjeta de crédito ($157)": "",
        "Datos de tarjeta de crédito ($227)": "",
        "Datos de tarjeta de crédito ($150)": "",
        "Datos de tarjeta de crédito ($225)": "",
        "Datos de tarjeta de crédito ($275)": "",
        "Datos de tarjeta de crédito ($500)": "",
        "Datos de tarjeta de crédito ($2500)": "",
        "Datos de tarjeta de crédito ($4000)": "pi_3PMEhNAAw3EQaeeA1X9eK1qB",
        "Datos de tarjeta de crédito ($6000)": "",
    }

I want to get as result the KEY NAME of the one that is not empty.

In this case, I want to get as output “Datos de tarjeta de crédito ($4000)”

Oops, my bad, I read it as you wanted the keys with empty values, so the regex needs a little modification to select keys without empty values:

"(?<key>[^"]+)":\s+"[^"]+",?

everything else remains the same.

1 Like

In the text parser you use in “Text” field > “62.Text”

From where it comes that?

That should come from your JSON string in your first screenshot. Do not use the Parse JSON module on the JSON yet, but map the JSON into this text parser module.

samliewrequest private consultation

Join the unofficial Make Discord server to chat with us!

1 Like