How to iterate over JSON array to find value with changing keys? - python

I am trying to extract many values from a JSON array so I am iterating through it to extract the values based on their keys, however one of the keys changes depending on the item and I am getting a KeyError when the loop comes across the different key.
I've tried using try and except to catch this but since I am looping through the entire array it will throw the same exception for the other key this time.
Here is my code to extract the values:
df = []
for item in json_response["items"]:
df.append({
'AccountName': item["accountName"],
'Action': item["action"],
'Application': item["application"],
'AppID': item["attributes"]["appId"],
'AppName': item["attributes"]["AppName"],
'Errors': item["attributes"]["errors"],
'ContextID': item["contextid"],
'Created': item["created"],
'HostName': item["hostname"],
'EventID': item["id"],
'Info': item["info"],
'ipaddr': item["ipaddr"],
'EventSource': item["source"],
'Stack': item["stack"],
'Target': item["target"],
'TrackingID': item["trackingId"],
'Type': item["type"]
})
Here is an example JSON from a larger array I am extracting from:
{
"accountName": null,
"action": "Disable",
"application": "Application1",
"attributes": {
"appId": "7d264050024",
"AppName": "Application1",
"errors": [
"Rule: Rule not found."
]
},
"contextid": null,
"created": 1553194821098,
"hostname": null,
"id": "ac09ea0082",
"info": null,
"ipaddr": null,
"source": "System1",
"stack": null,
"target": "TargetName1.",
"trackingId": null,
"type": null
}
This would work but sometimes the "attributes" looks like:
"attributes": {
"appId": "7d2451684288",
"cloudAppName": "Application1",
"RefreshFailure": true
}
How can I extract either the "errors" value or the "RefreshFailure" value when iterating over the entire array?

Test key existence in attributes to retrieve the different values:
df = []
for item in json_response["items"]:
errors = "NA"
if "errors" in item["attributes"]
errors = item["attributes"]["errors"]
elif "RefreshFailure" in item["attributes"]:
errors = item["attributes"]["RefreshFailure"]
df.append({
'AccountName': item["accountName"],
'Action': item["action"],
'Application': item["application"],
'AppID': item["attributes"]["appId"],
'AppName': item["attributes"]["AppName"],
'Errors': errors,
'ContextID': item["contextid"],
'Created': item["created"],
'HostName': item["hostname"],
'EventID': item["id"],
'Info': item["info"],
'ipaddr': item["ipaddr"],
'EventSource': item["source"],
'Stack': item["stack"],
'Target': item["target"],
'TrackingID': item["trackingId"],
'Type': item["type"]
})

