I'm rewriting a view based on what I know the final output should be in json but it's returning the dictionary as a string.
new output
{
"results":
["
{
'plot': u'',
'runtime': u'N/A',
'description': u'x',
'videos': [
{
'id': 823,
'name': u'x',
'youtube_id': u'FtcubOnXgZk'
}
],
'country': u'India',
'writer': u'Neetu Varma, Ranjeev Verma',
'name': u'Chalk N Duster',
'id': 940,
'director': u'Jayant Gilatar',
'hot': True,
'content': u'x',
'actors': u'Shabana Azmi, Arya Babbar, Gavie Chahal, Juhi Chawla',
'year': 2015,
'images': [
{'small': '/media/cache/62/fd/62fd5158d281c042e3cf1f919183e94e.jpg', 'medium': '/media/cache/5e/32/5e32ebb1a4d25bba0d0c70b4b448e948.jpg'}],
'trailer_youtube_id': u'FtcubOnXgZk',
'type': 'movie',
'slug': u'chalk-n-duster',
'categories': [{'parent_id': 2, 'id': 226, 'name': u'Drama'}],
'shows': {
'starts': '2016-01-16',
'booking_url': u'',
'venue': {
'address': u'',
'id': 854,
'name': u'Nyali Cinemax',
'area': {
'id': 52,
'parent': {
'id': 48,
'name': u'Mombasa'
},
'name': u'Nyali'
}
},
'starts_time': '18:30:00'
}
}", "{'plot': u'' ....
old output
"results": [
{
"actors": "x",
"categories": [
{
"id": 299,
"name": "Biography",
"parent_id": 2
},
],
"content": "x",
"country": "x",
"description": "x",
"director": "x",
"hot": true,
"id": 912,
"images": [
{
"medium": "/media/cache/d2/b3/d2b3a7885e7c39bfc5c2b297b66619c5.jpg",
"small": "/media/cache/e2/d0/e2d01b2c7c77d3590536666de4a7fd7d.jpg"
}
],
"name": "Bridge of Spies",
"plot": "x",
"runtime": "141 min",
"shows": [
{
"booking_url": "",
"starts": "2015-11-27",
"starts_time": "16:30:00",
"venue": {
"address": "The Junction Shopping Mall",
"area": {
"id": 68,
"name": "Ngong Road",
"parent": {
"id": 2,
"name": "Nairobi"
}
},
"id": 1631,
"name": "Century Cinemax Junction"
}
},
],
"slug": "bridge-of-spies",
"trailer_youtube_id": "",
"type": "movie",
"videos": [
{
"id": "795",
"name": "Bridge of Spies",
"youtube_id": "2-2x3r1m2I4"
}
],
"writer": "Matt Charman, Ethan Coen, Joel Coen",
"year": 2015
}, ...
]
Here's the view, I know the shows should also be a list, but in order to start testing I'll need the data to come in the right format. If it's involves too much rewriting I'm okay with links and explanation.
#memoize(timeout=60*60)
def movies_json():
today = datetime.date.today()
movies = Movie.objects.filter(shows__starts__gte=today)
results = []
number = len(movies)
for movie in movies:
print "Now Remaining: {0}".format(number)
number -= 1
medium = get_thumbnail(movie.picture(), '185x274', crop='center', quality=99).url
small = get_thumbnail(movie.picture(), '50x74', crop='center', quality=99).url
movie_details = {
'director':movie.director,
'plot':movie.plot,
'actors':movie.actors,
'content':movie.content,
'country':movie.country,
'description':movie.description,
'hot':movie.hot,
'id':movie.id,
'images':[{'medium':medium, 'small':small}],
'name':movie.name,
'plot':movie.plot,
'runtime':movie.runtime,
'slug':movie.slug,
'type':'movie',
'writer':movie.writer,
'year':movie.year,
}
youtube_details = movie.videos.filter(youtube_id__isnull=False)[0]
movie_details['trailer_youtube_id'] = youtube_details.youtube_id if youtube_details.youtube_id else ""
movie_details['videos'] = [
{
'id':youtube_details.id,
'name':movie.name,
'youtube_id':youtube_details.youtube_id,
}
]
shows = []
for show in movie.shows.all():
show_details = {
'booking_url':show.booking_url,
'starts':show.starts.isoformat(),
'starts_time':show.starts_time.isoformat(),
'venue': {
'address':show.venue.address,
'area': {
'id': show.venue.area.id,
'name': show.venue.area.name,
'parent': {
'id': show.venue.area.parent.id,
'name': show.venue.area.parent.name,
}
},
'id': show.venue.id,
'name': show.venue.name,
}
}
shows.append(show_details)
movie_details['shows'] = show_details
category_list = []
for category in movie.categories.all():
category_details = {
'id':category.id,
'name':category.name,
'parent_id':category.parent.id,
}
category_list.append(category_details)
movie_details['categories'] = category_list
results.append(movie_details)
return results
The data is returned by django rest framework 0.4.0
import json
json_obj = json.load(json_string)
Related
I am trying to extract data from a JSON file, of which a snippet is below. I want to loop through it to get all categories>name and get , as in this case, "Convenience Store" as a result.
{
'meta': {
'code': 200,
'requestId': '5ea184baedbcad001b7a3f8c'
},
'response': {
'venues': [
{
'id': '4d03b2f6dc45a093b4b0e5c6',
'name': 'Ozbesa Market',
'location': {
'address': 'Acibadem basogretmen sokak',
'lat': 41.00622726261631,
'lng': 29.051791450375678,
'labeledLatLngs': [
{
'label': 'display',
'lat': 41.00622726261631,
'lng': 29.051791450375678
}
],
'distance': 92,
'cc': 'TR',
'country': 'Türkiye',
'formattedAddress': [
'Acibadem basogretmen sokak',
'Türkiye'
]
},
'categories': [
{
'id': '4d954b0ea243a5684a65b473',
'name': 'Convenience Store',
'pluralName': 'Convenience Stores',
'shortName': 'Convenience Store',
'icon': {
'prefix': 'https://ss3.4sqi.net/img/categories_v2/shops/conveniencestore_',
'suffix': '.png'
},
'primary': True
}
],
'referralId': 'v-1587643627',
'hasPerk': False
},
Here is my for loop, please help me fix it. It is only returning just convenience stores, but there are also others like 'shopping mall', 'residential building', etc.
for ven in json_data:
for cat in ven:
print(json_data['response']['venues'][0]['categories'][0]['name'])
Thanks in advance!
For the sake of example, I elided some of the per-venue data and added some categories... but as I mentioned in the comment, you're not using the values you loop over.
json_data = {
"meta": {"code": 200, "requestId": "5ea184baedbcad001b7a3f8c"},
"response": {
"venues": [
{
"name": "Ozbesa Market",
"categories": [
{"name": "Convenience Store", "primary": True},
{"name": "Imaginary Category", "primary": False},
],
},
{
"name": "Another Location",
"categories": [
{"name": "Bus Station", "primary": True},
{"name": "Fun Fair", "primary": False},
],
},
]
},
}
for venue in json_data["response"]["venues"]:
print(venue["name"])
for cat in venue["categories"]:
print("..", cat["name"])
will output e.g.
Ozbesa Market
.. Convenience Store
.. Imaginary Category
Another Location
.. Bus Station
.. Fun Fair
I collect data from some django models in a very specific order and output them in json to use in React.
quiz = LessonQuiz.objects.filter(lesson=details).order_by('position')
quizdata = []
if quiz.count() > 0:
for a in quiz:
qz = LessonQuiz.objects.get(id=a.id)
item = {'id': qz.id, 'title': qz.title}
if qz.linkbutton_set.count() > 0:
buttons = qz.linkbutton_set.all()
for b in buttons:
item[b.id] = {
"id": b.id,
"type": b.qtype,
"text": b.text,
"link": b.link,
"color": b.color
}
quizdata.append(item)
at the moment it returns all the data I want as follows,
[
{
"id": 3,
"title": "Do you agree?",
"1": {
"id": 1,
"type": "btn",
"text": "I agree",
"link": "/lesson/welcome/completed",
"color": "#e2574c"
},
"2": {
"id": 2,
"type": "btn",
"text": "I'm not sure I agree",
"link": "/contact",
"color": "#e2574c"
},
"3": {
"id": 3,
"type": "btn",
"text": "I have a suggestion",
"link": "/contact",
"color": "#e2574c"
}
}
]
However, ideally, I would like to have array 1, 2 & 3 in it's own array eg data.
[
{
'id': 3,
'title': 'Do you agree?',
'data': [
1: {
'id': 1,
'type': 'btn',
'text': 'I agree',
'link': '/lesson/welcome/completed',
'color': '#e2574c'
},
2: {
'id': 2,
'type': 'btn',
'text': "I'm not sure I agree",
'link': '/contact',
'color': '#e2574c'
},
3: {
'id': 3,
'type': 'btn',
'text': 'I have a suggestion',
'link': '/contact',
'color': '#e2574c'
}
]
}
]
Any recommendations how I can achieve this?
Alternatively, is there more of a Django way to achieve this?
Thanks in advance
Try using
quiz = LessonQuiz.objects.filter(lesson=details).order_by('position')
quizdata = []
if quiz.count() > 0:
for a in quiz:
qz = LessonQuiz.objects.get(id=a.id)
item = {'id': qz.id, 'title': qz.title, 'data': []} #Add key(data)-value(empty list)
if qz.linkbutton_set.count() > 0:
buttons = qz.linkbutton_set.all()
for b in buttons:
#append to `data`
item['data'].append({b.id: {
"id": b.id,
"type": b.qtype,
"text": b.text,
"link": b.link,
"color": b.color
}
}
quizdata.append(item)
I have the following json document:
{
"id": "5c26321bd8f4113d43b91141",
"idMemberCreator": "5b203bc7e47d817a8138bc37",
"data": {
"list": {
"name": "Sorji for QA",
"id": "5b0a2543b89acdbdb85f7b42"
},
"board": {
"shortLink": "iyCzZ5jx",
"name": "FlicksIO",
"id": "5b0a251f68a9e74b8ec3b3ac"
},
"card": {
"shortLink": "vOt2vO7v",
"idShort": 92,
"name": "New column in main for Storefront provider correlation.",
"id": "5b9c0023533f7c26424ea4ed",
"closed": true
},
"old": {
"closed": false
}
},
"type": "updateCard",
"date": "2018-12-28T14:24:27.455Z",
"limits": {},
"memberCreator": {
"id": "5b203bc7e47d817a8138bc37",
"avatarHash": "73bfa48c76c3c92615fe89ff79a6c5ae",
"avatarUrl": "https://trello-avatars.s3.amazonaws.com/73bfa48f79a6c5ae",
"fullName": "Marie Bond",
"idMemberReferrer": null,
"initials": "MB",
"username": "mb"
}
}
I would like to expand this out to be a single level with dot notation. That is, it should look like:
{
"id": "5c26321bd8f4113d43b91141",
"idMemberCreator": "5b203bc7e47d817a8138bc37",
"data.list.name": "Sorji for QA",
"data.list.id": "5b0a2543b89acdbdb85f7b42"
"data.board.shortLink": "iyCzZ5jx",
"data.board.name": "FlicksIO",
"data.board.id": "5b0a251f68a9e74b8ec3b3ac"
"data.card.shortLink": "vOt2vO7v",
"data.card.idShort": 92,
"data.card.name": "New column in main for Storefront provider correlation.",
"data.card.id": "5b9c0023533f7c26424ea4ed",
"data.card.closed": true
"data.old.closed": false
"type": "updateCard",
"date": "2018-12-28T14:24:27.455Z",
"limits": {},
"memberCreator.id": "5b203bc7e47d817a8138bc37",
"memberCreator.avatarHash": "73bfa48c76c3c92615fe89ff79a6c5ae",
"memberCreator.avatarUrl": "https://trello-avatars.s3.amazonaws.com/73bfa48f79a6c5ae",
"memberCreator.fullName": "Marie Bond",
"memberCreator.idMemberReferrer": null,
"memberCreator.initials": "MB",
"memberCreator.username": "mb"
}
Would it be possible to do this with a generator object? I've been working a lot on recursion today, and have been trying to move from while loops to using generator objects and yields, etc.
You can keep a parameter in the signature of the recursive function to store the paths:
data = {'id': '5c26321bd8f4113d43b91141', 'idMemberCreator': '5b203bc7e47d817a8138bc37', 'data': {'list': {'name': 'Sorji for QA', 'id': '5b0a2543b89acdbdb85f7b42'}, 'board': {'shortLink': 'iyCzZ5jx', 'name': 'FlicksIO', 'id': '5b0a251f68a9e74b8ec3b3ac'}, 'card': {'shortLink': 'vOt2vO7v', 'idShort': 92, 'name': 'New column in main for Storefront provider correlation.', 'id': '5b9c0023533f7c26424ea4ed', 'closed': True}, 'old': {'closed': False}}, 'type': 'updateCard', 'date': '2018-12-28T14:24:27.455Z', 'limits': {}, 'memberCreator': {'id': '5b203bc7e47d817a8138bc37', 'avatarHash': '73bfa48c76c3c92615fe89ff79a6c5ae', 'avatarUrl': 'https://trello-avatars.s3.amazonaws.com/73bfa48f79a6c5ae', 'fullName': 'Marie Bond', 'idMemberReferrer': None, 'initials': 'MB', 'username': 'mb'}}
def dot_paths(d, _paths = []):
for a, b in d.items():
if not b or not isinstance(b, dict):
yield ['.'.join(_paths+[a]), b]
else:
yield from dot_paths(b, _paths+[a])
import json
print(json.dumps(dict(dot_paths(data)), indent=4))
Output:
{
"id": "5c26321bd8f4113d43b91141",
"idMemberCreator": "5b203bc7e47d817a8138bc37",
"data.list.name": "Sorji for QA",
"data.list.id": "5b0a2543b89acdbdb85f7b42",
"data.board.shortLink": "iyCzZ5jx",
"data.board.name": "FlicksIO",
"data.board.id": "5b0a251f68a9e74b8ec3b3ac",
"data.card.shortLink": "vOt2vO7v",
"data.card.idShort": 92,
"data.card.name": "New column in main for Storefront provider correlation.",
"data.card.id": "5b9c0023533f7c26424ea4ed",
"data.card.closed": true,
"data.old.closed": false,
"type": "updateCard",
"date": "2018-12-28T14:24:27.455Z",
"limits": {},
"memberCreator.id": "5b203bc7e47d817a8138bc37",
"memberCreator.avatarHash": "73bfa48c76c3c92615fe89ff79a6c5ae",
"memberCreator.avatarUrl": "https://trello-avatars.s3.amazonaws.com/73bfa48f79a6c5ae",
"memberCreator.fullName": "Marie Bond",
"memberCreator.idMemberReferrer": null,
"memberCreator.initials": "MB",
"memberCreator.username": "mb"
}
I have a JSON file. I would like to change to User Required Format.
input.json file
[{
"Data": [{
"name": [" Devlopment", "34876", "Tez", "4578"],
"results": [{
"sum": 54
}]
}, {
"name": ["production", "09876", "phonepay", "2312"],
"results": [{
"sum": 50.0
}]
}],
"totalResult": {
"results": [{
"sum": 2027.0
}]
},
"unknownGroup": {
"results": [{
"sum": 0.0
}]
},
"performanceStats": {
"fileReadCount": 1,
"decompressionCount": 0,
"decompressionCacheEnabledCount": 0
},
"metadata": {
"eventTypes": ["Data_collection"],
"eventType": "Data_collection",
"openEnded": true
}
},
{
"Data":
[{
"name": [" Quality_Analyst", "623456", "slicepay", "989766"],
"results": [{
"sum": 54
}]
}, {
"name": ["Testing", "7654", "krazybee", "1234"],
"results": [{
"sum": 50.0
}]
}],
"totalResult": {
"results": [{
"sum": 2027.0
}]
},
"unknownGroup": {
"results": [{
"sum": 0.0
}]
},
"performanceStats": {
"fileReadCount": 1,
"decompressionCount": 0,
"decompressionCacheEnabledCount": 0
},
"metadata": {
"eventTypes": ["Data_collection"],
"eventType": "Data_collection",
"openEnded": true
}
}]
I am expecting Output:
==============================
Generate a new JSON file with the help of python:
Read the given JSON file and make a new JSON file
[{
"Data": [{
"Domain_name": " Devlopment",
"Domain_Id": "34876",
"app": "Tez",
"appId": 4578,
"sum": 54
}, {
"Domain_name": "production",
"Domain_Id": "09876",
"app": "phonepay",
"appId": 2312,
"sum": 54
}],
"totalResult": {
"results": [{
"sum": 2027.0
}]
},
"unknownGroup": {
"results": [{
"sum": 0.0
}]
},
"performanceStats": {
"fileReadCount": 1,
"decompressionCount": 0,
"decompressionCacheEnabledCount": 0
},
"metadata": {
"eventTypes": ["Data_collection"],
"eventType": "Data_collection",
"openEnded": true
}
}, {
"Data":
[{
"name": " Quality_Analyst",
"Domain_Id": "623456",
"app": "slicepay",
"appId": 989766,
"sum": 54
}, {
"name": "Testing",
"Domain_Id": "76554",
"app": "krazybee",
"appId": 1234,
"sum": 54
}],
"totalResult": {
"results": [{
"sum": 2027.0
}]
},
"unknownGroup": {
"results": [{
"sum": 0.0
}]
},
"performanceStats": {
"fileReadCount": 1,
"decompressionCount": 0,
"decompressionCacheEnabledCount": 0
},
"metadata": {
"eventTypes": ["Data_collection"],
"eventType": "Data_collection",
"openEnded": true
}
}]
Try it:
json_data = ... # load your json file
for item in json_data:
new_data = []
for data in item["Data"]:
data_item = dict()
data_item["Domain_name"] = data["name"][0]
data_item["Domain_Id"] = data["name"][1]
data_item["app"] = data["name"][2]
data_item["appId"] = data["name"][3]
data_item["sum"] = data["results"][0]["sum"]
new_data.append(data_item)
item["Data"] = new_data
Output:
[
{'Data': [
{'Domain_name': ' Devlopment',
'Domain_Id': '34876',
'app': 'Tez',
'appId': '4578',
'sum': 54},
{'Domain_name': 'production',
'Domain_Id': '09876',
'app': 'phonepay',
'appId': '2312',
'sum': 50.0}],
'totalResult': {'results': [{'sum': 2027.0}]},
'unknownGroup': {'results': [{'sum': 0.0}]},
'performanceStats':
{'fileReadCount': 1,
'decompressionCount': 0,
'decompressionCacheEnabledCount': 0},
'metadata':
{'eventTypes': ['Data_collection'],
'eventType': 'Data_collection',
'openEnded': True}},
{'Data': [
{'Domain_name': ' Quality_Analyst',
'Domain_Id': '623456',
'app': 'slicepay',
'appId': '989766',
'sum': 54},
...
Hii,
Try this Code:Here json_dic in the sense Your JSON-Data
def change_format(val_lst):
key_lst = ['Domain_name','Domain_Id','app','appId']
res_lst = []
for dic in val_lst:
temp_dic = {key_lst[i]:val for i,val in enumerate(dic['name'])}
temp_dic['sum'] = dic['results'][0]['sum']
res_lst.append(temp_dic)
return res_lst
print(list(map(lambda x:dict([(key,change_format(val_lst)) if key=='Data' else (key,val_lst) for key,val_lst in x.items()]),json_dic)))
Result:
[{'Data': [{'Domain_name': ' Devlopment', 'Domain_Id': '34876', 'app': 'Tez', 'appId': '4578', 'sum': 54}, {'Domain_name': 'production', 'Domain_Id': '09876', 'app': 'phonepay', 'appId': '2312', 'sum': 50.0}], 'totalResult': {'results': [{'sum': 2027.0}]}, 'unknownGroup': {'results': [{'sum': 0.0}]}, 'performanceStats': {'fileReadCount': 1, 'decompressionCount': 0, 'decompressionCacheEnabledCount': 0}, 'metadata': {'eventTypes': ['Data_collection'], 'eventType': 'Data_collection', 'openEnded': 'true'}}, {'Data': [{'Domain_name': ' Quality_Analyst', 'Domain_Id': '623456', 'app': 'slicepay', 'appId': '989766', 'sum': 54}, {'Domain_name': 'Testing', 'Domain_Id': '7654', 'app': 'krazybee', 'appId': '1234', 'sum': 50.0}], 'totalResult': {'results': [{'sum': 2027.0}]}, 'unknownGroup': {'results': [{'sum': 0.0}]}, 'performanceStats': {'fileReadCount': 1, 'decompressionCount': 0, 'decompressionCacheEnabledCount': 0}, 'metadata': {'eventTypes': ['Data_collection'], 'eventType': 'Data_collection', 'openEnded': 'true'}}]
Use my function to change format of new_json data and i just use list comprehension to change new_data.Happy Coding
I have a Google Chrome Bookmark file, and it's in JSON format
{
"checksum": "b884cbfb1a6697fa9b9eea9cb2054183",
"roots": {
"bookmark_bar": {
"children": [ {
"date_added": "12989159740428363",
"id": "4",
"name": "test2",
"type": "url",
"url": "chrome://bookmarks/#1"
} ],
"date_added": "12989159700896551",
"date_modified": "12989159740428363",
"id": "1",
"name": "bookmark_bar",
"type": "folder"
},
"other": {
"children": [ {
"date_added": "12989159740428363",
"id": "4",
"name": "test",
"type": "url",
"url": "chrome://bookmarks/#1"
} ],
"date_added": "12989159700896557",
"date_modified": "0",
"id": "2",
"name": "aaa",
"type": "folder"
},
"synced": {
"children": [ ],
"date_added": "12989159700896558",
"date_modified": "0",
"id": "3",
"name": "bbb",
"type": "folder"
}
},
"version": 1
}
and in Python format:
{'checksum': 'b884cbfb1a6697fa9b9eea9cb2054183', 'version': 1, 'roots': {'synced': {'name': 'bbb', 'date_modified': '0', 'children': [], 'date_added': '12989159700896558', 'type': 'folder', 'id': '3'}, 'bookmark_bar': {'name': 'bookmark_bar', 'date_modified': '12989159740428363', 'children': [{'url': 'chrome://bookmarks/#1', 'date_added': '12989159740428363', 'type': 'url', 'id': '4', 'name': 'test2'}], 'date_added': '12989159700896551', 'type': 'folder', 'id': '1'}, 'other': {'name': 'aaa', 'date_modified': '0', 'children': [{'url': 'chrome://bookmarks/#1', 'date_added': '12989159740428363', 'type': 'url', 'id': '4', 'name': 'test'}], 'date_added': '12989159700896557', 'type': 'folder', 'id': '2'}}}
I'm writing a bookmark manager now.
I want to move the web pages by name.
For example: mv /bookmark_bar/test2 /other/test2
But every web pages are dictionaries, and they are in a list. So, I must use index to locate the web pages, I can't locate them by name.
Any ideas?
Is it what you need https://gist.github.com/3332055 ?
An example of how to iterate over the structure - exactly what you want to do with it then, is up to you:
for root, val in bm['roots'].iteritems():
print root, 'is named', val['name']
for child in val['children']:
print '\t', child['name'], 'is at', child['url']
# -*- coding: utf-8 -*-
import json
def hook(pairs):
o = {}
for k, v in pairs.iteritems():
o[str(k)] = v
return o
jsonString = """{"a":"a","b":"b","c":{"c1":"c1","c2":"c2"}}"""
r = json.loads(jsonString, object_hook=hook)
assert r['c']['c1'] == "c1"
del r['c']['c1']
assert not r['c'].has_key('c1')