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

How to get all nested objects via bloblang #1036

Closed
packman80 opened this issue Jan 2, 2022 · 3 comments
Closed

How to get all nested objects via bloblang #1036

packman80 opened this issue Jan 2, 2022 · 3 comments
Labels
bloblang Bloblang features question

Comments

@packman80
Copy link

packman80 commented Jan 2, 2022

Assume, I've this JSON

[
    {
        "key1": "value1",
        "key2": "value2",
        "subs": [
            {
                "key3": "value3",
                "key4": "value4",
                "subs": [
                    {
                        "key5": "value5",
                        "key6": "value6"    
                    }
                ]
            },
            {
                "key7": "value7",
                "key8": "value8"
            },
            {
                "key9": "value9",
                "key10": "value10"
            }
        ]
    }
]

JSON can have arbitrary nesting, but has static field "subs" for nested objects.

I need to get all objects (incl nested):

[
           {
                "key1": "value1",
                "key2": "value2"
            },
           {
                "key3": "value3",
                "key4": "value4"
            },
           {
                "key5": "value5",
                "key6": "value6"
            },
           {
                "key7": "value7",
                "key8": "value8"
            },
            {
                "key9": "value9",
                "key10": "value10"
            }
]

How can i achieve this?

@Jeffail
Copy link
Collaborator

Jeffail commented Jan 2, 2022

@packman80 I think this should work:

map extract_subs {
  let collapsed_subs = if this.subs != null {
    this.subs.fold({}, item -> item.tally.merge(item.value.apply("extract_subs")))
  } else { {} }
  
  root = this.without("subs").merge($collapsed_subs)
}

root = this.fold({}, item -> item.tally.merge(item.value.apply("extract_subs")))

@Jeffail Jeffail added bloblang Bloblang features question labels Jan 2, 2022
@packman80
Copy link
Author

packman80 commented Jan 3, 2022

It produces one object. Not exactly what i need (array of objects). I will try to improve it for my task. Thanks anyway.

@mihaitodor
Copy link
Collaborator

mihaitodor commented Jan 4, 2022

I have a mapping that does the job as requested, but it's a bit hackish:

map do_obj {
    let innerObjs = this.subs.map_each(v -> v.apply("do_obj")).or([])
    
    root = [this.without("subs")].merge($innerObjs).flatten()
}

root = this.map_each(v -> v.apply("do_obj")).flatten()

Besides the two calls to flatten(), which I think should be avoidable somehow, that should do the trick.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bloblang Bloblang features question
Projects
None yet
Development

No branches or pull requests

3 participants