I am trying to load below json string using json.loads function in python. but &q; is not a valid json object.
Is there a way for me to remove this? I've provided a sample below:
[{&q;Id&q;:1,&q;Name&q;:&q;Name}]
Angular uses a special escapeHtml function for transfer state. You can find those escapeHtml/unescapeHtml functions here:
export function escapeHtml(text: string): string {
const escapedText: {[k: string]: string} = {
'&': '&a;',
'"': '&q;',
'\'': '&s;',
'<': '&l;',
'>': '&g;',
};
return text.replace(/[&"'<>]/g, s => escapedText[s]);
}
export function unescapeHtml(text: string): string {
const unescapedText: {[k: string]: string} = {
'&a;': '&',
'&q;': '"',
'&s;': '\'',
'&l;': '<',
'&g;': '>',
};
return text.replace(/&[^;]+;/g, s => unescapedText[s]);
}
You can reproduce this escape function in python using the following code:
import json
unescapedText = {
'&a;': '&',
'&q;': '"',
'&s;': '\'',
'&l;': '<',
'&g;': '>',
}
def unescape(str):
for key, value in unescapedText.items():
str = str.replace(key, value)
return str
state = "[{&q;Id&q;:1,&q;Name&q;:&q;Name&q;}]"
decoded = json.loads(unescape(state))
print(decoded)
repl.it: https://replit.com/@bertrandmartel/AngularTransferStateDecode2
try simple to replace the &q; with " double quotes
import json
data = '[{&q;Id&q;:1,&q;Name&q;:&q;Name&q;}]'
data = data.replace('&q;', '"')
print(json.loads(data))
Output
[{'Id': 1, 'Name': 'Name'}]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With