Reputation: 11035
I need to convert a bs4.element.NavigableString
(which is from beautiful soup: http://www.crummy.com/software/BeautifulSoup/bs4/doc/#navigablestring) into a json string.
I am using beautiful soup to scrape, and I am having what looks like a json object returned, which looks like the following (without the pretty formatting):
my_object = {"menu": {
"id": "file",
"value": "File",
"popup": {
"menuitem": [
{"value": "New", "onclick": "CreateNewDoc()"},
{"value": "Open", "onclick": "OpenDoc()"},
{"value": "Close", "onclick": "CloseDoc()"}
]
}
}}
I have tried to search my_object
with the normal json methodology, but it does't work, as well as tried searching through it with beautiful soup's methodology. I have also searched for how to convert the bs4.element.NavigableString
into a json.
Thanks for your help!
Upvotes: 1
Views: 1892
Reputation: 365935
Well, that string isn't valid JSON. Part of it is valid JSON.
In particular, it's a line of JavaScript code that assigns a JavaScript literal that happens to be written as valid JSON to a my_object
variable.
If you want to parse the JSON, you have to separate it out from the rest of the statement. For example:
s = '''my_object = {"menu": {
"id": "file",
"value": "File",
"popup": {
"menuitem": [
{"value": "New", "onclick": "CreateNewDoc()"},
{"value": "Open", "onclick": "OpenDoc()"},
{"value": "Close", "onclick": "CloseDoc()"}
]
}
}}'''
jsvar, _, jsvalue = s.partition('=')
value = json.loads(jsvalue)
Upvotes: 1