How to get individual dict from a nested dict? - json

Having following json file
{
"front_page": {
"institue": {
"inst_name": "University Name",
"size": 12,
"type": "bold"
},
"doc_type": {
"name": "(Scope Document)",
"size": 12,
"type": "bold"
},
"project_title": {
"name": "Project Title",
"size": 12,
"type": "bold"
},
"Version": {
"Version": "Version 1.0",
"size": 12,
"type": "bold"
},
"Degree": {
"name": "Becholar of Science in Computer Science(2016-2020)",
"size": 12,
"type": "bold"
}
}
}
I need to get all nested dictionaries as separate dict object.
So far I only managed to get all key, value pairs
def parse_json_obj(json_obj):
for k, v in json_obj.items():
if isinstance(v, dict):
print('found at ', k)
parse_json_obj(v)
else:
print(v)
what I'm trying to do is get each dict and append its content to a pdf page. I've already figured out how to handle each dict for pdf but don't know how to extract dictionaries.
Any help would be appreciated.

Assuming if what you want is all the dicts which dont have sub dicts in them you could do the following.
jdict = {
"front_page": {
"institue": {
"inst_name": "University Name",
"size": 12,
"type": "bold"
},
"doc_type": {
"name": "(Scope Document)",
"size": 12,
"type": "bold"
},
"project_title": {
"name": "Project Title",
"size": 12,
"type": "bold"
},
"Version": {
"Version": "Version 1.0",
"size": 12,
"type": "bold"
},
"Degree": {
"name": "Becholar of Science in Computer Science(2016-2020)",
"size": 12,
"type": "bold"
}
}
}
def parse_json_obj(json_obj):
list_of_dicts = []
for item in json_obj.values():
if isinstance(item, dict):
has_sub_dict = [subdict for subdict in item.values() if isinstance(subdict, dict)]
if has_sub_dict:
list_of_dicts += parse_json_obj(item)
else:
list_of_dicts.append(item)
return list_of_dicts
list_of_dicts = parse_json_obj(jdict)
print(*list_of_dicts, sep="\n")
OUTPUT
{'inst_name': 'University Name', 'size': 12, 'type': 'bold'}
{'name': '(Scope Document)', 'size': 12, 'type': 'bold'}
{'name': 'Project Title', 'size': 12, 'type': 'bold'}
{'Version': 'Version 1.0', 'size': 12, 'type': 'bold'}
{'name': 'Becholar of Science in Computer Science(2016-2020)', 'size': 12, 'type': 'bold'}
If this isnt what you were trying to achieve then i would suggest to update the question to make it more clear.

Related

How do I get back the value from one of the nested dictionary from JSON file

