Python Forum
Write a dictionary with arrays as values into JSON format
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Write a dictionary with arrays as values into JSON format
#4
For completeness, let's add one more solution

spam = np.array([1, 2, 3])
eggs = {'some_key':spam}
print(eggs)
with open('export.json', 'w') as f:
    json.dump(eggs, f, separators=(',', ':'), sort_keys=True, indent=4, default=lambda x: x.tolist())
  
## to read it afterward
with open('export.json', "r") as f:
    data = json.load(f)
data = {key:np.array(value) for key, value in data.items()}
print(data)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
RE: Write a dictionary with arrays as values into JSON format - by buran - Oct-20-2021, 10:38 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Write from dictionary to excel divon 3 3,727 Jun-11-2023, 10:37 AM
Last Post: Larz60+
  comparing floating point arrays to arrays of integers in Numpy amjass12 0 1,627 Jul-26-2021, 11:58 AM
Last Post: amjass12
  How to compare two json and write to third json differences with pandas and numpy onenessboy 0 4,700 Jul-24-2020, 01:56 PM
Last Post: onenessboy
  Numpy arrays and compatability with Fortran arrays merrittr 0 1,873 Sep-03-2019, 03:54 AM
Last Post: merrittr
  Merge JSON files prioritizing the updated values from most recent file nebulae 0 2,538 Apr-17-2019, 10:15 AM
Last Post: nebulae
  Trying to import JSON data into Python/Pandas DataFrame then edit then write CSV Rhubear 0 4,098 Jul-23-2018, 09:50 PM
Last Post: Rhubear

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020