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

Blank array not flattened! #66

Open
shishir99111 opened this issue Oct 26, 2017 · 4 comments
Open

Blank array not flattened! #66

shishir99111 opened this issue Oct 26, 2017 · 4 comments

Comments

@shishir99111
Copy link

I'm using flat module for preparing object to store data in Redis DB. (Redis doesn't allow array and nested object )
Now, the Issue is Flat doesn't do anything to blank array.
E.g.

{
    a: [],
    a1: {
        a11: 1
    }
    b: 'test',
    c: 12
}
is flatten to 
{
    a: [],
    a1.a11: 1,
    b: "test",
    c: 12,
}

this gives me error on Redis. Can Flat module help me with this?

@aecepoglu
Copy link

And what should flat do instead? Remove the empty array?

@shishir99111
Copy link
Author

shishir99111 commented Oct 26, 2017

@aecepoglu not sure whether this module is intended to achieve it or not.
But, I required my JSON Object should be flat (no nesting of object, no array inside the object. not even if it is blank).
So, i think there should be a notation which will point as blank object and a blank array.

@edosssa
Copy link

edosssa commented Jun 13, 2018

I know I'm a a little late to the party. @shishir99111 I don't think Flat is supposed to do that. It does what it's supposed to do - flatten objects, and not remove empty arrays from flattened objects.
If you don't want empty arrays in your flattened object (for some reason), it's left to you to cycle through the flattened object and perhaps delete properties whose values are arrays.
P.s. if you don't want arrays so bad, why'd you include them in the original object in the first place? :)

@lucas-rudd
Copy link

I've opened up a similar issue regarding this.
Flatten does the same thing with empty objects, where a is an object instead of an array.

Because of this, the object returned is not 'flattened', as an empty object is still nested within the original object.

This is causing us some issues as well, and we have written code to go through and remove these (they aren't needed in our specific case) but that means we cannot unflatten the resulted object to its original state.

A better solution may be to do something like

{
    a: '[Object|Array],
    a1.a11: 1,
    b: "test",
    c: 12,
}

or

{
    a: '[Object|Object]',
    a1.a11: 1,
    b: "test",
    c: 12,
}

to indicate that we have an empty object/array.

This is an odd case, because it requires some semi-unique value to indicate that the unflattened object should contain an empty nested object/array, rather than interpreting the string literally. In which case, if your original object had a literal string which was '[Object|Array]' for whatever reason, it would get converted to an array in the unflattening process, which is not what we would want.

Regardless, in order to fully 'flatten' an object, empty objects must also be flattened in the process.

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

No branches or pull requests

4 participants