Flatten nested json into multiple rows
I am working on a heavily nested json file that contains multiple dictionaries and lists. Some of the keys are similar and I am wondering if I can put each dictionary into different row. It doesn't matter, if some columns are not the same, the values can be left blank.
Json file (in similar form to the original)
"ID": "01",
"session": [
"A": [
"abc",
"cde",
"efj"
],
"B": 14,
"C": 14,
"D": [
"F":
"a": "ldjf",
"b": "kdj",
"ID": "01",
"c": "kjasgfk",
"d": [
"pw"
],
"e": "dsg"
,
"F":
"a": "ldjewiorf",
"ID": "01",
"c": "kjasnbgfk",
"d": "mbxzc" ,
"e": "dsg"
,
"F":
"f": "1232",
"g": "rege",
"h": "en-gb",
"i": "dfkj34",
"j": "iyt658"
],
"properties":
"AA":"esg",
"BB": "skdjghk",
"CC": "adfkh",
"DD": "sdlkfh"
,
"A": [
"abc",
"cde",
"efj"
],
"B": 16,
"C": 14,
"D": [
"F":
"a": "sdg",
"b": "sg",
"ID": "01",
"c": "sg",
"d": "shfh",
"e": "weitu"
,
"F":
"f": "1232",
"m": "sdg",
"n": "en-sdg",
"o": "eqe",
"p": "sdg"
],
"properties":
"AA":"ekjhsg",
"BB": "skkldjghk",
"CC": "adfyurkh",
"DD": "sdlkfmlh"
],
"G":
"A1":
"year": 2016,
"month": 5,
"dayOfMonth": 1,
"hourOfDay": 0,
"minute": 0,
"second": 0
,
"A2": "ksjdf",
"A3": "s38764",
"A4": [
"year": 2016,
"month": 5,
"dayOfMonth": 1,
"hourOfDay": 0,
"minute": 0,
"second": 0
]
I tried this code, but puts the whole file in 1 row with multiple columns:
def flatten_json(y):
out =
def flatten(x, name=''):
if type(x) is dict:
for a in x:
flatten(x[a], name + a + '_')
elif type(x) is list:
i = 0
for a in x:
flatten(a, name + str(i) + '_')
i += 1
else:
out[name[:-1]] = x
flatten(y)
return out
Is there a solution of what I am trying to do or is it impossible?
python json
add a comment |
I am working on a heavily nested json file that contains multiple dictionaries and lists. Some of the keys are similar and I am wondering if I can put each dictionary into different row. It doesn't matter, if some columns are not the same, the values can be left blank.
Json file (in similar form to the original)
"ID": "01",
"session": [
"A": [
"abc",
"cde",
"efj"
],
"B": 14,
"C": 14,
"D": [
"F":
"a": "ldjf",
"b": "kdj",
"ID": "01",
"c": "kjasgfk",
"d": [
"pw"
],
"e": "dsg"
,
"F":
"a": "ldjewiorf",
"ID": "01",
"c": "kjasnbgfk",
"d": "mbxzc" ,
"e": "dsg"
,
"F":
"f": "1232",
"g": "rege",
"h": "en-gb",
"i": "dfkj34",
"j": "iyt658"
],
"properties":
"AA":"esg",
"BB": "skdjghk",
"CC": "adfkh",
"DD": "sdlkfh"
,
"A": [
"abc",
"cde",
"efj"
],
"B": 16,
"C": 14,
"D": [
"F":
"a": "sdg",
"b": "sg",
"ID": "01",
"c": "sg",
"d": "shfh",
"e": "weitu"
,
"F":
"f": "1232",
"m": "sdg",
"n": "en-sdg",
"o": "eqe",
"p": "sdg"
],
"properties":
"AA":"ekjhsg",
"BB": "skkldjghk",
"CC": "adfyurkh",
"DD": "sdlkfmlh"
],
"G":
"A1":
"year": 2016,
"month": 5,
"dayOfMonth": 1,
"hourOfDay": 0,
"minute": 0,
"second": 0
,
"A2": "ksjdf",
"A3": "s38764",
"A4": [
"year": 2016,
"month": 5,
"dayOfMonth": 1,
"hourOfDay": 0,
"minute": 0,
"second": 0
]
I tried this code, but puts the whole file in 1 row with multiple columns:
def flatten_json(y):
out =
def flatten(x, name=''):
if type(x) is dict:
for a in x:
flatten(x[a], name + a + '_')
elif type(x) is list:
i = 0
for a in x:
flatten(a, name + str(i) + '_')
i += 1
else:
out[name[:-1]] = x
flatten(y)
return out
Is there a solution of what I am trying to do or is it impossible?
python json
1
Please post your desired output.
– Ajax1234
Nov 13 '18 at 18:13
add a comment |
I am working on a heavily nested json file that contains multiple dictionaries and lists. Some of the keys are similar and I am wondering if I can put each dictionary into different row. It doesn't matter, if some columns are not the same, the values can be left blank.
Json file (in similar form to the original)
"ID": "01",
"session": [
"A": [
"abc",
"cde",
"efj"
],
"B": 14,
"C": 14,
"D": [
"F":
"a": "ldjf",
"b": "kdj",
"ID": "01",
"c": "kjasgfk",
"d": [
"pw"
],
"e": "dsg"
,
"F":
"a": "ldjewiorf",
"ID": "01",
"c": "kjasnbgfk",
"d": "mbxzc" ,
"e": "dsg"
,
"F":
"f": "1232",
"g": "rege",
"h": "en-gb",
"i": "dfkj34",
"j": "iyt658"
],
"properties":
"AA":"esg",
"BB": "skdjghk",
"CC": "adfkh",
"DD": "sdlkfh"
,
"A": [
"abc",
"cde",
"efj"
],
"B": 16,
"C": 14,
"D": [
"F":
"a": "sdg",
"b": "sg",
"ID": "01",
"c": "sg",
"d": "shfh",
"e": "weitu"
,
"F":
"f": "1232",
"m": "sdg",
"n": "en-sdg",
"o": "eqe",
"p": "sdg"
],
"properties":
"AA":"ekjhsg",
"BB": "skkldjghk",
"CC": "adfyurkh",
"DD": "sdlkfmlh"
],
"G":
"A1":
"year": 2016,
"month": 5,
"dayOfMonth": 1,
"hourOfDay": 0,
"minute": 0,
"second": 0
,
"A2": "ksjdf",
"A3": "s38764",
"A4": [
"year": 2016,
"month": 5,
"dayOfMonth": 1,
"hourOfDay": 0,
"minute": 0,
"second": 0
]
I tried this code, but puts the whole file in 1 row with multiple columns:
def flatten_json(y):
out =
def flatten(x, name=''):
if type(x) is dict:
for a in x:
flatten(x[a], name + a + '_')
elif type(x) is list:
i = 0
for a in x:
flatten(a, name + str(i) + '_')
i += 1
else:
out[name[:-1]] = x
flatten(y)
return out
Is there a solution of what I am trying to do or is it impossible?
python json
I am working on a heavily nested json file that contains multiple dictionaries and lists. Some of the keys are similar and I am wondering if I can put each dictionary into different row. It doesn't matter, if some columns are not the same, the values can be left blank.
Json file (in similar form to the original)
"ID": "01",
"session": [
"A": [
"abc",
"cde",
"efj"
],
"B": 14,
"C": 14,
"D": [
"F":
"a": "ldjf",
"b": "kdj",
"ID": "01",
"c": "kjasgfk",
"d": [
"pw"
],
"e": "dsg"
,
"F":
"a": "ldjewiorf",
"ID": "01",
"c": "kjasnbgfk",
"d": "mbxzc" ,
"e": "dsg"
,
"F":
"f": "1232",
"g": "rege",
"h": "en-gb",
"i": "dfkj34",
"j": "iyt658"
],
"properties":
"AA":"esg",
"BB": "skdjghk",
"CC": "adfkh",
"DD": "sdlkfh"
,
"A": [
"abc",
"cde",
"efj"
],
"B": 16,
"C": 14,
"D": [
"F":
"a": "sdg",
"b": "sg",
"ID": "01",
"c": "sg",
"d": "shfh",
"e": "weitu"
,
"F":
"f": "1232",
"m": "sdg",
"n": "en-sdg",
"o": "eqe",
"p": "sdg"
],
"properties":
"AA":"ekjhsg",
"BB": "skkldjghk",
"CC": "adfyurkh",
"DD": "sdlkfmlh"
],
"G":
"A1":
"year": 2016,
"month": 5,
"dayOfMonth": 1,
"hourOfDay": 0,
"minute": 0,
"second": 0
,
"A2": "ksjdf",
"A3": "s38764",
"A4": [
"year": 2016,
"month": 5,
"dayOfMonth": 1,
"hourOfDay": 0,
"minute": 0,
"second": 0
]
I tried this code, but puts the whole file in 1 row with multiple columns:
def flatten_json(y):
out =
def flatten(x, name=''):
if type(x) is dict:
for a in x:
flatten(x[a], name + a + '_')
elif type(x) is list:
i = 0
for a in x:
flatten(a, name + str(i) + '_')
i += 1
else:
out[name[:-1]] = x
flatten(y)
return out
Is there a solution of what I am trying to do or is it impossible?
python json
python json
edited Nov 13 '18 at 18:16
user2722968
2,69911637
2,69911637
asked Nov 13 '18 at 18:11
tpklaratpklara
84
84
1
Please post your desired output.
– Ajax1234
Nov 13 '18 at 18:13
add a comment |
1
Please post your desired output.
– Ajax1234
Nov 13 '18 at 18:13
1
1
Please post your desired output.
– Ajax1234
Nov 13 '18 at 18:13
Please post your desired output.
– Ajax1234
Nov 13 '18 at 18:13
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53287123%2fflatten-nested-json-into-multiple-rows%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53287123%2fflatten-nested-json-into-multiple-rows%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Please post your desired output.
– Ajax1234
Nov 13 '18 at 18:13