I tried to emulate your data to make the code work.
import json
from pprint import pprint
json_data = '''
{
"items": [
{
"accountName": null,
"action": "Disable",
"application": "Application1",
"attributes": {
"appId": "7d264050024",
"AppName": "Application1",
"errors": [
"Rule: Rule not found."
]
},
"contextid": null,
"created": 1553194821098,
"hostname": null,
"id": "ac09ea0082",
"info": null,
"ipaddr": null,
"source": "System1",
"stack": null,
"target": "TargetName1.",
"trackingId": null,
"type": null
},
{
"accountName": null,
"action": "Disable",
"application": "Application1",
"attributes": {
"appId": "7d2451684288",
"cloudAppName": "Application1",
"RefreshFailure": true
},
"contextid": null,
"created": 1553194821098,
"hostname": null,
"id": "ac09ea0082",
"info": null,
"ipaddr": null,
"source": "System1",
"stack": null,
"target": "TargetName1.",
"trackingId": null,
"type": null
}
]
}'''
json_response = json.loads(json_data)
def capitalize(s):
return s[0].upper() + s[1:]
df = []
for item in json_response["items"]:
d = {}
# Iterate over the items in the dictionary/json object and add them one by one using a loop
# This will work even if the items in the json_response changes without having to change the code
for key, value in item.items():
# "attributes" is itself a dictionary/json object
# Its items have to be unpacked and added instead of adding it as a raw object
if isinstance(value, dict):
for k, v in value.items():
d[capitalize(k)] = v
else:
d[capitalize(key)] = value
df.append(d)
pprint(df)
Output:
[{'AccountName': None,
'Action': 'Disable',
'AppId': '7d264050024',
'AppName': 'Application1',
'Application': 'Application1',
'Contextid': None,
'Created': 1553194821098,
'Errors': ['Rule: Rule not found.'],
'Hostname': None,
'Id': 'ac09ea0082',
'Info': None,
'Ipaddr': None,
'Source': 'System1',
'Stack': None,
'Target': 'TargetName1.',
'TrackingId': None,
'Type': None},
{'AccountName': None,
'Action': 'Disable',
'AppId': '7d2451684288',
'Application': 'Application1',
'CloudAppName': 'Application1',
'Contextid': None,
'Created': 1553194821098,
'Hostname': None,
'Id': 'ac09ea0082',
'Info': None,
'Ipaddr': None,
'RefreshFailure': True,
'Source': 'System1',
'Stack': None,
'Target': 'TargetName1.',
'TrackingId': None,
'Type': None}]
If you want the key name to be Errors even when the actual key name is RefreshFailure, you can add these lines of code before df.append(d)
...
if 'RefreshFailure' in d:
d['Errors'] = d['RefreshFailure']
del d['RefreshFailure']
df.append(d)
With these few extra lines of code, the output would look like this:
[{'AccountName': None,
'Action': 'Disable',
'AppId': '7d264050024',
'AppName': 'Application1',
'Application': 'Application1',
'Contextid': None,
'Created': 1553194821098,
'Errors': ['Rule: Rule not found.'],
'Hostname': None,
'Id': 'ac09ea0082',
'Info': None,
'Ipaddr': None,
'Source': 'System1',
'Stack': None,
'Target': 'TargetName1.',
'TrackingId': None,
'Type': None},
{'AccountName': None,
'Action': 'Disable',
'AppId': '7d2451684288',
'Application': 'Application1',
'CloudAppName': 'Application1',
'Contextid': None,
'Created': 1553194821098,
'Errors': True,
'Hostname': None,
'Id': 'ac09ea0082',
'Info': None,
'Ipaddr': None,
'Source': 'System1',
'Stack': None,
'Target': 'TargetName1.',
'TrackingId': None,
'Type': None}]

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.

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

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)

Expanding a dictionary using dot notation

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"
}

Not able to Parse JSON Response

I am using Python2.7 and simple json module, I am able to get a response but when i want to do something with this JSON Response I am not able to do.
Python Code :
query_url = self.api_url + 'projects'
try:
req = urllib2.Request(query_url, None, {"Authorization": self._auth})
handler = self._opener.open(req)
except urllib2.HTTPError, e:
print e.headers
raise e
print simplejson.load(handler)
JSON Response:
{'start': 0, 'nextPageStart': 166, 'values': [{'description': 'This Repo is created to maintain the code versioning accordingly for My project', 'links': {'self': [{'href': 'https://bitbucket.xyz.xyz/projects/My'}]}, 'id': 757, 'key': 'MY', 'type': 'NORMAL', 'public': False, 'name': 'hub'},{'description': 'Services ', 'links': {'self': [{'href': 'https://bitbucket.xyz.xyz/projects/Hello'}]}, 'id': 1457, 'key': 'HE', 'type': 'NORMAL', 'public': False, 'name': 'Hello'}], 'limit': 25, 'isLastPage': False, 'size': 25}
Few data i have removed just kept first and last.
The error which i am observing
Error: Parse error on line 1:
..."NORMAL", "public": False, "name": "Advi
-----------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'undefined'
Can some body help me here what i am doing wrong here.
Because your json is invalid. Here it is valid version of your Json data.
First error Strings should be wrapped in double quotes.
Secondly boolean variable in javascprit is lower case. Use false instead of False
{
"start": 0,
"nextPageStart": 166,
"values": [{
"description": "This Repo is created to maintain the code versioning accordingly for My project",
"links": {
"self": [{
"href": "https://bitbucket.xyz.xyz/projects/My"
}]
},
"id": 757,
"key": "MY",
"type": "NORMAL",
"public": false,
"name": "hub"
},
{
"description": "Services ",
"links": {
"self": [{
"href": "https://bitbucket.xyz.xyz/projects/Hello"
}]
},
"id": 1457,
"key": "HE",
"type": "NORMAL",
"public": false,
"name": "Hello"
}
],
"limit": 25,
"isLastPage": false,
"size": 25
}

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