i am new to python and json. I have this issue where i am unable to get the values of 'labels' print out. It is under a nested dictionary 'result' .
Code that i have tried
for i in data['result']:
print("Value:", i['value'])
values = i['value']
print("X:", values['x'])
print("Y:", values['y'])
print("Width:", values['width'])
print("Height:", values['height'])
Output from the above code
However when i try to do print("labels:", values['labels'])
it return a keyerror: 'labels'
for item in values:
print(item)
this is the output shown
x
y
width
height
rotation
x
y
width
height
rotation
labels
May i ask how do i go out to print out the 'labels' value?
The json files is as follows:
{
"id": 9,
"created_username": "",
"created_ago": "3\u00a0weeks, 3\u00a0days",
"completed_by": {
"id": 1,
"first_name": "",
"last_name": "",
"email": ""
},
"task": {
"id": 1,
"data": {
"image": "/data/upload/2/adf8540d-2-33.png"
},
"meta": {},
"created_at": "2022-11-21T14:52:22.478537Z",
"updated_at": "2022-11-22T12:51:18.105745Z",
"is_labeled": true,
"overlap": 1,
"inner_id": 1,
"total_annotations": 1,
"cancelled_annotations": 0,
"total_predictions": 0,
"comment_count": 0,
"unresolved_comment_count": 0,
"last_comment_updated_at": null,
"project": 2,
"updated_by": 1,
"file_upload": 70,
"comment_authors": []
},
"result": [
{
"original_width": 512,
"original_height": 512,
"image_rotation": 0,
"value": {
"x": 50.898958419872464,
"y": 44.0069438675168,
"width": 2.397222452993284,
"height": 2.0975696463691196,
"rotation": 0
},
"id": "pgaiV8NjWC",
"from_name": "rect",
"to_name": "image",
"type": "rectangle",
"origin": "manual"
},
{
"original_width": 512,
"original_height": 512,
"image_rotation": 0,
"value": {
"x": 50.898958419872464,
"y": 44.0069438675168,
"width": 2.397222452993284,
"height": 2.0975696463691196,
"rotation": 0,
"labels" [
"LM"*
]
},
"id": "pgaiV8NjWC",
"from_name": "labels",
"to_name": "image",
"type": "labels",
"origin": "manual"
}
],
"was_cancelled": false,
"ground_truth": false,
"created_at": "2022-11-22T12:51:18.001777Z",
"updated_at": "2022-11-22T12:51:18.001777Z",
"lead_time": 21.556,
"project": 2,
"parent_prediction": null,
"parent_annotation": null
}
Use the json module
import json
dict = json.load(json_filename)
Then you should access the values by the dictionary keys. Be aware that the result value is a list. Therefore the index 1 is necessary.
values = dict['result'][1]['value']
print("X:", values['x'])

Convert nested json to multiple dataframes - dynamically

i have a question:
Is it possible read a json file and convert to dataframe dynamically?
My example is the next code:
Having this json file, i need 3 table dataframes:
{
"date_time": "01-03-2022, 15:18:32",
"regions": {
"Home Region": "Madrid",
"Primary Region": "Barcelona",
"Secondary Region": "Rio"
},
"customers": [
{
"name": "campo santo",
"address": "rua trebal 1",
"phone": 987456321,
"parking": true
},
{
"name": "santo da silva",
"address": "rua sama 6",
"phone": 654321987,
"parking": false
},
{
"name": "roger campos",
"address": "av casal 10",
"phone": 684426654,
"parking": true
}
],
"office": [
{
"location": "madrid",
"co_working_spaces": 25,
"kitchen": false,
"food_track": 2,
"internal_staff": [
{
"id": 123,
"name": "pablo"
},
{
"id": 874,
"name": "saul"
},
{
"id": 741,
"name": "maria"
}
]
},
{
"location": "rio",
"co_working_spaces": 55,
"kitchen": true,
"food_track": 4,
"internal_staff": [
{
"id": 784,
"name": "raquel"
},
{
"id": 874,
"name": "pedro"
},
{
"id": 145,
"name": "maria"
},
{
"id": 365,
"name": "rocio"
}
]
},
{
"location": "barcelona",
"co_working_spaces": 5,
"kitchen": false,
"food_track": 1,
"internal_staff": [
]
},
{
"location": "la",
"co_working_spaces": 5,
"kitchen": true,
"food_track": 4,
"internal_staff": [
{
"id": 852,
"name": "maria"
},
{
"id": 748,
"name": "sara"
}
]
}
]
}
this is my python code:
import pandas as pd
# from pandas.io.json import json_normalize
import json
with open('offices.json') as f:
dt = json.load(f)
# df = pd.json_normalize(dt)
df1 = pd.json_normalize(dt, 'customers', 'date_time')[['name', 'address', 'phone', 'parking', 'date_time']]
print(df1)
df2 = pd.json_normalize(dt, 'office', 'date_time')[['location', 'co_working_spaces', 'kitchen', 'food_track']]
print(df2)
df3 = pd.json_normalize(dt['office'], 'internal_staff', 'location')
print(df3)
With this code, i got my 3 table dataframes. But i have to know the json structure to create the dataframes.
So is it possible to do it dynamically ?
Regards

