python3 in json have as value a variable

Viewed 44

I would like to achieve something that seems very easy but I cannot since I am new to python. I have seen a lot of posts in the web but are not easily understandable.

How can I set a value of a json as a variable? For example below:

import requests 
from getmac import get_mac_address
              eth_mac = get_mac_address()
              newHeaders = {'Content-type': 'application/json', 'Accept': 'text/plain'}
             response = requests.post('.......device.php',
                                 data={"mascineid": 1, "mac":eth_mac},
                                      headers=newHeaders)
          print(response.json())

how can I write the code near 'eth_mac' text so I can type there a variable?

By the above code I get:

Traceback (most recent call last):
  File "pythoniot.py", line 83, in <module>
    print(response.json())
  File "/usr/lib/python3/dist-packages/requests/models.py", line 897, in json
    return complexjson.loads(self.text, **kwargs)
  File "/usr/lib/python3/dist-packages/simplejson/__init__.py", line 518, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3/dist-packages/simplejson/decoder.py", line 370, in decode
    obj, end = self.raw_decode(s)
  File "/usr/lib/python3/dist-packages/simplejson/decoder.py", line 400, in raw_decode
    return self.scan_once(s, idx=_w(s, idx).end())
simplejson.errors.JSONDecodeError: Expecting value: line 1 column 2 (char 1)

Thank you in advance

1 Answers

the solution:

import requests
import json
    
    
eth_mac = get_mac_address()    
data={'machineid': 1, 'mac': eth_mac}
newHeaders = {'Content-type': 'application/json', 'Accept': 'text/plain'}
        
response = requests.post('.............device.php',
                 data=json.dumps(data),
                 headers=newHeaders)
        
print(response.text)

Thank you team for your responds

Related