I believe that you need to pass the key of the nested array together with the non-nested ones.
a=json_normalize(data,'digitalAssetFoodservice',['id','extendedDescriptionFrench','brand','productLife'])
print(a)
print(a.columns)
Out:
digitalAssetFormatFoodservice digitalAssetGDTIFoodservice digitalAssetImageVersionDateTimeFoodservice ... extendedDescriptionFrench brand productLife
0 JPG 754000000016500000000002167445 2016-06-28T20:06:06.000-04:00 ... Fromage Brick Petit Gaspesien PETIT GASPESIEN 90
1 JPG 754000000016500000000002167597 2016-06-28T20:06:06.000-04:00 ... Fromage Brick Petit Gaspesien PETIT GASPESIEN 90
2 JPG 754000000016500000000002167687 2016-06-28T20:06:06.000-04:00 ... Fromage Brick Petit Gaspesien PETIT GASPESIEN 90
[3 rows x 9 columns]
Index(['digitalAssetFormatFoodservice', 'digitalAssetGDTIFoodservice',
'digitalAssetImageVersionDateTimeFoodservice',
'digitalAssetStateFoodservice', 'digitalAssetImageTypeFoodservice',
'id', 'extendedDescriptionFrench', 'brand', 'productLife'],
dtype='object')
When trying for two cases:
This is what I came up with given I couldn't not find a way to generate in one line your expected output. I also changed the script a bit to make it easier. (Based on: "Question: What is your expected output? Everyting duplicated, once for name: x and one for name: y ? OP: yes , duplicated"
from pandas.io.json import json_normalize
data ={
"id":"001",
"counties" : [ {"name":"y"},{"name":"X"}],
"eDF" : "Fromage Brick Petit Gaspesien",
"brand" : "PETIT GASPESIEN",
"brandFrench" : "PETIT GASPESIEN",
"productLife" : "90",
"dAF" : [ {
"dAFF" : "JPG",
"dAGDTIF" : "75401652167445",
"dAIVDTF" : "2016-06-28",
"dASF" : "P"
}, {
"dAFF" : "JPG",
"dAGDTIF" : "75401652167597",
"dAITFa" : "M",
"dAIVDTF" : "2016-06-28"
}, {
"dAFF" : "JPG",
"dAGDTIF" : "7540162167687",
"dAITF" : "C",
"dAIVDTF" : "2016-06-28",
"dASF" : "C"
} ]
}
repetitive = ['id','eDF','brand','brandFrench','productLife']
a=json_normalize(data,'counties',repetitive)
b=json_normalize(data,'dAF',repetitive)
c = a.merge(b,how='inner',left_on=repetitive,right_on=repetitive)
print(a)
Output:
name id eDF brand brandFrench productLife
0 y 001 Fromage Brick Petit Gaspesien PETIT GASPESIEN PETIT GASPESIEN 90
1 X 001 Fromage Brick Petit Gaspesien PETIT GASPESIEN PETIT GASPESIEN 90
Now the other b
:
print(b)
Output:
dAFF dAGDTIF dAIVDTF dASF dAITFa dAITF id eDF brand brandFrench productLife
0 JPG 75401652167445 2016-06-28 P NaN NaN 001 Fromage Brick Petit Gaspesien PETIT GASPESIEN PETIT GASPESIEN 90
1 JPG 75401652167597 2016-06-28 NaN M NaN 001 Fromage Brick Petit Gaspesien PETIT GASPESIEN PETIT GASPESIEN 90
2 JPG 7540162167687 2016-06-28 C NaN C 001 Fromage Brick Petit Gaspesien PETIT GASPESIEN PETIT GASPESIEN 90
Finally c
:
print(c)
Output:
name id eDF brand brandFrench productLife dAFF dAGDTIF dAIVDTF dASF dAITFa dAITF
0 y 001 Fromage Brick Petit Gaspesien PETIT GASPESIEN PETIT GASPESIEN 90 JPG 75401652167445 2016-06-28 P NaN NaN
1 y 001 Fromage Brick Petit Gaspesien PETIT GASPESIEN PETIT GASPESIEN 90 JPG 75401652167597 2016-06-28 NaN M NaN
2 y 001 Fromage Brick Petit Gaspesien PETIT GASPESIEN PETIT GASPESIEN 90 JPG 7540162167687 2016-06-28 C NaN C
3 X 001 Fromage Brick Petit Gaspesien PETIT GASPESIEN PETIT GASPESIEN 90 JPG 75401652167445 2016-06-28 P NaN NaN
4 X 001 Fromage Brick Petit Gaspesien PETIT GASPESIEN PETIT GASPESIEN 90 JPG 75401652167597 2016-06-28 NaN M NaN
5 X 001 Fromage Brick Petit Gaspesien PETIT GASPESIEN PETIT GASPESIEN 90 JPG 7540162167687 2016-06-28 C NaN C