Skip to content

Error: Type is not JSON serializable when Exporting POSCAR Structure to JSON #4460

@vladimirovpv

Description

@vladimirovpv

Python version

Python 3.12.0

Pymatgen version

2025.6.14

Operating system version

Mac Os

Current behavior

Attempting to export a Structure object initialized from a POSCAR file containing site_properties["selective_dynamics"] to JSON format using to(filename="structure.json", fmt="json") results in a Error: Type is not JSON serializable: numpy.ndarray. The error message indicates that numpy.ndarray objects within the site_properties are not JSON serializable.

Expected Behavior

The Structure object should be successfully serialized to JSON format.

Minimal example

import json
import numpy as np
from pymatgen.io.vasp import Poscar
from pymatgen.core.structure import Structure

# Create a mock structure with selective_dynamics
structure = Structure(
    lattice=[[10, 0, 0], [0, 10, 0], [0, 0, 10]],
    species=["H", "O"],
    coords=[[0, 0, 0], [0.5, 0.5, 0.5]],
    site_properties={"selective_dynamics": [[True, True, True], [False, False, False]]}
)

# Create a POSCAR object from the structure
poscar1 = Poscar(structure)
# write poscar to file
poscar1.write_file("test.poscar")


# read poscar from file
poscar = Poscar.from_file("test.poscar")
# Attempt to export the POSCAR structure to JSON
try:
    poscar.structure.to(filename="structure.json", fmt="json")
except TypeError as e:
    print(f"Error: {e}")

Relevant files to reproduce this bug

test.poscar is created by the minimal example itself.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions