Python JSON Iteration into a POST request
Question: I have a JSON file that is formatted something like

    
    
    {
       "unknown1": 
       [
            {"text": "random text again",
            "time": "Thu May 15 19:21:59 +0000 2016"}, 
            "text": "akmfkdlm safsa fasffalmfa",
            "time": "Thu May 21 09:53:51 +0000 2016"}
       ]
       "unknown2":
       [ 
            "text": "fsda lmfalmfa",
            "time": "Thu May 21 09:53:51 +0000 2016"},
       ]
    }
    

The first item in the JSON is a random (unknown) label and there can be any
number of these unknowns. Within these unknowns are always a bunch
`text`/`time` pairings.

I am trying to send each `text` into my REST post service which accepts JSON
formatted to

    
    
    text: "foo bar bat",
    mime_type: "text/html",
    extract_type: "HP" # HP, MP
    

So I am getting this error when I try to run my code and I not sure what to
do.

Here is my code:

    
    
    import json
    import requests
    
    with open('locations_stripped.json') as data_file:
        data = json.load(data_file)
    
    headers = {'Content-Type' : 'application/json'}
    
    for thing in data:
        for text, time in data.iteritems():
            print text
            body = [{ "text": text , "mime_type": "text/html", "extract_type": "HP"}]
            r = requests.post('localhost:3003/api/extract/run', data=body, headers=headers)
            print (r.content)
    

and here is the error:

    
    
     $ python filterrest.py
    unknown1
    Traceback (most recent call last):
      File "filterrest.py", line 30, in <module>
        r = requests.post('localhost:3003/api/extract/run', data=body, headers=headers)
      File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/api.py", line 111, in post
    return request('post', url, data=data, json=json, **kwargs)
      File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/api.py", line 57, in request
    return session.request(method=method, url=url, **kwargs)
      File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/sessions.py", line 461, in request
    prep = self.prepare_request(req)
      File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/sessions.py", line 394, in prepare_request
    hooks=merge_hooks(request.hooks, self.hooks),
      File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/models.py", line 298, in prepare
    self.prepare_body(data, files, json)
      File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/models.py", line 452, in prepare_body
    body = self._encode_params(data)
      File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/models.py", line 89, in _encode_params
    for k, vs in to_key_val_list(data):
    ValueError: too many values to unpack
    

One thing to note is it is printing the wrong text ("unknown1" instead of
"random text again") and I am not sure how to get it to only print the text.

Any help on this?

**UPDATE**

Per everyone's answer/comment I changed my code

    
    
    ... 
    for thing in data:
        for text in data[thing]:
            print text['text']
    

and this prints the text['text'] as I would expect. The issue lies in the way
I am doing my request. I changed my code as a test and set the data to
something that I know should work (I ran it via
[Postman](https://www.getpostman.com/%20%22Postman)).

Changed code:

    
    
    r = requests.post('localhost:3003/api/extract/run', data='Hello. Where does the brown fox go?', headers=headers)
    

Expected Response:

    
    
    [
      {
        "score": 0.30253747367501777,
        "tag": "HP",
      }
    ]
    

Instead what gets printed is what looks like an entire HTML page.


Answer: Assuming you have a valid json. You first need to traverse the list
corresponding to "unknown" keys, now this list again contains dictionaries
with `text` `time` keys.

    
    
    for unknown_key in data:
        for obj in data[unknown_key]:
             body = { "text": obj['text'] , "mime_type": "text/html", "extract_type": "HP"}
             r = requests.post('localhost:3003/api/extract/run', data=body, headers=headers)
             print (r.content)
    