How to convert below nested json into pandas dataframe columns. Each node to a different column

I have below json sample, I want it to be converted to pandas dataframe. This is instagram data.
I tried few approaches but I am not getting all nodes as pandas columns.
[
{
"dimensions": {
"height": 734,
"width": 640
},
"display_url": "someurl1",
"edge_liked_by": {
"count": 3797
},
"edge_media_preview_like": {
"count": 3797
},
"edge_media_to_caption": {
"edges": [
{
"node": {
"text": "some text1"
}
}
]
},
"edge_media_to_comment": {
"count": 52
},
"id": "183471293",
"is_video": true,
"location": null,
"owner": {
"id": "191968"
},
"shortcode": "Bl2Nb",
"tags": [
"tag1",
"tag2"
],
"taken_at_timestamp": 153293,
"thumbnail_resources": [
{
"config_height": 150,
"config_width": 150,
"src": "3382263118984904704_n.jpg"
},
{
"config_height": 240,
"config_width": 240,
"src": "904704_n.jpg"
},
{
"config_height": 320,
"config_width": 320,
"src": "904704_n.jpg"
},
{
"config_height": 480,
"config_width": 480,
"src": "904704_n.jpg"
},
{
"config_height": 640,
"config_width": 640,
"src": "904704_n.jpg"
}
],
"thumbnail_src": "904704_n.jpg",
"urls": [
"08864_n.mp4"
],
"video_view_count": 0
}
]
I want it to be converted to pandas dataframe columns. e.g. I need "edge_media_to_caption>edges>node>text" as one column in my dataframe.
Please give me code or logic to do this in python 3.x.
I have tried following,
data = pd.read_json('file.json')
dimensions display_url edge_liked_by edge_media_preview_like edge_media_to_caption edge_media_to_comment id is_video location owner shortcode tags taken_at_timestamp thumbnail_resources thumbnail_src urls video_view_count
0 {'height': 734, 'width': 640} someurl1 {'count': 3797} {'count': 3797} {'edges': [{'node': {'text': 'some text1'}}]} {'count': 52} 183471293 True NaN {'id': '191968'} Bl2Nb [tag1, tag2] 153293 [{'config_height': 150, 'config_width': 150, '... 904704_n.jpg [08864_n.mp4] 0
df1 = pd.DataFrame(pd.json_normalize(data.edge_media_to_caption))
df1

load json data in vue

I am trying to load my data/data.json file to my component so that I can display the data when a particular part is clicked.
Snippet of my data
"colors": [
{"id": 1, "type": "direct", "primary": "red", "hex": [1, 4]},
{"id": 2, "type": "direct", "primary": "yellow", "hex": [2, 5]},
{"id": 3, "type": "direct", "primary": "blue", "hex": [3, 6]},
{"id": 4, "type": "split", "primary": "red", "hex": [1, 7, 8]},
{"id": 5, "type": "split", "primary": "yellow", "hex": [2, 9, 10]},
{"id": 6, "type": "split", "primary": "blue", "hex": [3, 11, 12]},
{"id": 7, "type": "analogous", "primary": "red", "hex": [1, 13, 14]},
{"id": 8, "type": "analogous", "primary": "yellow", "hex": [2, 15, 16]},
{"id": 9, "type": "analogous", "primary": "blue", "hex": [3, 17, 18]}
]
I have a another data set that is connected to the hex data that's why they are just numbered
For the loading in the data, I have it as follows:
var app = new Vue({
el:"#app",
data:function() {
return{
json: null
}
},
methods: {
}
});
$.getJSON('data/data.json', function (json) {
app.json = json;
});
What is a good way to add it to my components so that I can properly display what data I am wanting to show?
You could load it within the component itself once it's mounted, using the mounted hook:
// ...
data () {
return { json: null }
},
mounted () {
$.getJSON('data/data.json', json => {
this.json = json
})
}

Getting error while parsing json response from a dynamic {System.RuntimeType} variable

I'm working on some code in which uses dynamic variables jsonResponse .
dynamic jsonResponse = JsonConvert.DeserializeObject(response);
This variable contains collection of hotel list in json format. From this collection I am getting roomlist collection in a new variable roomResponseList :
var roomResponseList = jsonResponse["hotels"]["hotels"][rooms].roomResponseList;
I am getting first room detail into **JObject responseRateKeys **:
foreach (var roomByResponse in roomResponseList)
{
JObject responseRateKeys = JObject.Parse(roomByResponse.ToString());
var boardNameListByResponse = responseRateKeys.AsJEnumerable().AsEnumerable()
.Select(t => t["rates"]["boardName"].ToString().Trim())
.Distinct()
.ToList();
}
But when I am trying to get any item list from JObject by using linq lambda, I am getting error,
"Cannot access child value on Newtonsoft.Json.Linq.JProperty."
Value of roomByResponse=
{ "code": "DBL.KG-NM", "name": "DOUBLE KING BED NON SMOKING", "rates": [ { "rateKey": "20171217|20171219|W|256|237403|DBL.KG-NM|ID_B2B_26|RO|IWH25|1~1~0||N#AFF5C93E36054661ADCBC14A78A532AE1007", "rateClass": "NRF", "rateType": "RECHECK", "net": "186.04", "allotment": 99, "paymentType": "AT_WEB", "packaging": false, "boardCode": "RO", "boardName": "ROOM ONLY", "cancellationPolicies": [ { "amount": "149.63", "from": "2017-07-14T03:29:00+05:30" } ], "rooms": 1, "adults": 1, "children": 0, "dailyRates": [ { "offset": 1, "dailyNet": "93.02" }, { "offset": 2, "dailyNet": "93.02" } ] }, { "rateKey": "20171217|20171219|W|256|237403|DBL.KG-NM|ID_B2B_26|BB|IWB25|1~1~0||N#AFF5C93E36054661ADCBC14A78A532AE1007", "rateClass": "NOR", "rateType": "RECHECK", "net": "238.92", "allotment": 99, "paymentType": "AT_WEB", "packaging": false, "boardCode": "BB", "boardName": "BED AND BREAKFAST", "rooms": 1, "adults": 1, "children": 0, "dailyRates": [ { "offset": 1, "dailyNet": "119.46" }, { "offset": 2, "dailyNet": "119.46" } ] }, { "rateKey": "20171217|20171219|W|256|237403|DBL.KG-NM|ID_B2B_26|RO|IWH25|2~2~1|2|N#AFF5C93E36054661ADCBC14A78A532AE1007", "rateClass": "NRF", "rateType": "RECHECK", "net": "372.06", "allotment": 99, "paymentType": "AT_WEB", "packaging": false, "boardCode": "RO", "boardName": "ROOM ONLY", "cancellationPolicies": [ { "amount": "299.25", "from": "2017-07-14T03:29:00+05:30" } ], "rooms": 2, "adults": 2, "children": 1, "childrenAges": "2", "dailyRates": [ { "offset": 1, "dailyNet": "186.03" }, { "offset": 2, "dailyNet": "186.03" } ] }, { "rateKey": "20171217|20171219|W|256|237403|DBL.KG-NM|ID_B2B_26|BB|IWB25|2~2~1|2|N#AFF5C93E36054661ADCBC14A78A532AE1007", "rateClass": "NOR", "rateType": "RECHECK", "net": "477.84", "allotment": 99, "paymentType": "AT_WEB", "packaging": false, "boardCode": "BB", "boardName": "BED AND BREAKFAST", "rooms": 2, "adults": 2, "children": 1, "childrenAges": "2", "dailyRates": [ { "offset": 1, "dailyNet": "238.92" }, { "offset": 2, "dailyNet": "238.92" } ] } ] }
Thank you
Pravesh Singh
change linq to
responseRateKeys["rates"].AsJEnumerable().Select(t=>t["boardName"]).Distinct().ToList()