Serializing STIX Objects¶
The string representation of all STIX classes is a valid STIX JSON object.
In [2]:
from stix2 import Indicator
indicator = Indicator(name="File hash for malware variant",
labels=["malicious-activity"],
pattern="[file:hashes.md5 = 'd41d8cd98f00b204e9800998ecf8427e']")
print(str(indicator))
Out[2]:
{
"type": "indicator",
"id": "indicator--5eac4517-6539-4e48-ab51-7d499f599674",
"created": "2017-11-09T19:21:06.285Z",
"modified": "2017-11-09T19:21:06.285Z",
"name": "File hash for malware variant",
"pattern": "[file:hashes.md5 = 'd41d8cd98f00b204e9800998ecf8427e']",
"valid_from": "2017-11-09T19:21:06.285451Z",
"labels": [
"malicious-activity"
]
}
However, the string representation can be slow, as it sorts properties to be in a more readable order. If you need performance and don’t care about the human-readability of the output, use the object’s serialize() function:
In [6]:
print(indicator.serialize())
Out[6]:
{"valid_from": "2017-11-09T19:21:06.285451Z", "name": "File hash for malware variant", "created": "2017-11-09T19:21:06.285Z", "pattern": "[file:hashes.md5 = 'd41d8cd98f00b204e9800998ecf8427e']", "labels": ["malicious-activity"], "modified": "2017-11-09T19:21:06.285Z", "type": "indicator", "id": "indicator--5eac4517-6539-4e48-ab51-7d499f599674"}