Prompt Title: flatten json object

Created 1 year ago
Votes 0
Views 425 times
0
Human: I have the following json: { payment: { id: 1, amount: 100, org: { id: 2, name: 'or'} } } How can I flat it recursively for a bigger object in javascript the following output: { payment_id: 1, payment_amount: 100, payment_org_id: 2, payment_org_name: 'or' }
ChatGPT:

You can use a recursive function to flatten the JSON object and create new keys by concatenating the parent keys with the child keys separated by an underscore. Here's an example implementation:

javascript
function flatten(obj, prefix = '') { return Object.keys(obj).reduce((acc, key) => { const newKey = prefix ? `${prefix}_${key}` : key; if (typeof obj[key] === 'object') { Object.assign(acc, flatten(obj[key], newKey)); } else { acc[newKey] = obj[key]; } return acc; }, {}); } const json = { payment: { id: 1, amount: 100, org: { id: 2, name: 'or' } } }; const flattened = flatten(json); console.log(flattened); // { payment_id: 1, payment_amount: 100, payment_org_id: 2, payment_org_name: 'or' }

The flatten function takes two parameters: obj is the JSON object to be flattened, and prefix is used to keep track of the parent keys. The function returns a new object that has been flattened.

Inside the function, Object.keys is used to loop through all the keys in the current object. For each key, a new key is created by concatenating the prefix with the current key using an underscore. If the value of the current key is an object, the flatten function is called recursively with the current value and the new key as the prefix. Otherwise, the current key-value pair is added to the result object.

The reduce method is used to accumulate the flattened object, starting with an empty object ({}). At the end of each iteration, the current key-value pair is added to the accumulated object using Object.assign.

Finally, we call the flatten function with the input JSON object and log the resulting flattened object to the console.

Human: Tries this code on another object but I got the following error: Cannot convert undefined or null to object at Function.keys Can you fix this edge case?
ChatGPT:

Yes, the error you're seeing is likely due to the fact that the flatten function is being called with null or undefined as the obj parameter. To handle this edge case, you can add a check at the beginning of the function to return an empty object if obj is falsy.

Here's an updated version of the flatten function that handles this case:

javascript
function flatten(obj, prefix = '') { if (!obj) { return {}; } return Object.keys(obj).reduce((acc, key) => { const newKey = prefix ? `${prefix}_${key}` : key; if (typeof obj[key] === 'object') { Object.assign(acc, flatten(obj[key], newKey)); } else { acc[newKey] = obj[key]; } return acc; }, {}); } const json = { payment: { id: 1, amount: 100, org: { id: 2, name: 'or' } } }; const flattened = flatten(json); console.log(flattened); // { payment_id: 1, payment_amount: 100, payment_org_id: 2, payment_org_name: 'or' } const emptyJson = null; const flattenedEmpty = flatten(emptyJson); console.log(flattenedEmpty); // {}

In this updated version, we add a check at the beginning of the flatten function to see if obj is falsy (i.e. null, undefined, 0, '', etc.). If it is, we return an empty object {}.

With this check in place, calling flatten(null) or flatten(undefined) should now return an empty object instead of causing an error.

avatar
Bla
Shared 1 prompt
Created 1 year ago

Leave a Comment

Related Tag Prompts

94
0
151
0
Docker Pro
1 year ago 2023-01-29 06:08:59 AIPRM
308
0
MySQL Marvel
1 year ago 2023-02-13 00:05:32 AIPRM
260
0
Ansible Expert
1 year ago 2023-02-01 09:23:50 AIPRM
193
0
PostgreSQL Pro
1 year ago 2023-02-07 03:45:19 AIPRM
114
0
270
1
Dockerize me
1 year ago 2023-02-17 08:27:58 Chad Thompson-Smith