How to Group Json data based on Key and combine values under in Python?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















Have a json that contains structure as shown below:
What I'm trying is to group and merge data based on 'name' , for example the two examples below have same name i.e 'abc', so they will get merged into a single collection and 'id' and 'nested prop' will be an array under the 'name' with two contents



 [{
"id" : [{"random" : "12345"}],
"name" : "abc",
"nestedprop" : {
"malfunc" : [
{
"Info" : {
"xyz" : [
{
"vamp" : "104531_0_46095",
"ramp" : {
"samp" : [
{
"int" : 532,
}
],
},
"class_unique_id" : "05451",
}
],
"nati" : 39237,
"apper" : 00,
"supp" : {
"sess" : ""
},
"session_id" : "42461920181213044516299872341"
}
},
{
"Info" : {
"xyz" : [
{
"vamp" : "104531_0_46095",
"ramp" : {
"samp" : [
{
"int" : 5325,
}
],
},
"class_unique_id" : "05451",
}
],
"nati" : 392537,
"apper" : 00,
"supp" : {
"sess" : ""
},
"session_id" : "42461920181213044516299872341"
}
},
]
},
},
{
"id" : [{"asdad" : "63653"}],
"name" : "abc",
"nestedprop" : {
"malfunc" : [
{
"Info" : {
"xyz" : [
{
"vamp" : "104531_0_46095",
"ramp" : {
"samp" : [
{
"int" : 532,
}
],
},
"class_unique_id" : "05451",
}
],
"nati" : 39237,
"apper" : 00,
"supp" : {
"sess" : ""
},
"session_id" : "42461920181213044516299872341"
}
},
{
"Info" : {
"xyz" : [
{
"vamp" : "104531_0_46095",
"ramp" : {
"samp" : [
{
"int" : 532,
}
],
},
"class_unique_id" : "05451",
}
],
"nati" : 39237,
"apper" : 00,
"supp" : {
"sess" : ""
},
"session_id" : "42461920181213044516299872341"
}
},
]
}
}]


Expected Result:



[{
"id" : {"0":[{"random" : "12345"}],"1":[{"random" : "12345"}]},
"name" : "abc",
"nestedprop" : {"0":[{"content from 1st obj"}],"1":[{"content from 1st obj"}]
},
{"id" : "0":[{"randsdaom" : "123sdas45"}]},
"name" : "def",
"nestedprop" : {"0":[{"content from 1st obj"}]}]


Note: I have achieved this using Mapreduce in Mongodb,however I wanted to try it out in Python










share|improve this question

























  • Your question statement is confusing. Mention your expected output.

    – Abdur Rehman
    Jan 3 at 10:08











  • I've already posted the expected result

    – Austin Joy
    Jan 3 at 10:09











  • Your expected output is not correct. "id":{[{"random" : "12345"}],[{"asdad" : "63653"}]}, this is dictionary without any key, value pair, so correct it.

    – Abdur Rehman
    Jan 3 at 10:20











  • Changed the structure of expected result

    – Austin Joy
    Jan 3 at 10:31











  • let me update my reply.

    – Abdur Rehman
    Jan 3 at 10:32


















0















Have a json that contains structure as shown below:
What I'm trying is to group and merge data based on 'name' , for example the two examples below have same name i.e 'abc', so they will get merged into a single collection and 'id' and 'nested prop' will be an array under the 'name' with two contents



 [{
"id" : [{"random" : "12345"}],
"name" : "abc",
"nestedprop" : {
"malfunc" : [
{
"Info" : {
"xyz" : [
{
"vamp" : "104531_0_46095",
"ramp" : {
"samp" : [
{
"int" : 532,
}
],
},
"class_unique_id" : "05451",
}
],
"nati" : 39237,
"apper" : 00,
"supp" : {
"sess" : ""
},
"session_id" : "42461920181213044516299872341"
}
},
{
"Info" : {
"xyz" : [
{
"vamp" : "104531_0_46095",
"ramp" : {
"samp" : [
{
"int" : 5325,
}
],
},
"class_unique_id" : "05451",
}
],
"nati" : 392537,
"apper" : 00,
"supp" : {
"sess" : ""
},
"session_id" : "42461920181213044516299872341"
}
},
]
},
},
{
"id" : [{"asdad" : "63653"}],
"name" : "abc",
"nestedprop" : {
"malfunc" : [
{
"Info" : {
"xyz" : [
{
"vamp" : "104531_0_46095",
"ramp" : {
"samp" : [
{
"int" : 532,
}
],
},
"class_unique_id" : "05451",
}
],
"nati" : 39237,
"apper" : 00,
"supp" : {
"sess" : ""
},
"session_id" : "42461920181213044516299872341"
}
},
{
"Info" : {
"xyz" : [
{
"vamp" : "104531_0_46095",
"ramp" : {
"samp" : [
{
"int" : 532,
}
],
},
"class_unique_id" : "05451",
}
],
"nati" : 39237,
"apper" : 00,
"supp" : {
"sess" : ""
},
"session_id" : "42461920181213044516299872341"
}
},
]
}
}]


Expected Result:



[{
"id" : {"0":[{"random" : "12345"}],"1":[{"random" : "12345"}]},
"name" : "abc",
"nestedprop" : {"0":[{"content from 1st obj"}],"1":[{"content from 1st obj"}]
},
{"id" : "0":[{"randsdaom" : "123sdas45"}]},
"name" : "def",
"nestedprop" : {"0":[{"content from 1st obj"}]}]


Note: I have achieved this using Mapreduce in Mongodb,however I wanted to try it out in Python










share|improve this question

























  • Your question statement is confusing. Mention your expected output.

    – Abdur Rehman
    Jan 3 at 10:08











  • I've already posted the expected result

    – Austin Joy
    Jan 3 at 10:09











  • Your expected output is not correct. "id":{[{"random" : "12345"}],[{"asdad" : "63653"}]}, this is dictionary without any key, value pair, so correct it.

    – Abdur Rehman
    Jan 3 at 10:20











  • Changed the structure of expected result

    – Austin Joy
    Jan 3 at 10:31











  • let me update my reply.

    – Abdur Rehman
    Jan 3 at 10:32














0












0








0


1






Have a json that contains structure as shown below:
What I'm trying is to group and merge data based on 'name' , for example the two examples below have same name i.e 'abc', so they will get merged into a single collection and 'id' and 'nested prop' will be an array under the 'name' with two contents



 [{
"id" : [{"random" : "12345"}],
"name" : "abc",
"nestedprop" : {
"malfunc" : [
{
"Info" : {
"xyz" : [
{
"vamp" : "104531_0_46095",
"ramp" : {
"samp" : [
{
"int" : 532,
}
],
},
"class_unique_id" : "05451",
}
],
"nati" : 39237,
"apper" : 00,
"supp" : {
"sess" : ""
},
"session_id" : "42461920181213044516299872341"
}
},
{
"Info" : {
"xyz" : [
{
"vamp" : "104531_0_46095",
"ramp" : {
"samp" : [
{
"int" : 5325,
}
],
},
"class_unique_id" : "05451",
}
],
"nati" : 392537,
"apper" : 00,
"supp" : {
"sess" : ""
},
"session_id" : "42461920181213044516299872341"
}
},
]
},
},
{
"id" : [{"asdad" : "63653"}],
"name" : "abc",
"nestedprop" : {
"malfunc" : [
{
"Info" : {
"xyz" : [
{
"vamp" : "104531_0_46095",
"ramp" : {
"samp" : [
{
"int" : 532,
}
],
},
"class_unique_id" : "05451",
}
],
"nati" : 39237,
"apper" : 00,
"supp" : {
"sess" : ""
},
"session_id" : "42461920181213044516299872341"
}
},
{
"Info" : {
"xyz" : [
{
"vamp" : "104531_0_46095",
"ramp" : {
"samp" : [
{
"int" : 532,
}
],
},
"class_unique_id" : "05451",
}
],
"nati" : 39237,
"apper" : 00,
"supp" : {
"sess" : ""
},
"session_id" : "42461920181213044516299872341"
}
},
]
}
}]


Expected Result:



[{
"id" : {"0":[{"random" : "12345"}],"1":[{"random" : "12345"}]},
"name" : "abc",
"nestedprop" : {"0":[{"content from 1st obj"}],"1":[{"content from 1st obj"}]
},
{"id" : "0":[{"randsdaom" : "123sdas45"}]},
"name" : "def",
"nestedprop" : {"0":[{"content from 1st obj"}]}]


Note: I have achieved this using Mapreduce in Mongodb,however I wanted to try it out in Python










share|improve this question
















Have a json that contains structure as shown below:
What I'm trying is to group and merge data based on 'name' , for example the two examples below have same name i.e 'abc', so they will get merged into a single collection and 'id' and 'nested prop' will be an array under the 'name' with two contents



 [{
"id" : [{"random" : "12345"}],
"name" : "abc",
"nestedprop" : {
"malfunc" : [
{
"Info" : {
"xyz" : [
{
"vamp" : "104531_0_46095",
"ramp" : {
"samp" : [
{
"int" : 532,
}
],
},
"class_unique_id" : "05451",
}
],
"nati" : 39237,
"apper" : 00,
"supp" : {
"sess" : ""
},
"session_id" : "42461920181213044516299872341"
}
},
{
"Info" : {
"xyz" : [
{
"vamp" : "104531_0_46095",
"ramp" : {
"samp" : [
{
"int" : 5325,
}
],
},
"class_unique_id" : "05451",
}
],
"nati" : 392537,
"apper" : 00,
"supp" : {
"sess" : ""
},
"session_id" : "42461920181213044516299872341"
}
},
]
},
},
{
"id" : [{"asdad" : "63653"}],
"name" : "abc",
"nestedprop" : {
"malfunc" : [
{
"Info" : {
"xyz" : [
{
"vamp" : "104531_0_46095",
"ramp" : {
"samp" : [
{
"int" : 532,
}
],
},
"class_unique_id" : "05451",
}
],
"nati" : 39237,
"apper" : 00,
"supp" : {
"sess" : ""
},
"session_id" : "42461920181213044516299872341"
}
},
{
"Info" : {
"xyz" : [
{
"vamp" : "104531_0_46095",
"ramp" : {
"samp" : [
{
"int" : 532,
}
],
},
"class_unique_id" : "05451",
}
],
"nati" : 39237,
"apper" : 00,
"supp" : {
"sess" : ""
},
"session_id" : "42461920181213044516299872341"
}
},
]
}
}]


Expected Result:



[{
"id" : {"0":[{"random" : "12345"}],"1":[{"random" : "12345"}]},
"name" : "abc",
"nestedprop" : {"0":[{"content from 1st obj"}],"1":[{"content from 1st obj"}]
},
{"id" : "0":[{"randsdaom" : "123sdas45"}]},
"name" : "def",
"nestedprop" : {"0":[{"content from 1st obj"}]}]


Note: I have achieved this using Mapreduce in Mongodb,however I wanted to try it out in Python







python json






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 4 at 6:56







Austin Joy

















asked Jan 3 at 9:50









Austin JoyAustin Joy

187




187













  • Your question statement is confusing. Mention your expected output.

    – Abdur Rehman
    Jan 3 at 10:08











  • I've already posted the expected result

    – Austin Joy
    Jan 3 at 10:09











  • Your expected output is not correct. "id":{[{"random" : "12345"}],[{"asdad" : "63653"}]}, this is dictionary without any key, value pair, so correct it.

    – Abdur Rehman
    Jan 3 at 10:20











  • Changed the structure of expected result

    – Austin Joy
    Jan 3 at 10:31











  • let me update my reply.

    – Abdur Rehman
    Jan 3 at 10:32



















  • Your question statement is confusing. Mention your expected output.

    – Abdur Rehman
    Jan 3 at 10:08











  • I've already posted the expected result

    – Austin Joy
    Jan 3 at 10:09











  • Your expected output is not correct. "id":{[{"random" : "12345"}],[{"asdad" : "63653"}]}, this is dictionary without any key, value pair, so correct it.

    – Abdur Rehman
    Jan 3 at 10:20











  • Changed the structure of expected result

    – Austin Joy
    Jan 3 at 10:31











  • let me update my reply.

    – Abdur Rehman
    Jan 3 at 10:32

















Your question statement is confusing. Mention your expected output.

– Abdur Rehman
Jan 3 at 10:08





Your question statement is confusing. Mention your expected output.

– Abdur Rehman
Jan 3 at 10:08













I've already posted the expected result

– Austin Joy
Jan 3 at 10:09





I've already posted the expected result

– Austin Joy
Jan 3 at 10:09













Your expected output is not correct. "id":{[{"random" : "12345"}],[{"asdad" : "63653"}]}, this is dictionary without any key, value pair, so correct it.

– Abdur Rehman
Jan 3 at 10:20





Your expected output is not correct. "id":{[{"random" : "12345"}],[{"asdad" : "63653"}]}, this is dictionary without any key, value pair, so correct it.

– Abdur Rehman
Jan 3 at 10:20













Changed the structure of expected result

– Austin Joy
Jan 3 at 10:31





Changed the structure of expected result

– Austin Joy
Jan 3 at 10:31













let me update my reply.

– Abdur Rehman
Jan 3 at 10:32





let me update my reply.

– Abdur Rehman
Jan 3 at 10:32












1 Answer
1






active

oldest

votes


















1














There could be many other approaches but this one would give you desired results,



names = 
for dic in dictList:
names.append(dic['name'])

# unique list of names
names = list(set(names))


results =

for name in names:
idx = 0
ids = {}
props = {}
names =
for dic in dictList:
dic_name = dic['name']
if dic_name == name:
ids[str(idx)] = dic['id']
props[str(idx)] = dic['nestedprop']
idx += 1
result_dict = {"name": name,
"id": ids,
"nestedprop": props}

results.append(result_dict)

# result = json.dumps(result) to convert back into double quotes ""
results


Output:



   [{'name': 'abc',
'id': {'0': [{'random': '12345'}],
'1': [{'asdad': '63653'}],
'2': [{'asdad': '63653'}]},
'nestedprop': {'0': {'malfunc': [{'Info': {'xyz': [{'vamp': '104531_0_46095',
'ramp': {'samp': [{'int': 532}]},
'class_unique_id': '05451'}],
'nati': 39237,
'apper': 0,
'supp': {'sess': ''},
'session_id': '42461920181213044516299872341'}},
{'Info': {'xyz': [{'vamp': '104531_0_46095',
'ramp': {'samp': [{'int': 5325}]},
'class_unique_id': '05451'}],
'nati': 392537,
'apper': 0,
'supp': {'sess': ''},
'session_id': '42461920181213044516299872341'}}]},
'1': {'malfunc': [{'Info': {'xyz': [{'vamp': '104531_0_46095',
'ramp': {'samp': [{'int': 532}]},
'class_unique_id': '05451'}],
'nati': 39237,
'apper': 0,
'supp': {'sess': ''},
'session_id': '42461920181213044516299872341'}},
{'Info': {'xyz': [{'vamp': '104531_0_46095',
'ramp': {'samp': [{'int': 532}]},
'class_unique_id': '05451'}],
'nati': 39237,
'apper': 0,
'supp': {'sess': ''},
'session_id': '42461920181213044516299872341'}}]},
'2': {'malfunc': [{'Info': {'xyz': [{'vamp': '104531_0_46095',
'ramp': {'samp': [{'int': 532}]},
'class_unique_id': '05451'}],
'nati': 39237,
'apper': 0,
'supp': {'sess': ''},
'session_id': '42461920181213044516299872341'}},
{'Info': {'xyz': [{'vamp': '104531_0_46095',
'ramp': {'samp': [{'int': 532}]},
'class_unique_id': '05451'}],
'nati': 39237,
'apper': 0,
'supp': {'sess': ''},
'session_id': '42461920181213044516299872341'}}]}}},
{'name': 'def',
'id': {'0': [{'asdad': '63653'}], '1': [{'asdad': '63653'}]},
'nestedprop': {'0': {'malfunc': [{'Info': {'xyz': [{'vamp': '104531_0_46095',
'ramp': {'samp': [{'int': 532}]},
'class_unique_id': '05451'}],
'nati': 39237,
'apper': 0,
'supp': {'sess': ''},
'session_id': '42461920181213044516299872341'}},
{'Info': {'xyz': [{'vamp': '104531_0_46095',
'ramp': {'samp': [{'int': 532}]},
'class_unique_id': '05451'}],
'nati': 39237,
'apper': 0,
'supp': {'sess': ''},
'session_id': '42461920181213044516299872341'}}]},
'1': {'malfunc': [{'Info': {'xyz': [{'vamp': '104531_0_46095',
'ramp': {'samp': [{'int': 532}]},
'class_unique_id': '05451'}],
'nati': 39237,
'apper': 0,
'supp': {'sess': ''},
'session_id': '42461920181213044516299872341'}},
{'Info': {'xyz': [{'vamp': '104531_0_46095',
'ramp': {'samp': [{'int': 532}]},
'class_unique_id': '05451'}],
'nati': 39237,
'apper': 0,
'supp': {'sess': ''},
'session_id': '42461920181213044516299872341'}}]}}}]





share|improve this answer


























  • Thanks ,din't know it would be that easy

    – Austin Joy
    Jan 3 at 11:08











  • @AustinJoy please up-vote this answer.

    – Abdur Rehman
    Jan 3 at 11:09











  • This doesnt work if there are different value in 'names'.

    – Austin Joy
    Jan 4 at 6:51











  • @AustinJoy let me check.

    – Abdur Rehman
    Jan 4 at 6:52











  • kindly check the updated output.

    – Austin Joy
    Jan 4 at 6:57












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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54019822%2fhow-to-group-json-data-based-on-key-and-combine-values-under-in-python%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














There could be many other approaches but this one would give you desired results,



names = 
for dic in dictList:
names.append(dic['name'])

# unique list of names
names = list(set(names))


results =

for name in names:
idx = 0
ids = {}
props = {}
names =
for dic in dictList:
dic_name = dic['name']
if dic_name == name:
ids[str(idx)] = dic['id']
props[str(idx)] = dic['nestedprop']
idx += 1
result_dict = {"name": name,
"id": ids,
"nestedprop": props}

results.append(result_dict)

# result = json.dumps(result) to convert back into double quotes ""
results


Output:



   [{'name': 'abc',
'id': {'0': [{'random': '12345'}],
'1': [{'asdad': '63653'}],
'2': [{'asdad': '63653'}]},
'nestedprop': {'0': {'malfunc': [{'Info': {'xyz': [{'vamp': '104531_0_46095',
'ramp': {'samp': [{'int': 532}]},
'class_unique_id': '05451'}],
'nati': 39237,
'apper': 0,
'supp': {'sess': ''},
'session_id': '42461920181213044516299872341'}},
{'Info': {'xyz': [{'vamp': '104531_0_46095',
'ramp': {'samp': [{'int': 5325}]},
'class_unique_id': '05451'}],
'nati': 392537,
'apper': 0,
'supp': {'sess': ''},
'session_id': '42461920181213044516299872341'}}]},
'1': {'malfunc': [{'Info': {'xyz': [{'vamp': '104531_0_46095',
'ramp': {'samp': [{'int': 532}]},
'class_unique_id': '05451'}],
'nati': 39237,
'apper': 0,
'supp': {'sess': ''},
'session_id': '42461920181213044516299872341'}},
{'Info': {'xyz': [{'vamp': '104531_0_46095',
'ramp': {'samp': [{'int': 532}]},
'class_unique_id': '05451'}],
'nati': 39237,
'apper': 0,
'supp': {'sess': ''},
'session_id': '42461920181213044516299872341'}}]},
'2': {'malfunc': [{'Info': {'xyz': [{'vamp': '104531_0_46095',
'ramp': {'samp': [{'int': 532}]},
'class_unique_id': '05451'}],
'nati': 39237,
'apper': 0,
'supp': {'sess': ''},
'session_id': '42461920181213044516299872341'}},
{'Info': {'xyz': [{'vamp': '104531_0_46095',
'ramp': {'samp': [{'int': 532}]},
'class_unique_id': '05451'}],
'nati': 39237,
'apper': 0,
'supp': {'sess': ''},
'session_id': '42461920181213044516299872341'}}]}}},
{'name': 'def',
'id': {'0': [{'asdad': '63653'}], '1': [{'asdad': '63653'}]},
'nestedprop': {'0': {'malfunc': [{'Info': {'xyz': [{'vamp': '104531_0_46095',
'ramp': {'samp': [{'int': 532}]},
'class_unique_id': '05451'}],
'nati': 39237,
'apper': 0,
'supp': {'sess': ''},
'session_id': '42461920181213044516299872341'}},
{'Info': {'xyz': [{'vamp': '104531_0_46095',
'ramp': {'samp': [{'int': 532}]},
'class_unique_id': '05451'}],
'nati': 39237,
'apper': 0,
'supp': {'sess': ''},
'session_id': '42461920181213044516299872341'}}]},
'1': {'malfunc': [{'Info': {'xyz': [{'vamp': '104531_0_46095',
'ramp': {'samp': [{'int': 532}]},
'class_unique_id': '05451'}],
'nati': 39237,
'apper': 0,
'supp': {'sess': ''},
'session_id': '42461920181213044516299872341'}},
{'Info': {'xyz': [{'vamp': '104531_0_46095',
'ramp': {'samp': [{'int': 532}]},
'class_unique_id': '05451'}],
'nati': 39237,
'apper': 0,
'supp': {'sess': ''},
'session_id': '42461920181213044516299872341'}}]}}}]





share|improve this answer


























  • Thanks ,din't know it would be that easy

    – Austin Joy
    Jan 3 at 11:08











  • @AustinJoy please up-vote this answer.

    – Abdur Rehman
    Jan 3 at 11:09











  • This doesnt work if there are different value in 'names'.

    – Austin Joy
    Jan 4 at 6:51











  • @AustinJoy let me check.

    – Abdur Rehman
    Jan 4 at 6:52











  • kindly check the updated output.

    – Austin Joy
    Jan 4 at 6:57
















1














There could be many other approaches but this one would give you desired results,



names = 
for dic in dictList:
names.append(dic['name'])

# unique list of names
names = list(set(names))


results =

for name in names:
idx = 0
ids = {}
props = {}
names =
for dic in dictList:
dic_name = dic['name']
if dic_name == name:
ids[str(idx)] = dic['id']
props[str(idx)] = dic['nestedprop']
idx += 1
result_dict = {"name": name,
"id": ids,
"nestedprop": props}

results.append(result_dict)

# result = json.dumps(result) to convert back into double quotes ""
results


Output:



   [{'name': 'abc',
'id': {'0': [{'random': '12345'}],
'1': [{'asdad': '63653'}],
'2': [{'asdad': '63653'}]},
'nestedprop': {'0': {'malfunc': [{'Info': {'xyz': [{'vamp': '104531_0_46095',
'ramp': {'samp': [{'int': 532}]},
'class_unique_id': '05451'}],
'nati': 39237,
'apper': 0,
'supp': {'sess': ''},
'session_id': '42461920181213044516299872341'}},
{'Info': {'xyz': [{'vamp': '104531_0_46095',
'ramp': {'samp': [{'int': 5325}]},
'class_unique_id': '05451'}],
'nati': 392537,
'apper': 0,
'supp': {'sess': ''},
'session_id': '42461920181213044516299872341'}}]},
'1': {'malfunc': [{'Info': {'xyz': [{'vamp': '104531_0_46095',
'ramp': {'samp': [{'int': 532}]},
'class_unique_id': '05451'}],
'nati': 39237,
'apper': 0,
'supp': {'sess': ''},
'session_id': '42461920181213044516299872341'}},
{'Info': {'xyz': [{'vamp': '104531_0_46095',
'ramp': {'samp': [{'int': 532}]},
'class_unique_id': '05451'}],
'nati': 39237,
'apper': 0,
'supp': {'sess': ''},
'session_id': '42461920181213044516299872341'}}]},
'2': {'malfunc': [{'Info': {'xyz': [{'vamp': '104531_0_46095',
'ramp': {'samp': [{'int': 532}]},
'class_unique_id': '05451'}],
'nati': 39237,
'apper': 0,
'supp': {'sess': ''},
'session_id': '42461920181213044516299872341'}},
{'Info': {'xyz': [{'vamp': '104531_0_46095',
'ramp': {'samp': [{'int': 532}]},
'class_unique_id': '05451'}],
'nati': 39237,
'apper': 0,
'supp': {'sess': ''},
'session_id': '42461920181213044516299872341'}}]}}},
{'name': 'def',
'id': {'0': [{'asdad': '63653'}], '1': [{'asdad': '63653'}]},
'nestedprop': {'0': {'malfunc': [{'Info': {'xyz': [{'vamp': '104531_0_46095',
'ramp': {'samp': [{'int': 532}]},
'class_unique_id': '05451'}],
'nati': 39237,
'apper': 0,
'supp': {'sess': ''},
'session_id': '42461920181213044516299872341'}},
{'Info': {'xyz': [{'vamp': '104531_0_46095',
'ramp': {'samp': [{'int': 532}]},
'class_unique_id': '05451'}],
'nati': 39237,
'apper': 0,
'supp': {'sess': ''},
'session_id': '42461920181213044516299872341'}}]},
'1': {'malfunc': [{'Info': {'xyz': [{'vamp': '104531_0_46095',
'ramp': {'samp': [{'int': 532}]},
'class_unique_id': '05451'}],
'nati': 39237,
'apper': 0,
'supp': {'sess': ''},
'session_id': '42461920181213044516299872341'}},
{'Info': {'xyz': [{'vamp': '104531_0_46095',
'ramp': {'samp': [{'int': 532}]},
'class_unique_id': '05451'}],
'nati': 39237,
'apper': 0,
'supp': {'sess': ''},
'session_id': '42461920181213044516299872341'}}]}}}]





share|improve this answer


























  • Thanks ,din't know it would be that easy

    – Austin Joy
    Jan 3 at 11:08











  • @AustinJoy please up-vote this answer.

    – Abdur Rehman
    Jan 3 at 11:09











  • This doesnt work if there are different value in 'names'.

    – Austin Joy
    Jan 4 at 6:51











  • @AustinJoy let me check.

    – Abdur Rehman
    Jan 4 at 6:52











  • kindly check the updated output.

    – Austin Joy
    Jan 4 at 6:57














1












1








1







There could be many other approaches but this one would give you desired results,



names = 
for dic in dictList:
names.append(dic['name'])

# unique list of names
names = list(set(names))


results =

for name in names:
idx = 0
ids = {}
props = {}
names =
for dic in dictList:
dic_name = dic['name']
if dic_name == name:
ids[str(idx)] = dic['id']
props[str(idx)] = dic['nestedprop']
idx += 1
result_dict = {"name": name,
"id": ids,
"nestedprop": props}

results.append(result_dict)

# result = json.dumps(result) to convert back into double quotes ""
results


Output:



   [{'name': 'abc',
'id': {'0': [{'random': '12345'}],
'1': [{'asdad': '63653'}],
'2': [{'asdad': '63653'}]},
'nestedprop': {'0': {'malfunc': [{'Info': {'xyz': [{'vamp': '104531_0_46095',
'ramp': {'samp': [{'int': 532}]},
'class_unique_id': '05451'}],
'nati': 39237,
'apper': 0,
'supp': {'sess': ''},
'session_id': '42461920181213044516299872341'}},
{'Info': {'xyz': [{'vamp': '104531_0_46095',
'ramp': {'samp': [{'int': 5325}]},
'class_unique_id': '05451'}],
'nati': 392537,
'apper': 0,
'supp': {'sess': ''},
'session_id': '42461920181213044516299872341'}}]},
'1': {'malfunc': [{'Info': {'xyz': [{'vamp': '104531_0_46095',
'ramp': {'samp': [{'int': 532}]},
'class_unique_id': '05451'}],
'nati': 39237,
'apper': 0,
'supp': {'sess': ''},
'session_id': '42461920181213044516299872341'}},
{'Info': {'xyz': [{'vamp': '104531_0_46095',
'ramp': {'samp': [{'int': 532}]},
'class_unique_id': '05451'}],
'nati': 39237,
'apper': 0,
'supp': {'sess': ''},
'session_id': '42461920181213044516299872341'}}]},
'2': {'malfunc': [{'Info': {'xyz': [{'vamp': '104531_0_46095',
'ramp': {'samp': [{'int': 532}]},
'class_unique_id': '05451'}],
'nati': 39237,
'apper': 0,
'supp': {'sess': ''},
'session_id': '42461920181213044516299872341'}},
{'Info': {'xyz': [{'vamp': '104531_0_46095',
'ramp': {'samp': [{'int': 532}]},
'class_unique_id': '05451'}],
'nati': 39237,
'apper': 0,
'supp': {'sess': ''},
'session_id': '42461920181213044516299872341'}}]}}},
{'name': 'def',
'id': {'0': [{'asdad': '63653'}], '1': [{'asdad': '63653'}]},
'nestedprop': {'0': {'malfunc': [{'Info': {'xyz': [{'vamp': '104531_0_46095',
'ramp': {'samp': [{'int': 532}]},
'class_unique_id': '05451'}],
'nati': 39237,
'apper': 0,
'supp': {'sess': ''},
'session_id': '42461920181213044516299872341'}},
{'Info': {'xyz': [{'vamp': '104531_0_46095',
'ramp': {'samp': [{'int': 532}]},
'class_unique_id': '05451'}],
'nati': 39237,
'apper': 0,
'supp': {'sess': ''},
'session_id': '42461920181213044516299872341'}}]},
'1': {'malfunc': [{'Info': {'xyz': [{'vamp': '104531_0_46095',
'ramp': {'samp': [{'int': 532}]},
'class_unique_id': '05451'}],
'nati': 39237,
'apper': 0,
'supp': {'sess': ''},
'session_id': '42461920181213044516299872341'}},
{'Info': {'xyz': [{'vamp': '104531_0_46095',
'ramp': {'samp': [{'int': 532}]},
'class_unique_id': '05451'}],
'nati': 39237,
'apper': 0,
'supp': {'sess': ''},
'session_id': '42461920181213044516299872341'}}]}}}]





share|improve this answer















There could be many other approaches but this one would give you desired results,



names = 
for dic in dictList:
names.append(dic['name'])

# unique list of names
names = list(set(names))


results =

for name in names:
idx = 0
ids = {}
props = {}
names =
for dic in dictList:
dic_name = dic['name']
if dic_name == name:
ids[str(idx)] = dic['id']
props[str(idx)] = dic['nestedprop']
idx += 1
result_dict = {"name": name,
"id": ids,
"nestedprop": props}

results.append(result_dict)

# result = json.dumps(result) to convert back into double quotes ""
results


Output:



   [{'name': 'abc',
'id': {'0': [{'random': '12345'}],
'1': [{'asdad': '63653'}],
'2': [{'asdad': '63653'}]},
'nestedprop': {'0': {'malfunc': [{'Info': {'xyz': [{'vamp': '104531_0_46095',
'ramp': {'samp': [{'int': 532}]},
'class_unique_id': '05451'}],
'nati': 39237,
'apper': 0,
'supp': {'sess': ''},
'session_id': '42461920181213044516299872341'}},
{'Info': {'xyz': [{'vamp': '104531_0_46095',
'ramp': {'samp': [{'int': 5325}]},
'class_unique_id': '05451'}],
'nati': 392537,
'apper': 0,
'supp': {'sess': ''},
'session_id': '42461920181213044516299872341'}}]},
'1': {'malfunc': [{'Info': {'xyz': [{'vamp': '104531_0_46095',
'ramp': {'samp': [{'int': 532}]},
'class_unique_id': '05451'}],
'nati': 39237,
'apper': 0,
'supp': {'sess': ''},
'session_id': '42461920181213044516299872341'}},
{'Info': {'xyz': [{'vamp': '104531_0_46095',
'ramp': {'samp': [{'int': 532}]},
'class_unique_id': '05451'}],
'nati': 39237,
'apper': 0,
'supp': {'sess': ''},
'session_id': '42461920181213044516299872341'}}]},
'2': {'malfunc': [{'Info': {'xyz': [{'vamp': '104531_0_46095',
'ramp': {'samp': [{'int': 532}]},
'class_unique_id': '05451'}],
'nati': 39237,
'apper': 0,
'supp': {'sess': ''},
'session_id': '42461920181213044516299872341'}},
{'Info': {'xyz': [{'vamp': '104531_0_46095',
'ramp': {'samp': [{'int': 532}]},
'class_unique_id': '05451'}],
'nati': 39237,
'apper': 0,
'supp': {'sess': ''},
'session_id': '42461920181213044516299872341'}}]}}},
{'name': 'def',
'id': {'0': [{'asdad': '63653'}], '1': [{'asdad': '63653'}]},
'nestedprop': {'0': {'malfunc': [{'Info': {'xyz': [{'vamp': '104531_0_46095',
'ramp': {'samp': [{'int': 532}]},
'class_unique_id': '05451'}],
'nati': 39237,
'apper': 0,
'supp': {'sess': ''},
'session_id': '42461920181213044516299872341'}},
{'Info': {'xyz': [{'vamp': '104531_0_46095',
'ramp': {'samp': [{'int': 532}]},
'class_unique_id': '05451'}],
'nati': 39237,
'apper': 0,
'supp': {'sess': ''},
'session_id': '42461920181213044516299872341'}}]},
'1': {'malfunc': [{'Info': {'xyz': [{'vamp': '104531_0_46095',
'ramp': {'samp': [{'int': 532}]},
'class_unique_id': '05451'}],
'nati': 39237,
'apper': 0,
'supp': {'sess': ''},
'session_id': '42461920181213044516299872341'}},
{'Info': {'xyz': [{'vamp': '104531_0_46095',
'ramp': {'samp': [{'int': 532}]},
'class_unique_id': '05451'}],
'nati': 39237,
'apper': 0,
'supp': {'sess': ''},
'session_id': '42461920181213044516299872341'}}]}}}]






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 4 at 7:21

























answered Jan 3 at 10:29









Abdur RehmanAbdur Rehman

626511




626511













  • Thanks ,din't know it would be that easy

    – Austin Joy
    Jan 3 at 11:08











  • @AustinJoy please up-vote this answer.

    – Abdur Rehman
    Jan 3 at 11:09











  • This doesnt work if there are different value in 'names'.

    – Austin Joy
    Jan 4 at 6:51











  • @AustinJoy let me check.

    – Abdur Rehman
    Jan 4 at 6:52











  • kindly check the updated output.

    – Austin Joy
    Jan 4 at 6:57



















  • Thanks ,din't know it would be that easy

    – Austin Joy
    Jan 3 at 11:08











  • @AustinJoy please up-vote this answer.

    – Abdur Rehman
    Jan 3 at 11:09











  • This doesnt work if there are different value in 'names'.

    – Austin Joy
    Jan 4 at 6:51











  • @AustinJoy let me check.

    – Abdur Rehman
    Jan 4 at 6:52











  • kindly check the updated output.

    – Austin Joy
    Jan 4 at 6:57

















Thanks ,din't know it would be that easy

– Austin Joy
Jan 3 at 11:08





Thanks ,din't know it would be that easy

– Austin Joy
Jan 3 at 11:08













@AustinJoy please up-vote this answer.

– Abdur Rehman
Jan 3 at 11:09





@AustinJoy please up-vote this answer.

– Abdur Rehman
Jan 3 at 11:09













This doesnt work if there are different value in 'names'.

– Austin Joy
Jan 4 at 6:51





This doesnt work if there are different value in 'names'.

– Austin Joy
Jan 4 at 6:51













@AustinJoy let me check.

– Abdur Rehman
Jan 4 at 6:52





@AustinJoy let me check.

– Abdur Rehman
Jan 4 at 6:52













kindly check the updated output.

– Austin Joy
Jan 4 at 6:57





kindly check the updated output.

– Austin Joy
Jan 4 at 6:57




















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54019822%2fhow-to-group-json-data-based-on-key-and-combine-values-under-in-python%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

'app-layout' is not a known element: how to share Component with different Modules

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

WPF add header to Image with URL pettitions [duplicate]