How can i insert this JSON file into a MongoDB collection? - python

I'm trying to insert this object into a mongo DB collection. I've tried a lot of ways and haven't gotten any results. I was wondering if anyone here could help me. The main problem is when passing the array of items into the key items.
The JSON File is similar to this:
{
'code': 'iuhuilknlkn',
'description': 'nllksnd',
'currency': 'Mxn',
'items': [
{
'item': {
'_id': {
'$oid': '60065d253ef6d468ced3603f'
},
'code': '2',
'description': '22',
'currency': 'Mxn',
'MU': 'Hr',
'sellingPrice': 1,
'buyingPrice': 3,
'supplier': None,
'cid': '5fbd81b32b325e5ca15fe5c9',
'itemAmount': 1,
'itemProductPrice': 1,
'itemAmountPrice': 1
}
},
{
'item': {
'_id': {
'$oid': '6011c18883a280ae0e5b8185'
},
'code': 'prb-001',
'description': 'prueba 1 artículo',
'currency': 'Mxn',
'MU': 'Ser',
'sellingPrice': 100.59,
'buyingPrice': 12,
'supplier': None,
'cid': '5fbd81b32b325e5ca15fe5c9',
'itemAmount': 1,
'itemProductPrice': 100.59,
'itemAmountPrice': 100.59
}
}
],
'price': 101.59
}
and the code I'm using right now is the following:
productsM.insert({
'code':newP['code'],
'description':newP['description'],
'currency' :newP['currency'],
'items' : [([val for dic in newP['items'] for val in
dic.values()])],
'price' : newP['price'],
'cid': csHe })
The error I get is the next one:
key '$oid' must not start with '$'

Your error is quite clear. Simply removing the $ from the $oid keys in your dictionary/json will resolve the issue. I don't think you can have a $ in key names since they are reserved for operators such as $in or $regex.
I removed the $ from the $oid keys and it worked like a charm. All I did was the following:
data = {
"code": "iuhuilknlkn",
"description": "nllksnd",
"currency": "Mxn",
"items": [
{
"item": {
"_id": {
"oid": "60065d253ef6d468ced3603f"
},
"code": "2",
"description": "22",
"currency": "Mxn",
"MU": "Hr",
"sellingPrice": 1,
"buyingPrice": 3,
"supplier": None,
"cid": "5fbd81b32b325e5ca15fe5c9",
"itemAmount": 1,
"itemProductPrice": 1,
"itemAmountPrice": 1
}
},
{
"item": {
"_id": {
"oid": "6011c18883a280ae0e5b8185"
},
"code": "prb-001",
"description": "prueba 1 artículo",
"currency": "Mxn",
"MU": "Ser",
"sellingPrice": 100.59,
"buyingPrice": 12,
"supplier": None,
"cid": "5fbd81b32b325e5ca15fe5c9",
"itemAmount": 1,
"itemProductPrice": 100.59,
"itemAmountPrice": 100.59
}
}
],
"price": 101.59
}
db.insert(data)

Related

get name of "product" from json

