-3

I have a json file with the following structure:

[{"date_value": "2021-04-01", "boolean_value": false, "string_value": "Any string value", "integer_value": 0}]

I need to edit every element with new data but I having a wrong and different result on the final json file.

For this, I am using the following code:

date_value = date.today()
boolean_value = False
string_value = 'Any string value'
integer_value = 0

...and for editing the json file:

with open(file_name, 'rb') as f:
    mydata = f.read()
    f.close()
            
json_object = json.loads(mydata)

json_object['date_value'] = date_value
json_object['boolean_value'] = boolean_value
json_object['string_value'] = string_value
json_object['integer_value'] = integer_value

with open(file_name, 'w') as outfile:
    json.dump(json_object, outfile)

My problem is, when I open the new json file, I have a different format on every value.

The final file format is:

[{"date_value": ['2021-04-01'], "boolean_value": [False], "string_value": ['Any string value'], "integer_value": [0]}]

Every item value have square brackets on it. How can I write this values without this brackets?

Thank you very much

EDIT: Thanks to @sadik I have solved the problem. On the original code I was using "commas" at the end of every value passed to json_object:

Wrong code:

json_object['date_value'] = date_value,
json_object['boolean_value'] = boolean_value,
json_object['string_value'] = string_value,
json_object['integer_value'] = integer_value,

Correct code:

json_object['date_value'] = date_value
json_object['boolean_value'] = boolean_value
json_object['string_value'] = string_value
json_object['integer_value'] = integer_value
pmatos
  • 276
  • 4
  • 18
  • 2
    Can't reproduce. With the given input and code, I get an error, since `json_object` is a list, not a dict. – 9769953 Apr 18 '21 at 11:12
  • 1
    My guess would be stray commas (`,`) in the code; Python interprets `0,` as a tuple with one item, `(0,)` and that would then get converted to a list `[0]` when written out as JSON. Similarly for the other values — an extra `,` in the code would become `[`...`]` in the JSON. – Jiří Baum Apr 18 '21 at 11:21
  • [{"date_value" ...] indicates a list. The example in my answer below will write the content of the list without brackets using `json_object[0] ` (ie first element of the list) – IODEV Apr 18 '21 at 11:24
  • @IODEV They're talking about the "item values", though, claiming they get `[False]` instead of `false`, for example. (Of course none of that is true, but that's what they're asking about.) – Manuel Apr 18 '21 at 11:34
  • 1
    @sabik, you saved the day. The problem was with "commas" at the end of every value passed to json_object. Edited the question and added the solution at the end – pmatos Apr 18 '21 at 11:35
  • @pmatos Your code doesn't have any such commas, though. – Manuel Apr 18 '21 at 11:36
  • @Manuel, but of course! Need to use my reading glasses :-) – IODEV Apr 18 '21 at 11:38

1 Answers1

1

Per discussion in the comments, the problem was that the original code was using commas at the end of every value passed to json_object:

Wrong code:

json_object['date_value'] = date_value,
json_object['boolean_value'] = boolean_value,
json_object['string_value'] = string_value,
json_object['integer_value'] = integer_value,

Correct code:

json_object['date_value'] = date_value
json_object['boolean_value'] = boolean_value
json_object['string_value'] = string_value
json_object['integer_value'] = integer_value

Python interprets a value with a comma as a tuple with one item which then gets converted to a list when written out as JSON.

Jiří Baum
  • 6,697
  • 2
  • 17
  • 17