1

I've an endpoint where I post the data.

r = requests.post(url, data=data, headers=headers)

I get the following response in Javascript:-

throw 'allowScriptTagRemoting is false.';
(function() {
    var r = window.dwr._[0];
    //#DWR-INSERT
    //#DWR-REPLY
    r.handleCallback("1", "0", {
        msg: "",
        output: {
            msg: "Showing from city center",
            resultcode: 1,
            scrpresent: true,
            srclatitude: "28.63244546123956",
            srclongitude: "77.21981048583984",
        },
        result: "success"
    });
})();

How do I parse the response? I basically want the output json. HOw can I get the same?

1
  • 1
    regex and json.loads Commented Feb 20, 2016 at 13:31

1 Answer 1

1

The problem is - output is not a valid JSON string and cannot be loaded via json.loads().

I would use regular expressions to locate the output object and then use findall() to locate key-value pairs. Sample working code:

import re


data = """
throw 'allowScriptTagRemoting is false.';
(function() {
    var r = window.dwr._[0];
    //#DWR-INSERT
    //#DWR-REPLY
    r.handleCallback("1", "0", {
        msg: "",
        output: {
            msg: "Showing from city center",
            resultcode: 1,
            scrpresent: true,
            srclatitude: "28.63244546123956",
            srclongitude: "77.21981048583984",
        },
        result: "success"
    });
})();
"""
output_str = re.search(r"output: (\{.*?\}),\n", data, re.DOTALL | re.MULTILINE).group(1)

d = dict(re.findall(r'(\w+): "?(.*?)"?,', output_str))
print(d)

Prints:

{'msg': 'Showing from city center', 'resultcode': '1', 'srclongitude': '77.21981048583984', 'srclatitude': '28.63244546123956', 'scrpresent': 'true'}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.