Handling nested objects
See original GitHub issueHi,
I am trying to fetch some records from MongoDB, where there are some properties whose value itself is a nested JSON object. Is there an easy method to ‘un-pack’ the nested properties into top-level CSV columns.
E.g., suppose I have an array of n
objects of the following format:
{ first_name: 'Raghu', last_name: 'Ugare', location: { 'city': 'Cambridge', 'state': 'MA' } }
I know we can easily map first_name
& last_name
as columns. But is there a simple way to map location.city
& location.state
as well? Or should we do it manually?
Thanks for this anyways!
Issue Analytics
- State:
- Created 8 years ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Accessing Nested Objects in JavaScript | HackerNoon
Array reduce method is very powerful and it can be used to safely access nested objects. const getNestedObject = (nestedObj, pathArr) => { ......
Read more >Nested objects in javascript, best practices - Stack Overflow
I would like to know the correct way to create a nested object in javascript. I want a base object called "defaultsettings".
Read more >How to access nested json objects in JavaScript?
Accessing nested json objects is just like accessing nested arrays. Nested objects are the objects that are inside an another object.
Read more >JSON nested objects - IBM
Objects can be nested inside other objects. Each nested object must have a unique access path. The same field name can occur in...
Read more >Creating Nested Objects in JavaScript Dynamically - LinkedIn
The basic definition of an object in JavaScript is a container for named values called properties (keys). Sometimes, we need to create an ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@raghuugare The reason why handling nested objects is not implemented is there are so many different cases in nested json objects. It can be nested objects as the example you gave or it can be nested objects with nested arrays. I faced the same problem as well; however, it is doable depends on individual case.
First, you need to consider the format of the CSV. Using your example and I want my CSV looks like:
And then convert your example to an array of json objects (get rid of all the nested objects or arrays into a single array of objects):
[{first_name: 'Raghu', last_name: 'Ugare', 'city': 'Cambridge', 'state': 'MA' } ]
I will suggest you to take a look
underscore.js
library to help you manipulating objects.hi @raghuugare , I have a small project, it used to export excel from json. I support nested json.You can refer
https://github.com/dtagdev1/json-export-excel
http://plnkr.co/edit/6ieuJ1khmKFds9VYHoDv