cti-stix-generator icon indicating copy to clipboard operation
cti-stix-generator copied to clipboard

Is there a concise way to directly export generated STIX data as JSON file?

Open KnightChaser opened this issue 7 months ago • 2 comments

Well, this package is straightforward at generating a random STIX data and I'm satisfied at this level of convenience. However, when I try to load this as JSON data and save it to the data like the code below,

import stix2generator
import json

stix2_generator = stix2generator.create_stix_generator()
stix = stix2_generator.generate()
stix_dump = json.dumps(stix, indent=4)

I get the errors below because some STIX SDO objects are not python default data types. (For example, the runtime exception log below says that the generated STIX data can't be converted to the JSON format since Note which is one of STIX SDO is not compatible.)

Traceback (most recent call last):
  File "C:\Users\user\Documents\GitHub\nis-ems-client\test\fake_stix2_generator.py", line 6, in <module>
    stix_dump = json.dumps(stix, indent=4)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user\.pyenv\pyenv-win\versions\3.11.9\Lib\json\__init__.py", line 238, in dumps
    **kw).encode(obj)
          ^^^^^^^^^^^
  File "C:\Users\user\.pyenv\pyenv-win\versions\3.11.9\Lib\json\encoder.py", line 202, in encode
    chunks = list(chunks)
             ^^^^^^^^^^^^
  File "C:\Users\user\.pyenv\pyenv-win\versions\3.11.9\Lib\json\encoder.py", line 432, in _iterencode
    yield from _iterencode_dict(o, _current_indent_level)
  File "C:\Users\user\.pyenv\pyenv-win\versions\3.11.9\Lib\json\encoder.py", line 406, in _iterencode_dict
    yield from chunks
  File "C:\Users\user\.pyenv\pyenv-win\versions\3.11.9\Lib\json\encoder.py", line 439, in _iterencode
    o = _default(o)
        ^^^^^^^^^^^
  File "C:\Users\user\.pyenv\pyenv-win\versions\3.11.9\Lib\json\encoder.py", line 180, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type Note is not JSON serializable

So, my question is, is there a way to cope with this issue? I'm almost new to STIX, I just read the STIX documentation from the official page today and I am about to use STIX data with my project. Thanks in advance.

KnightChaser avatar Jul 29 '24 07:07 KnightChaser