Python: make a list generator JSON serializable

后端 未结 4 1577
旧巷少年郎
旧巷少年郎 2020-12-29 05:23

How can I concat a list of JSON files into a huge JSON array? I\'ve 5000 files and 550 000 list items.

My fist try was to use jq, but it looks like jq -s is not opt

4条回答
  •  醉酒成梦
    2020-12-29 06:09

    As of simplejson 3.8.0, you can use the iterable_as_array option to make any iterable serializable into an array

    # Since simplejson is backwards compatible, you should feel free to import
    # it as `json`
    import simplejson as json
    json.dumps((i*i for i in range(10)), iterable_as_array=True)
    

    result is [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

提交回复
热议问题