Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⭐ dict.recurse( .. ) #3082

Merged
merged 2 commits into from
Jan 22, 2024
Merged

⭐ dict.recurse( .. ) #3082

merged 2 commits into from
Jan 22, 2024

Conversation

arlimus
Copy link
Member

@arlimus arlimus commented Jan 21, 2024

Recurse through an arbitrary dict (JSON/YAML/...) structure and find entries that match a search criteria.

For example, you can set up a data structure like this:

{
  "users": [
    {"name": "bob"}
  ],
  "owners": {
    "admins": [
      {"name": "joy", "isOwner": true}
    ]
  }
}

Finding any user in this list is difficult with traditional mechanisms, since you'd have to understand the data structure and where to search.

With recurse it is made much easier:

jdata.recurse( name != empty )
[
  0: {
    name: "bob"
  }
  1: {
    isOwner: true
    name: "joy"
  }
]

You could then just grab the names and continue:

jdata.recurse( name != empty ).map(name)
[
  0: "bob"
  1: "joy"
]

Full JSON example with cnquery

Let's create a file t.json:

{
  "users": [
    {"name": "bob"}
  ],
  "owners": {
    "admins": [
      {"name": "joy", "isOwner": true}
    ]
  }
}

Then open a shell in the same folder:

> cnquery shell

We can now access the JSON file and recurse into it

cnquery> parse.json("t.json").params.recurse(name != empty).map(name)
parse.json.params.recurse.map: [
  0: "bob"
  1: "joy"
]

Recurse through an arbitrary dict (JSON/YAML/...) structure and find
entries that match a search criteria.

For example, you can set up a data structure like this:

```json
{
  "users": [
    {"name": "bob"}
  ],
  "owners": {
    "admins": [
      {"name": "joy", "isOwner": true}
    ]
  }
}
```

Finding any user in this list is difficult with traditional mechanisms,
since you'd have to understand the data structure and where to search.

With `recurse` it is made much easier:

```coffee
jdata.recurse( name != empty )
```

```
[
  0: {
    name: "bob"
  }
  1: {
    isOwner: true
    name: "joy"
  }
]
```

You could then just grab the names and continue:

```coffee
jdata.recurse( name != empty ).map(name)
```

```
[
  0: "bob"
  1: "joy"
]
```

Signed-off-by: Dominik Richter <[email protected]>
Copy link
Contributor

@scottford-io scottford-io left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This worked well for me @arlimus. ⭐

Copy link
Member

@chris-rock chris-rock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works like a charm! Great addition @arlimus

@chris-rock chris-rock merged commit ea8082d into main Jan 22, 2024
10 checks passed
@chris-rock chris-rock deleted the dom/recurse branch January 22, 2024 08:42
@github-actions github-actions bot locked and limited conversation to collaborators Jan 22, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants