Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

flatten nested json objects

Object.flatten = function(data) {
    var result = {};
    function recurse (cur, prop) {
        if (Object(cur) !== cur) {
            result[prop] = cur;
        } else if (Array.isArray(cur)) {
             for(var i=0, l=cur.length; i<l; i++)
                 recurse(cur[i], prop + "[" + i + "]");
            if (l == 0)
                result[prop] = [];
        } else {
            var isEmpty = true;
            for (var p in cur) {
                isEmpty = false;
                recurse(cur[p], prop ? prop+"."+p : p);
            }
            if (isEmpty && prop)
                result[prop] = {};
        }
    }
    recurse(data, "");
    return result;
}
Comment

Flatten a nested JSON

from flatten_json import flatten
dic_flattened = (flatten(d, '.') for d in test_json['result'])
df = pd.DataFrame(dic_flattened)

df.shape
(5, 160)
Comment

PREVIOUS NEXT
Code Example
Javascript :: mongoose get id after save 
Javascript :: render jsx in react 
Javascript :: Solution for Error [ERR_REQUIRE_ESM]: require() of ES Module 
Javascript :: two object combine together javascript 
Javascript :: string padStart padEnd 
Javascript :: add getter to object javascript 
Javascript :: animated typing js 
Javascript :: if array ontains any item of another array js 
Javascript :: variables in js 
Javascript :: reactjs date display 
Javascript :: react native run real device 
Javascript :: how to write a variable in js 
Javascript :: nginx get request method 
Javascript :: handling event in jsx 
Javascript :: how to validate express js form 
Javascript :: ckeditor config 
Javascript :: nestjs init 
Javascript :: javascript beginning of today and yesterday 
Javascript :: google tuner 
Javascript :: signed and unsigned integers in JavaScript 
Javascript :: wavesurf js 
Javascript :: javascript download image 
Javascript :: javascript string repeat 
Javascript :: JavaScript String endsWith() examples 
Javascript :: vue font awesome icons 
Javascript :: tinymce return text and html 
Javascript :: javascript window screen 
Javascript :: overflowx 
Javascript :: create bottom navigation bar react native 
Javascript :: is js object oriented 
ADD CONTENT
Topic
Content
Source link
Name
1+4 =