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

json2csv doesn't flatten values correctly #184

Closed
4 of 5 tasks
joshuali925 opened this issue May 14, 2021 · 4 comments
Closed
4 of 5 tasks

json2csv doesn't flatten values correctly #184

joshuali925 opened this issue May 14, 2021 · 4 comments

Comments

@joshuali925
Copy link

Background Information

  • Module Version: 3.11.1
  • Node/Browser Version: 10.23.1

The issue I'm reporting is with:

  • json2csv
  • csv2json

I have...

  • searched to see if an issue has already been reported.
  • verified that my JSON/CSV data is valid (using something like http://jsonlint.com or https://csvlint.io/).
  • tried upgrading to the latest version of json-2-csv (since the issue may already be fixed).

Expected Behavior

outputs values

Actual Behavior

outputs null

Data Sample

CSV:


(or)

JSON:

{
  "a.a": "a",
  "a.b": {
    "c": "b"
  }
}

Code Example

❯ cat test.json
{
  "a.a": "a",
  "a.b": {
    "c": "b"
  }
}

❯ npx json2csv test.json
a.a,a.b.c
a,null
@mrodrig
Copy link
Owner

mrodrig commented May 20, 2021

Thanks for reporting this @joshuali925. This wasn't a scenario I considered, but it's definitely valid. It looks like I'll need to rework things a bit in both the underlying key path generation module (deeks) and key path evaluation module (doc-path) to handle this in an efficient manner. I'll post back when I have progress to report.

mrodrig added a commit to mrodrig/doc-path that referenced this issue May 24, 2021
There are valid JSON keys which can contain a nested '.' in them.
However, this module would not handle those properly, except in a
limited set of conditions. This commit adds support for these paths when
they are properly escaped using a '\' character. However, this does
slightly change the base functionality for the module where a value with
the same key path as a top-level key with the nested value's key path
will now read the nested value, unless the provided path has appropriate
escaping on the key path.

For example... with the example doc:
    { 'a.b' : 2, a : { b : 3 } }

evaluatePath(doc, 'a.b') will now return 3
    and
evaluatePath(doc, 'a\\.b') will now return 2

Related to mrodrig/json-2-csv#184
mrodrig added a commit to mrodrig/deeks that referenced this issue May 26, 2021
This module did not previously offer a way to encode any nested '.'
characters in a key such that it could be easily identified for use by
the end user or other modules. As a result, any keys which had nested
'.' characters appeared as though they were simply another nested object
layer, but this could cause issues if trying to reconstruct the object.
This commit adds the escapeNestedDots option which allows the user to
optionally specify that this information should be encoded for later use
by the consuming software.

Related to mrodrig/json-2-csv#184
mrodrig added a commit that referenced this issue May 26, 2021
Through modifications to `doc-path` and `deeks` dependency underlying
code, I've worked to add support for the keys with nested dots in the
JSON key path and conversion back to CSV. This was achieved through the
`deeks` module's new `escapeNestedDots` option which will now be set to
`true`. Additionally, support for this escaping was added to `doc-path`
so that the correct values are retrieved and can also be correctly set
for csv2json. This commit adds the option specification for the `deeks`
module, and once the dependency modules are released, another commit
will be pushed which will update the dependency module versions to the
newest releases which add support.

Fixes #184
mrodrig added a commit to mrodrig/deeks that referenced this issue May 26, 2021
This module did not previously offer a way to encode any nested '.'
characters in a key such that it could be easily identified for use by
the end user or other modules. As a result, any keys which had nested
'.' characters appeared as though they were simply another nested object
layer, but this could cause issues if trying to reconstruct the object.
This commit adds the escapeNestedDots option which allows the user to
optionally specify that this information should be encoded for later use
by the consuming software.

Related to mrodrig/json-2-csv#184
mrodrig added a commit to mrodrig/deeks that referenced this issue May 26, 2021
* chore(release): 2.4.0

* Add escapeNestedDots option for preserving key structure. (#17)

This module did not previously offer a way to encode any nested '.'
characters in a key such that it could be easily identified for use by
the end user or other modules. As a result, any keys which had nested
'.' characters appeared as though they were simply another nested object
layer, but this could cause issues if trying to reconstruct the object.
This commit adds the escapeNestedDots option which allows the user to
optionally specify that this information should be encoded for later use
by the consuming software.

Related to mrodrig/json-2-csv#184
mrodrig added a commit to mrodrig/doc-path that referenced this issue May 26, 2021
* Add support for paths with escaped dots in path.

There are valid JSON keys which can contain a nested '.' in them.
However, this module would not handle those properly, except in a
limited set of conditions. This commit adds support for these paths when
they are properly escaped using a '\' character. However, this does
slightly change the base functionality for the module where a value with
the same key path as a top-level key with the nested value's key path
will now read the nested value, unless the provided path has appropriate
escaping on the key path.

For example... with the example doc:
    { 'a.b' : 2, a : { b : 3 } }

evaluatePath(doc, 'a.b') will now return 3
    and
evaluatePath(doc, 'a\\.b') will now return 2

Related to mrodrig/json-2-csv#184

* Infrastructure updates - node test, es version

* chore(release): 3.0.0

* Remove logic for unreachable condition

* Update README
mrodrig added a commit that referenced this issue May 26, 2021
Through modifications to `doc-path` and `deeks` dependency underlying
code, I've worked to add support for the keys with nested dots in the
JSON key path and conversion back to CSV. This was achieved through the
`deeks` module's new `escapeNestedDots` option which will now be set to
`true`. Additionally, support for this escaping was added to `doc-path`
so that the correct values are retrieved and can also be correctly set
for csv2json. This commit adds the option specification for the `deeks`
module and the updated versions of the dependency modules in
package.json.

Fixes #184
@mrodrig
Copy link
Owner

mrodrig commented May 26, 2021

Just wanted to give a quick update - I have the needed changes in the deeks and doc-path dependency modules complete and released to NPM. The only change that was needed to json-2-csv is also pushed up in a branch, but I need to formalize my manual test scripts into automated unit tests before I can get it out in a release. I'm hoping to have those written up and pushed in the next couple of days. Once those are all set, I should be able to release this shortly afterwards. Thanks for your patience.

@joshuali925
Copy link
Author

Got it, thanks a lot for your work!

mrodrig added a commit that referenced this issue May 28, 2021
* Add support for keys with nested dots.

Through modifications to `doc-path` and `deeks` dependency underlying
code, I've worked to add support for the keys with nested dots in the
JSON key path and conversion back to CSV. This was achieved through the
`deeks` module's new `escapeNestedDots` option which will now be set to
`true`. Additionally, support for this escaping was added to `doc-path`
so that the correct values are retrieved and can also be correctly set
for csv2json. This commit adds the option specification for the `deeks`
module and the updated versions of the dependency modules in
package.json.

Fixes #184
@mrodrig
Copy link
Owner

mrodrig commented May 28, 2021

I just merged and published the fix in 3.14.0 on NPM. Thanks again for reporting this. Feel free to let me know if you spot anything else that doesn't look right.

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

No branches or pull requests

2 participants