Retrieve first element from an array in this "complex" (for me) JSON structure

Example JSON: https://www.reddit.com/r/artificial/comments/1edo436/ai_meets_spanish_artists_kling_img2video/.json

I would like to retrieve the amount of upvotes “ups”, currently sitting at 325:

The inline functions I have tried:

{{3.data.children[0].data.ups}}

Result: this returns 2 operations but an empty value, if I dont put the 0 I get two operations, one with the value im after and another one

{{map(3.data.children; "data.ups")}}

Result: This gives me a result but I get 2 results again, 2 operations with 1 containing the 325 value that I want and another array with multiple values.

{{first(map(3.data.children; "data.ups"))}}

Result: This results in 2 operations, one with the value im expecting and another one.

{{ 3.data.children.1.data.ups }}

Hope this helps! Let me know if there are any further questions or issues.

You can also join us in the Make Fans Discord server to chat with other makers!

Thanks for your reply!

Result: Im getting two results

This is likely from a module before the Reddit module.

If you need further assistance, please provide the following:

1. Scenario blueprint

Please export the scenario blueprint file to allow others to view the mappings and settings. At the bottom of the scenario editor, you can click on the three dots to find the Export Blueprint menu item.


(Note: Exporting your scenario will not include private information or keys to your connections)

Uploading it here will look like this:

blueprint.json (12.3 KB)

2. And most importantly, Input/Output bundles

Please provide the output bundles of the modules by running the scenario (or get from the scenario History tab), then click the white speech bubble on the top-right of each module and select “Download input/output bundles”.

A.

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

Uploading them here will look like this:

module-1-output-bundle.txt (12.3 KB)

B.

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

Here are two ways to format text so that it won’t be changed by the forum:

A. Type code block manually
Add three backticks ``` before and after the content/bundle, like this:

```
content goes here
```

B. Highlight and click the format button in the editor

Providing the input/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!

You can also join us in the Make Fans Discord server to chat with other makers!

1 Like

1. Blueprint
blueprint reddit json.json (65.9 KB)

2.1 Input bundle: Parse JSON module [3]
input bundle parse JSON 3.json (62.5 KB)

2.1 Output bundle: Parse JSON module [3]
output bundle parse JSON 3.json (131.0 KB)

2.2 Input bundle: Set variable [1]
Operation 1:

[
    {
        "name": "upvote score",
        "scope": "roundtrip",
        "value": 325
    }
]

Operation 2:

[
    {
        "name": "upvote score",
        "scope": "roundtrip",
        "value": 29
    }
]

2.2 Output bundle: Set variable [1]
Operation 1

[
    {
        "upvote score": 325
    }
]

Operation 2

[
    {
        "upvote score": 29
    }
]

These are the input / output bundles for last ran settings:

{{ 3.data.children.1.data.ups }}

Welcome to the Make community!

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

As there is more than one listing, you’ll need to aggregate to an array so that you can get the first item’s ups.

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

  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.

Click to Expand Module Export Code

JSON - Copy and Paste this directly in the scenario editor

{
    "subflows": [
        {
            "flow": [
                {
                    "id": 5,
                    "module": "builtin:BasicAggregator",
                    "version": 1,
                    "parameters": {
                        "feeder": 3
                    },
                    "mapper": {
                        "kind": "{{3.kind}}",
                        "data": "{{3.data}}"
                    },
                    "metadata": {
                        "designer": {
                            "x": 243,
                            "y": -2
                        },
                        "restore": {
                            "extra": {
                                "feeder": {
                                    "label": "JSON - Parse JSON [3]"
                                },
                                "target": {
                                    "label": "Custom"
                                }
                            }
                        }
                    }
                },
                {
                    "id": 6,
                    "module": "util:SetVariable2",
                    "version": 1,
                    "parameters": {},
                    "mapper": {
                        "name": "ups",
                        "scope": "roundtrip",
                        "value": "{{first(map(5.array; \"data.children.1.data.ups\"))}}"
                    },
                    "metadata": {
                        "designer": {
                            "x": 434,
                            "y": -3
                        },
                        "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": "ups",
                                "label": "ups",
                                "type": "any"
                            }
                        ]
                    }
                }
            ]
        }
    ],
    "metadata": {
        "version": 1
    }
}

Hope this helps! Let me know if there are any further questions or issues.

@samliew

2 Likes

Thank you! This is the first time the use of the aggregator actually completely clicked for me as to where and why I should use it so thanks for that.

EDIT:

No haha sorry I have one additional question since it is directly related to testing and putting it into practice, once I have tested this successfully and I move the “parse JSON” module to the “Make An Oauth 2.0 request” to pull the JSON through reddit’s API, the structure is slightly different:

Input bundle:
Input bundle.json (148.1 KB)

Blueprint:
blueprint-2.json (75.6 KB)

While I still think it can be a bit more efficient my scenario, I think its fine like this:

  1. HTTP
  2. Parse JSON
  3. Aggregate
  4. Set variable

Latter 3 modules have the exact same setup.

Since the HTTP modules has an option to parse json I thought I could remove the 2nd module, but it messes it up. I am able to retrieve my data regardless so should you find joy in helping out to make it more efficient than that be awesome but not strictly necessary :slight_smile:

Thanks again 4 the support!