简体   繁体   中英

How to create a JSON file from several dictionaries and one list in python

I'm trying to create a JSON file in the following format:

[
{
  "startTimestamp" : "2016-01-03 13:55:00",
  "platform" : "MobileWeb",
  "product" : "20013509_825",
  "ctr" : 0.0150 
},
{...}
]

And the values are stored in the following way:

  • startTimeStamp is in a list called timestampsJSON
  • platform is in a dictionary in this form productBoughtPlatform[product] = platform
  • product is in a dictionary in this form productBoughtCount[product] = count
  • ctr is in a dictionary in this form CTR_Product[product] = ctr

I was trying to make something of the kind:

response = json.dumps({"startTimestamp":ts, for ts in timestampsJSON.items()
            "platform":plat, for plat in productBoughtPlatform.items()
            "product":pro, for pro, key in productBoughtCount.items()
            "ctr":ctr, for ctr in CTR_Product.items()})

I know the syntax is invalid, but can somebody suggest a way to structure this data in a JSON? Thanks!

@William , here I have presented a simple example. Please have a look into it any try the same.

Use all your lists as parameters of zip() .

import json

dates = ["2018-2-5", "2017-7-5", "2016-5-2"]
ages = [23, 45, 34]
names = ["William Studart", "Rishikesh Agrawani", "Tam Fransis"]

output = [{"myDate": d, "myAge": a, "myName": n} for d, a, n in zip(dates, ages, names)]

print(json.dumps(output, indent=4))

Output:

[
    {
        "myName": "William Studart",
        "myAge": 23,
        "myDate": "2018-2-5"
    },
    {
        "myName": "Rishikesh Agrawani",
        "myAge": 45,
        "myDate": "2017-7-5"
    },
    {
        "myName": "Tam Fransis",
        "myAge": 34,
        "myDate": "2016-5-2"
    }
]

@William, you need to know the relation between your timestamps and your product events. In your example, you have on one hand your list, which is ORDERED, and dicts, which are NOT. As your dicts all have products as keys, you have to link the products to your timestamps. Assuming you have a list of product events related to the timestamps in the same order, you want a list of dicts :

output=
[
  {
    "startTimestamp" : timestampsJSON[i],
    "platform" : productBoughtPlatform[products[i]],
    "product" : products[i],
    "ctr" : CTR_Product[products[i]] 
  } 
  for i in range(len(timestampsJSON))]

response = json.dumps(output)

should do the job. You can adapt to your relation between the timestamps and the products. If you have already a dict of events of the form :

events={
  "2016-02-01 00:11:00":"20013509_825",
  "2016-02-01 01:11:00":"20013509_826",
  "2016-02-01 02:11:00":"20013509_825",
  "2016-02-01 03:11:00":"20013509_827"
}

#you build your output list
output=[
  {
    "startTimestamp" : timestamp,
    "platform" : productBoughtPlatform[product],
    "product" : product,
    "ctr" : CTR_Product[product] 
  } 
  for timestamp,product in events.iteritems()]

response = json.dumps(output)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM