0

I have 2 lists - dates and weekdays

>>> dates
['2022-02-08', '2022-02-09', '2022-02-10', '2022-02-11', '2022-02-12', '2022-02-13', '2022-02-14', '2022-02-15']
>>> weekdays
['Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday', 'Monday', 'Tuesday']

I want to create a json object that looks like this (for eg) for each element in dates.

myjson = {"label": "Tuesday-2022-02-08", "value": "/inform_date{\"date_list\": \"2022-02-08\"}"}

Basically I want to run a loop where myjson looks like what is given above.

data = []
for i in range(len(dates)):
  myjson = {"label": "{0}-{1}".format(weekdays[i], dates[i]), "value": "/inform_date{\"date_list\": \"{}\"}".format(dates[i])}
  data.append(myjson)

but I get an error when I run the above for loop.

What is the correct way to build myjson so that it looks just like shown in the example above?

user882763
  • 51
  • 4
  • inside of format strings, curly braces that you want in the output need to be "escaped" by doubling them up. `f"I want this brace ->{{"` – JonSG Feb 08 '22 at 14:26

2 Answers2

1

Use a comprehension and f-strings:

import json

data = [{'label': f"{w}-{d}", 'value': f'/inform_date{{"date_list": "{d}"}}'}
            for d, w in zip(dates, weekdays)]
print(json.dumps(data, indent=4))
[
    {
        "label": "Tuesday-2022-02-08",
        "value": "/inform_date{\"date_list\": \"2022-02-08\"}"
    },
    {
        "label": "Wednesday-2022-02-09",
        "value": "/inform_date{\"date_list\": \"2022-02-09\"}"
    },
    {
        "label": "Thursday-2022-02-10",
        "value": "/inform_date{\"date_list\": \"2022-02-10\"}"
    },
    {
        "label": "Friday-2022-02-11",
        "value": "/inform_date{\"date_list\": \"2022-02-11\"}"
    },
    {
        "label": "Saturday-2022-02-12",
        "value": "/inform_date{\"date_list\": \"2022-02-12\"}"
    },
    {
        "label": "Sunday-2022-02-13",
        "value": "/inform_date{\"date_list\": \"2022-02-13\"}"
    },
    {
        "label": "Monday-2022-02-14",
        "value": "/inform_date{\"date_list\": \"2022-02-14\"}"
    },
    {
        "label": "Tuesday-2022-02-15",
        "value": "/inform_date{\"date_list\": \"2022-02-15\"}"
    }
]
Corralien
  • 109,409
  • 8
  • 28
  • 52
  • The dates need to be in quotes too. It should be \"2022-02-08\" instead of 2022-02-08 – user882763 Feb 08 '22 at 14:38
  • I get 2 slashes insted on single backslash on my windows 10 machine - – user882763 Feb 08 '22 at 15:53
  • >>> x = json.dumps(data) >>> x '[{"label": "Tuesday-2022-02-08", "value": "/inform_date{\\"date_list\\": \\"2022-02-08\\"}"}, {"label": "Wednesday-2022-02-09", "value": "/inform_date{\\"date_list\\": \\"2022-02-09\\"}"}, {"label": "Thursday-2022-02-10", "value": "/inform_date{\\"date_list\\": \\"2022-02-10\\"}"}, {"label": "Friday-2022-02-11", "value": "/inform_date{\\"date_list\\": \\"2022-02-11\\"}"}, {"label": "Saturday-2022-02-12", "value": "/inform_date{\\"date_list\\": \\"2022-02-12\\"}"}, ]' – user882763 Feb 08 '22 at 15:54
  • Use print(x). It's just the escape mecanisem of python to display the representation of a string. – Corralien Feb 08 '22 at 16:00
0

Your second format string has 'spare' braces which need to be escaped:

myjson = {"label": "{0}-{1}".format(weekdays[i], dates[i]), "value": "/inform_date{{\"date_list\": \"{}\"}}".format(dates[i])}
quamrana
  • 37,849
  • 12
  • 53
  • 71