0

I have this javascript file which looks like below:

var abc= {
    "a" : {
            "label": "Monthly",
            "URL": "xyz.com",
            "ArchTag": "M",
            "archData": {
                "Feb 2016":"20160229",
                "Jan 2016":"20160129",
                "Dec 2015":"20151231"
}}};

so I want a way to edit this and say add a new month detail. I guess its a json inside javascript. So how can one edit it in a standard way.

2
  • 4
    JavaScript is not JSON. Commented Apr 19, 2016 at 12:03
  • correct just saw..it is a javascript which looks to have a json inside it...can you help me like how can I add a new key value pair into it like "Mar 2016":"03312016" . I would like to do it the standard way... Commented Apr 19, 2016 at 13:41

5 Answers 5

4

If you can rely on the data looking exactly like this every time then you can just hack off the bit that stops it being JSON:

json_data = json.loads('{' + json_file.read().partition('{')[2])

(or use a regex, or whatever you prefer for string manipulation)

You should still beware that JavaScript is not the same as JSON, so you need some confidence that what you're given will always be an assignment of a valid JSON associative array to a variable.

If, on the other hand, you're being passed arbitrary JavaScript and you're expected to evaluate it, then you have a much bigger problem to solve. What if someone gives you var xyz = "Monthly"; var abc = {"label" : xyz, "URL" : "xyz" + "." + "com"}?

You need a JavaScript engine to solve that more general problem, not just a JSON parser. For example, js2py claims to be a complete JavaScript engine written in Python, but I've never used it so I can't recommend or dis-recommend it.

There are also security issues to consider in executing code from untrusted sources, beyond those you consider if all you're doing is parsing JSON from an untrusted source. So if you need to use a JavaScript engine, make sure you properly understand how it is sandboxed.

Sign up to request clarification or add additional context in comments.

Comments

2

Your code is Javascript, not JSON...

With this code in JS you just create a new Object.

But JSON IS NOT A LANGUAGE!!.! Json is a solution to efficiency store/transmit datas (as assiocative arrays).

Just try to do not define abc:

Your JSON file:

{
    "a" :
    {
            "label": "Monthly",
            "URL": "xyz.com",
            "ArchTag": "M",
            "archData":
            {
                "Feb 2016":"20160229",
                "Jan 2016":"20160129",
                "Dec 2015":"20151231"
            }
    }
}

4 Comments

The OP might know this. They said in their question "var abc infront of it which is used somewhere else hence cannot be removed.". Your answer is a correct explanation, but not an answer to the question the OP has. You could provide a solution on how to deal with the var = in front.
correct just saw..it is a javascript which looks to have a json inside it...can you help me like how can I add a new key value pair into it like "Mar 2016":"03312016" . I would like to do it the standard way..
@Piya no JS does not have JSON inside... JSON is just a cross-platform, cross-language data format, but this is not specific to JS!! he looks like JS, but the two things are differents... To add it in JS, just do abc.a.archData["Mar 2016"] = "03312016"
@Vincent: okay but before adding that how can we load and open our js file..can you help me with that please.
2

If you can not remove var abc of your file, read the file in a string, remove the first characters of the string and load the new string.

  try:
        with open(file) as json_file:
            data = file.read()
            json_data = json.loads(data[9:])
            pprint(json_data)
    except Exception as e:
        print(e)

if it is not always written var abc then start your string at the first { index.

Comments

1

Actually the file content is not json because of the var you mentioned. you should remove that part dynamically:

json_text = json_file.read().replace('var abc=','')
json_data = json.dumps(json_text)
pprint(json_data)

Comments

0

You can use regex to replace the var assignment piece:

with open('file.txt') as json_file:
        data = json_file.read()
        data = re.sub(r'^var\s*\S*\s*=\s*', '', data)
        json_data = json.loads(data)

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.