this is JSON text, I want to get name of "product":
{"result": "success", "totalresults": 1, "startnumber": 0, "numreturned": 1, "orders": {"order": [{"id": 3267, "ordernum": 13424555, "userid": 2132, "contactid": 0, "requestor_id": 2173, "admin_requestor_id": 0, "date": "2022-08-05 16:39:18", "nameservers": "", "transfersecret": "", "renewals": "", "promocode": "", "promotype": "", "promovalue": "", "orderdata": "[]", "amount": "12.00", "paymentmethod": "usd", "invoiceid": 101, "status": "Active", "ipaddress": "111.111.111.111", "fraudmodule": "", "fraudoutput": "", "notes": "", "paymentmethodname": "Cryptocurrencies", "paymentstatus": "Paid", "name": "Max Vorenberg", "currencyprefix": "$", "currencysuffix": " USD", "frauddata": "", "validationdata": "", "lineitems": {"lineitem": [{"type": "product", "relid": 3648, "producttype": "Dedicated/VPS Server", "product": "Dedicated Server Windows", "domain": "chz", "billingcycle": "Monthly", "amount": "$12.00 USD", "status": "Active"}]}}]}}
this is my code:
data = {
'identifier':identifier,
'secret':secret,
'action': 'GetOrders',
'id':3267,
'responsetype':'json',
}
# sending post request and saving response as response object
r = requests.post(url = API_ENDPOINT, data = data)
data2 = r.json()
text_data2 = json.dumps(data2)
print(text_data2)
print(text_data2['product'])
but get the error : "NameError: name 'string' is not defined"
This will get you your expected result for your example input:
data["orders"]["order"][0]["lineitems"]["lineitem"][0]["product"]
Although, where I have [0] it is getting the first item in those lists. Your example has only one item in the lists but if there are more, you will need to iterate over them to get the result(s) you want.
This is based on your example data:
data = {
'numreturned': 1,
'orders': {'order': [{'admin_requestor_id': 0,
'amount': '12.00',
'contactid': 0,
'currencyprefix': '$',
'currencysuffix': ' USD',
'date': '2022-08-05 16:39:18',
'frauddata': '',
'fraudmodule': '',
'fraudoutput': '',
'id': 3267,
'invoiceid': 101,
'ipaddress': '111.111.111.111',
'lineitems': {'lineitem': [{'amount': '$12.00 USD',
'billingcycle': 'Monthly',
'domain': 'chz',
'product': 'Dedicated '
'Server Windows',
'producttype': 'Dedicated/VPS '
'Server',
'relid': 3648,
'status': 'Active',
'type': 'product'}]},
'name': 'Max Vorenberg',
'nameservers': '',
'notes': '',
'orderdata': '[]',
'ordernum': 13424555,
'paymentmethod': 'usd',
'paymentmethodname': 'Cryptocurrencies',
'paymentstatus': 'Paid',
'promocode': '',
'promotype': '',
'promovalue': '',
'renewals': '',
'requestor_id': 2173,
'status': 'Active',
'transfersecret': '',
'userid': 2132,
'validationdata': ''}]},
'result': 'success',
'startnumber': 0,
'totalresults': 1
}
Explanation
To break this down I am first getting data["orders] which contains this dictionary:
{
'order': [{'admin_requestor_id': 0,
...
'lineitems': {'lineitem': [{'amount': '$12.00 USD',
'billingcycle': 'Monthly',
'domain': 'chz',
'product': 'Dedicated Server Windows',
'producttype': 'Dedicated/VPS Server',
'relid': 3648,
'status': 'Active',
'type': 'product'}]},
...
]
}
This dictionary only has one key "orders", which contains a list containing a single dictionary. If you expect it to have more than one dictionary in it then you will need to loop through it like so:
for order in data["orders"]["order"]:
...
In the ... you would have a single order like the following:
{
'admin_requestor_id': 0,
...
'lineitems': {'lineitem': [{'amount': '$12.00 USD',
'billingcycle': 'Monthly',
'domain': 'chz',
'product': 'Dedicated Server Windows',
'producttype': 'Dedicated/VPS Server',
'relid': 3648,
'status': 'Active',
'type': 'product'}]},
...
}
This has a key "lineitems" which is a dictionary containing a single key "lineitem" which is another list of dictionaries and like above you can iterate this as well. Our code thus far would become:
for order in data["orders"]["order"]:
for item in order["lineitems"]["lineitem"]:
...
Now in the inner loop we have a dictionary like the following:
{
'amount': '$12.00 USD',
'billingcycle': 'Monthly',
'domain': 'chz',
'product': 'Dedicated Server Windows',
'producttype': 'Dedicated/VPS Server',
'relid': 3648,
'status': 'Active',
'type': 'product'
}
This dictionary has the key "product" which is the value we are looking for. Our iterative approach now becomes:
for order in data["orders"]["order"]:
for item in order["lineitems"]["lineitem"]:
print(item["product"])
It's easier to see why, when the json is formatted visually:
{
"result": "success",
"totalresults": 1,
"startnumber": 0,
"numreturned": 1,
"orders": {
"order": [
{
"id": 3267,
"ordernum": 13424555,
"userid": 2132,
"contactid": 0,
"requestor_id": 2173,
"admin_requestor_id": 0,
"date": "2022-08-05 16:39:18",
"nameservers": "",
"transfersecret": "",
"renewals": "",
"promocode": "",
"promotype": "",
"promovalue": "",
"orderdata": "[]",
"amount": "12.00",
"paymentmethod": "usd",
"invoiceid": 101,
"status": "Active",
"ipaddress": "111.111.111.111",
"fraudmodule": "",
"fraudoutput": "",
"notes": "",
"paymentmethodname": "Cryptocurrencies",
"paymentstatus": "Paid",
"name": "Max Vorenberg",
"currencyprefix": "$",
"currencysuffix": " USD",
"frauddata": "",
"validationdata": "",
"lineitems": {
"lineitem": [
{
"type": "product",
"relid": 3648,
"producttype": "Dedicated/VPS Server",
"product": "Dedicated Server Windows",
"domain": "chz",
"billingcycle": "Monthly",
"amount": "$12.00 USD",
"status": "Active"
}
]
}
}
]
}
}
The product value that you are looking for is nested under a collection of objects and arrays, so you'll need a more sophisticated approach to parsing the value out.
At its simplest, you could access it via data2["orders"]["order"][0]["lineitems"]["lineitem"][0]["product"], but this assumes that all the objects and arrays are present, that they contain values, and the first item in each array is the one you want. Nothing to go wrong there. ;)
Also, you may want to add in some try blocks around the request etc, as if they fail they'll throw an exception.

Looping through JSON (Foursquare API)

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

how to generate a user required JSON file

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

Json format for python

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)

Work with JSON file on Python

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')

Categories