0

I'm currently trying to extract data from a JSON line, and store the data in an array. The data is a mathematical equation and has operators, groups and values

    {
    "rightArgument": {
        "cell": "C6",
        "value": 13394.5,
        "type": "cell"
    },
    "leftArgument": {
        "rightArgument": {
            "value": "20",
            "type": "constant"
        },
        "leftArgument": {
            "rightArgument": {
                "value": "90",
                "type": "constant"
            },
            "leftArgument": {
                "value": "100",
                "type": "constant"
            },
            "type": "operation",
            "operator": "+"
        },
        "type": "operation",
        "operator": "+"
    },
    "type": "operation",
    "operator": "+"
}

I'm able to extract the first operator and value (+ and 13394.5) however when attempting to navigate down the structure I'm thrown with many errors. I was wondering if anyone could help describe or assist with my code on how to do this?

Currently I have this Python code:

def _getCurrentOperator(data): # function to find current scope operator, pass in the current scope ie data, rightargument, leftargument


    list.append(data["operator"])
    data = data["rightArgument"]
    list.append(data["value"])
    data = data["leftArgument"]
    _getCurrentOperator(data) 

edit:

Within some of my mathematical formulae I have to cater for brackets, which are shown by type: "group" in my JSON file: here is an example of the JSON file:

{
    "rightArgument": {
        "rightArgument": {
            "value": "2",
            "type": "constant"
        },
        "leftArgument": {
            "value": "90",
            "type": "constant"
        },
        "type": "operation",
        "operator": "/"
    },
    "leftArgument": {
        "rightArgument": {
            "arguments": [],
            "name": "pi",
            "type": "function"
        },
        "leftArgument": {
            "rightArgument": {
                "argument": {
                    "rightArgument": {
                        "value": "100",
                        "type": "constant"
                    },
                    "leftArgument": {
                        "rightArgument": {
                            "cell": "C7",
                            "value": 13604.5,
                            "type": "cell"
                        },
                        "leftArgument": {
                            "rightArgument": {
                                "value": "20",
                                "type": "constant"
                            },
                            "leftArgument": {
                                "value": "90",
                                "type": "constant"
                            },
                            "type": "operation",
                            "operator": "/"
                        },
                        "type": "operation",
                        "operator": "*"
                    },
                    "type": "operation",
                    "operator": "+"
                },
                "type": "group"
            },
            "leftArgument": {
                "value": "100",
                "type": "constant"
            },
            "type": "operation",
            "operator": "+"
        },
        "type": "operation",
        "operator": "+"
    },
    "type": "operation",
    "operator

": "+"
}
5
  • 1
    Can you provide an example how you'd like this data to be represented in an array? Your example has a clear recursive structure which will be lost in case of array. Commented Jan 7, 2015 at 13:24
  • Do you want to use nested arrays to represent the nested dictionary structure in the input data? What then, if not? Commented Jan 7, 2015 at 13:34
  • You say "I'm currently trying to extract data from a JSON line". Which line? Commented Jan 7, 2015 at 13:37
  • How to process the arguments depends on how you want to process the resulting list, or how that list is meant to look. That's why I left this part open. You should post this as a new question, and include an example of how the list representation of a function call with arguments is supposed to look. Commented Jan 7, 2015 at 15:57
  • Hi @tobias_k I have posted a new question here: stackoverflow.com/questions/27824152/… Commented Jan 7, 2015 at 16:38

2 Answers 2

2

The problem is that you are redefining data, so when you are trying to get the left argument, data is already the right argument. Also, you should stop the recursion when the left argument is a value.

Try this:

def _getCurrentOperator(data):
    if "operator" in data:               # case 1: operator
        list.append(data["operator"])
        right = data["rightArgument"]    # other var name
        list.append(right["value"])
        left = data["leftArgument"]      # other var name
        _getCurrentOperator(left) 
    else:                                # case 2: value
        list.append(data["value"])

Given your test data, list comes out as ['+', 13394.5, '+', '20', '+', '90', '100']

Of course, this still only works if your right argument is always a value and never another operator. (And as a side note, it seems you are also swapping left and right.) For this case, you could try this:

def _getCurrentOperator(data):
    if "operator" in data:
        list.append(data["operator"])
        _getCurrentOperator(data["leftArgument"]) 
        _getCurrentOperator(data["rightArgument"]) 
    else:
        list.append(data["value"])

To handle all those other types of nodes, just add more cases, like this (probably incomplete):

def _getCurrentOperator(data):
    if data["type"] == "operation":
        list.append(data["operator"])
        _getCurrentOperator(data["rightArgument"]) 
        _getCurrentOperator(data["leftArgument"]) 
    elif data["type"] == "group":
        _getCurrentOperator(data["argument"]) 
    elif data["type"] == "function":
        list.append(data["name"]) # TODO do something with arguments
    else:
        list.append(data["value"])
Sign up to request clarification or add additional context in comments.

5 Comments

That's brilliant, the code works; however I'm now attempting to adapt the code to cater for brackets within my mathematical equations. Within the JSON, this is represented by a type called "Group". I have edited my code above to show this
@Craig My edit should get you on the right track. Basically you'll have to add more cases, like if data['type']=='operation': ... elif data['type']=='group':... etc.
Ah fantastic, you've been a great help! As you can probably tell I'm brand new to this and I've been thrown in the deep end of creating this program by Friday (having never touched or seen neither Python nor JSON before)! Kudos to you!
I have been using your code which is fantastic, however I am attempting to tackle a more complicated algorithm that involves sums. Currently, it will print out the SUM and the left and right arguments, however as the values for the SUM are stored in a list it doesn't print them out. Please could you take a look at the original post to see my updated code?
@Craig You should rather create a new question for this (you can add a link to this one for context) instead of editing this question to effectively be an entirely different question.
-1

The python JSON module will make this immensely easier.

After calling raw = json.dumps(<your-data>), followed by decoded = json.loads(raw), you can work with the JSON much more easily.

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.