question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Handling nested objects

See original GitHub issue

Hi,

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:closed
  • Created 8 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
shaohaolincommented, Oct 13, 2015

@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: result

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.

1reaction
trantrunghieu0809commented, Oct 26, 2015

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found