Normalize JSON for Single Level Children

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
0
down vote

favorite












The goal here is to normalize or flatten JSON:



[
"parent_key":
"child_key_1": "child_value_1",
"child_key_2": "child_value_2"

]


To this in a dataframe:



parent_key_child_key_1: child_value_1

parent_key_child_key_2: child_value_2


Children that are lists should be passed as lists.



Children that are dicts should be passed without modification.



This is my code right now:



from pandas.io.json import json_normalize

with open(INPUT_FILE_NAME) as f:
raw_data = json.load(f)

data = json_normalize(raw_data, sep='_')

# Reinsert list fields as lists
for key, value in raw_data.items():
if type(value) == list:
data[key] = str(raw_data[key])


I then ignore the columns that pandas created from lists. Pandas flattens lists to parent_key_index_number_child_key



Is there a better way to do this?







share|improve this question



















  • jq . file.json see: man jq
    – Martin York
    May 10 at 21:10
















up vote
0
down vote

favorite












The goal here is to normalize or flatten JSON:



[
"parent_key":
"child_key_1": "child_value_1",
"child_key_2": "child_value_2"

]


To this in a dataframe:



parent_key_child_key_1: child_value_1

parent_key_child_key_2: child_value_2


Children that are lists should be passed as lists.



Children that are dicts should be passed without modification.



This is my code right now:



from pandas.io.json import json_normalize

with open(INPUT_FILE_NAME) as f:
raw_data = json.load(f)

data = json_normalize(raw_data, sep='_')

# Reinsert list fields as lists
for key, value in raw_data.items():
if type(value) == list:
data[key] = str(raw_data[key])


I then ignore the columns that pandas created from lists. Pandas flattens lists to parent_key_index_number_child_key



Is there a better way to do this?







share|improve this question



















  • jq . file.json see: man jq
    – Martin York
    May 10 at 21:10












up vote
0
down vote

favorite









up vote
0
down vote

favorite











The goal here is to normalize or flatten JSON:



[
"parent_key":
"child_key_1": "child_value_1",
"child_key_2": "child_value_2"

]


To this in a dataframe:



parent_key_child_key_1: child_value_1

parent_key_child_key_2: child_value_2


Children that are lists should be passed as lists.



Children that are dicts should be passed without modification.



This is my code right now:



from pandas.io.json import json_normalize

with open(INPUT_FILE_NAME) as f:
raw_data = json.load(f)

data = json_normalize(raw_data, sep='_')

# Reinsert list fields as lists
for key, value in raw_data.items():
if type(value) == list:
data[key] = str(raw_data[key])


I then ignore the columns that pandas created from lists. Pandas flattens lists to parent_key_index_number_child_key



Is there a better way to do this?







share|improve this question











The goal here is to normalize or flatten JSON:



[
"parent_key":
"child_key_1": "child_value_1",
"child_key_2": "child_value_2"

]


To this in a dataframe:



parent_key_child_key_1: child_value_1

parent_key_child_key_2: child_value_2


Children that are lists should be passed as lists.



Children that are dicts should be passed without modification.



This is my code right now:



from pandas.io.json import json_normalize

with open(INPUT_FILE_NAME) as f:
raw_data = json.load(f)

data = json_normalize(raw_data, sep='_')

# Reinsert list fields as lists
for key, value in raw_data.items():
if type(value) == list:
data[key] = str(raw_data[key])


I then ignore the columns that pandas created from lists. Pandas flattens lists to parent_key_index_number_child_key



Is there a better way to do this?









share|improve this question










share|improve this question




share|improve this question









asked May 10 at 20:45









ProGirlXOXO

1091




1091











  • jq . file.json see: man jq
    – Martin York
    May 10 at 21:10
















  • jq . file.json see: man jq
    – Martin York
    May 10 at 21:10















jq . file.json see: man jq
– Martin York
May 10 at 21:10




jq . file.json see: man jq
– Martin York
May 10 at 21:10















active

oldest

votes











Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

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: "196"
;
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',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194143%2fnormalize-json-for-single-level-children%23new-answer', 'question_page');

);

Post as a guest



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes










 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194143%2fnormalize-json-for-single-level-children%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Chat program with C++ and SFML

Function to Return a JSON Like Objects Using VBA Collections and Arrays

Will my employers contract hold up in court?