0

I am trying to parse empty json file and trying to append the new data inside the Array.

this is what I am trying for: [{'key1': 'value1'}, {'key2': 'value2'}]

here is my code:

import json
import argparse
import sys


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('-t', '--title', type=str,
                        help='title of the your key')
    parser.add_argument('-v', '--value', type=str,
                        help='value of the your key')
    args = parser.parse_args()
    sys.stdout.write(str(tango(args)))


def tango(args):

    with open('tango.json', 'r') as fp:
        tango2 = json.load(fp)

    title = args.title
    value = args.value

    tango = {
        "name" : title,
        "value" : value
    }

    tango2.append([tango])
    with open('tango.json', 'a') as jsonfeed:
        json.dump(tango2, jsonfeed)


if __name__ == '__main__':
    main()

Error:

python3 playground2.py -t=title2 -v=value2

Traceback (most recent call last): File "playground2.py", line 39, in main() File "playground2.py", line 17, in main sys.stdout.write(str(apikeys(args)))

File "playground2.py", line 23, in apikeys apikeys2 = json.load(fp) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/init.py", line 299, in load parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/init.py", line 354, in loads return _default_decoder.decode(s) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/decoder.py", line 339, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/decoder.py", line 357, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

devops_coder
  • 51
  • 1
  • 4
  • Are you trying to decode the json before you have added any valid json? Your error message appears to be telling you that you don't have any characters in the supplied data – RedBullNinja Nov 13 '18 at 15:52
  • @devops_coder what are the contents of `tango.json` initially? – migron Nov 13 '18 at 16:14
  • I don't think you can load an empty json file as that reads the data in it and assigns it to a variable. Just open it and then write to it. – Zx4161 Nov 13 '18 at 16:18

0 Answers0