5

this is a preview of the data that im able to access via API using python 3.

   [{'DBNOs': 2,
  'assists': 0,
  'boosts': 0,
  'damageDealt': 129.820038,
  'deathType': 'byplayer',
  'headshotKills': 0,
  'heals': 0,
  'killPlace': 35,
  'killPoints': 1295,
  'killPointsDelta': 3.15819788,
  'killStreaks': 0,
  'kills': 1,
  'lastKillPoints': 0,
  'lastWinPoints': 0,
  'longestKill': 3,
  'mostDamage': 0,
  'name': 'Esskedit',
  'playerId': 'account.7a54835609584b9c943b213345ea7ffb',
  'revives': 1,
  'rideDistance': 2023.24707,
  'roadKills': 0,
  'teamKills': 1,
  'timeSurvived': 655,
  'vehicleDestroys': 0,
  'walkDistance': 1113.72375,
  'weaponsAcquired': 0,
  'winPlace': 17,
  'winPoints': 1281,
  'winPointsDelta': -6.71400356}]

I was able to dump it and make it into a json object using json.dumps(variablename), but how do i save it as a json file?

Thank you

Raegis
  • 53
  • 1
  • 1
  • 4

1 Answers1

4

I'm assuming you have the data into a variable inside your code. So:

You first need to transform this data into a json object (if you just add this list into an object, like "{'list':" + this_data + "}" you have a json!).

So then you only need to get this json data and write it into a file:

    writeFile =open('file_name.json', 'w')
    writeFile.write(your_data)
    writeFile.close()

Hope this might help you :)

PamelaTabak
  • 187
  • 1
  • 8
  • Hi Pamela, Im sorry im having trouble understanding you. What do you mean by "{'list':" + this_data + "}" ? I was able to convert it into a json object using "json.dumps(strip)" but then im unsure of how to save it. – Raegis May 09 '18 at 22:33
  • You can save a `list` object as a JSON just fine. A JSON array is fine as a top-level object. However, will need to use the `json` module, and use `json.dump` because the string representation of the Python data-structures is not quite valid JSON (although it looks similar, but, for example, will use single-quotes instead of double quotes and `None` instead of `null` etc) – juanpa.arrivillaga May 09 '18 at 22:36
  • Oh, if you already have a json file, ignore what I've said about transforming this data into a json! Then you just need to write it to the file, I'm pretty sure you can just writeFile.write(the_result_from_json_dumps) – PamelaTabak May 09 '18 at 22:36
  • I've just tried this and it works. Just replace the json dumps input with whatever you need to turn in a json import json writeFile =open('file_name.json', 'w') writeFile.write(json.dumps([1,2,3,{'4': 5, '6': 7}], separators=(',',':'))) writeFile.close() – PamelaTabak May 09 '18 at 22:39
  • @PamelaTabak you can just use `json.dump(my_python_object, writeFile)` – juanpa.arrivillaga May 09 '18 at 22:53