0

I spent several hours on this, tried everything I found online, pulled some of the hair left on my head... I have this JSON sent to a Flask webservice I'm writing :

{'jsonArray': '[
    {
        "nom":"0012345679",
        "Start":"2018-08-01",
        "Finish":"2018-08-17",
        "Statut":"Validee"
    },
    {
        "nom":"0012345679",
        "Start":"2018-09-01",
        "Finish":"2018-09-10",
        "Statut":"Demande envoyée au manager"
    },
    {
        "nom":"0012345681",
        "Start":"2018-04-01",
        "Finish":"2018-04-08",
        "Statut":"Validee"
    },
    {
        "nom":"0012345681",
        "Start":"2018-07-01",
        "Finish":"2018-07-15",
        "Statut":"Validee"
    }
    ]'}

I want to simply loop through the records :

app = Flask(__name__)

@app.route('/graph', methods=['POST'])
def webhook():

    if request.method == 'POST':
        req_data = request.get_json()
        print(req_data) #-> shows JSON that seems to be right
        ##print(type(req_data['jsonArray']))
        #j1 = json.dumps(req_data['jsonArray'])
        #j2 = json.loads(req_data['jsonArray'])
        #data = json.loads(j1)
        #for rec in data:
        #    print(rec) #-> This seems to consider rec as one of the characters of the whole JSON string, and prints every character one by one
        #for key in data:
        #     value = data[key]
        #     print("The key and value are ({}) = ({})".format(key, value)) #-> TypeError: string indices must be integers


        for record in req_data['jsonArray']:
            for attribute, value in rec.items(): #-> Gives error 'str' object has no attribute 'items'
                print(attribute, value)

I believe I am lost between JSON object, python dict object, strings, but I don't know what I am missing. I really tried to put the JSON received through json.dumps and json.loads methods, but still nothing. What am I missing ??

I simply want to loop through each record to create another python object that I will feed to a charting library like this :

df = [dict(Task="0012345678", Start='2017-01-01', Finish='2017-02-02', Statut='Complete'),
      dict(Task="0012345678", Start='2017-02-15', Finish='2017-03-15', Statut='Incomplete'),
      dict(Task="0012345679", Start='2017-01-17', Finish='2017-02-17', Statut='Not Started'),
      dict(Task="0012345679", Start='2017-01-17', Finish='2017-02-17', Statut='Complete'),
      dict(Task="0012345680", Start='2017-03-10', Finish='2017-03-20', Statut='Not Started'),
      dict(Task="0012345680", Start='2017-04-01', Finish='2017-04-20', Statut='Not Started'),
      dict(Task="0012345680", Start='2017-05-18', Finish='2017-06-18', Statut='Not Started'),
      dict(Task="0012345681", Start='2017-01-14', Finish='2017-03-14', Statut='Complete')]

1 Answer 1

1

The whole thing is wrapped in single quotes, meaning it's a string and you need to parse it.

for record in json.loads(req_data['jsonArray']):

Looking at your commented code, you did this:

j1 = json.dumps(req_data['jsonArray'])
data = json.loads(j1)

Using json.dumps on a string is the wrong idea, and moreover json.loads(json.dumps(x)) is just the same as x, so that just got you back where you started, i.e. data was the same thing as req_data['jsonArray'] (a string).

This was the right idea:

j2 = json.loads(req_data['jsonArray'])

but you never used j2.

As you've seen, iterating over a string gives you each character of the string.

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

3 Comments

Man... Thanks. I actually tried to use j2 with this : for key in j2: value = j2[key] But I got the list indices must be integers or slices, not dict
May I ask, does that mean that I wasn't getting a proper JSON in the first place ? The whole thing shouldn't have been wrapped in single quotes ?
Yes, j2 is a list of dicts, you can't treat it like a dict. And yes, request.get_json() should return a completely parsed data structure. req_data['jsonArray'] should already be a list.

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.