0

I want to make dic with check date

So I made

import json
import sys
import datetime

def check():
    with open('menu.json', encoding='utf-8') as json_file:
        data = json.load(json_file)
        print(str(data)) # or f"{data}"

check()

with this json

{
    "1": [
        "Date 1 Food.",
    ],
    "2": [
        "Date 2 Food",
    ],
}

But how can I line break I tried json.load.dumps but not works...

and how can I makes output different by date with json? 1,2 is different days

1 Answers1

0

your code works! you need to correct your json file. You have two erros on json format:

  • after of last element on array, you must not put comma,
  • after of last value of json, you must not put comma. Please, can you try using this new menu.json file
{
    "1": [
        "Date 1 Food."
    ],
    "2": [
        "Date 2 Food"
    ]
}
jelambrar
  • 39
  • 3
  • my menu json is pretty long can I make automatically delete commas, so python can understand please? –  Jul 11 '20 at 05:11
  • can you check this solution? https://stackoverflow.com/questions/23705304/can-json-loads-ignore-trailing-commas – jelambrar Jul 11 '20 at 05:21
  • then I got this error AttributeError: 'dict' object has no attribute 'pprint' –  Jul 11 '20 at 09:00
  • with open('menu.json', encoding='utf-8') as json_file: data = JsonComment(json) data = json.load(json_file) print(data.pprint) –  Jul 11 '20 at 09:00