= '../example_single_statement.json' json_file
Read an input JSON statement
The libraries used to import the data:
As an example, in this package we provide a json
file containing a statement and all its related metadata
Load an xAPI statement
Let’s start parsing the json file
load_statement
load_statement (json_file:str)
Load a json from file and store the information in a Python dictionary object. If the file does not exist, returns an empty dict and print an error message
Type | Details | |
---|---|---|
json_file | str | Filename of the json containing the statement |
Returns | dict | A dictionary representing the statement structure |
= load_statement(json_file) my_statement
pretty_print_statement
pretty_print_statement (statement:dict, indent:int=4)
Displays the content of the statement in a human readable format
Type | Default | Details | |
---|---|---|---|
statement | dict | the statement dict imported from JSON | |
indent | int | 4 | indentation used when printing |
Returns | None |
= json.loads('["foo", {"bar": ["baz", null, 1.0, 2]}]')
sample_json =2) pretty_print_statement(sample_json, indent
[
"foo",
{
"bar": [
"baz",
null,
1.0,
2
]
}
]
get_value
get_value (statement:dict, key:str)
Return the value associated to the specified key in the statement dictionary. If the key does not exist, returns None
Type | Details | |
---|---|---|
statement | dict | Our xAPI statement imported from JSON |
key | str | The key we are interested in |
Returns | typing.Union[str, dict, typing.List, NoneType] | The value associated to the key in the statement |
"not_a_key"), None)
test_eq(get_value(my_statement, "stored"), "2022-09-30T13:34:35.959Z") test_eq(get_value(my_statement,
Extract statement data
The following methids are used to extract the actor, verb and object information, which represents the core information provided in each statement, as well as the version statement
get_actor
get_actor (statement:dict)
Extract the actor information from the statement
Type | Details | |
---|---|---|
statement | dict | Our xAPI statement imported from JSON |
Returns | dict | dictionary containing actor information |
= {
test_actor "objectType": "Agent",
"name": "1s1116",
"mbox": "mailto:student@app.com"
}= get_actor(my_statement)
actor "objectType"], test_actor["objectType"])
test_eq(actor["name"], test_actor["name"])
test_eq(actor["mbox"], test_actor["mbox"]) test_eq(actor[
get_actor_name
get_actor_name (statement:dict)
Quick access to the name field of the actor, as it is the most relevant information
Type | Details | |
---|---|---|
statement | dict | Our xAPI statement imported from JSON |
Returns | str | name of the actor |
"1s1116") test_eq(get_actor_name(my_statement),
get_verb
get_verb (statement:dict)
Extract the verb information from the statement
Type | Details | |
---|---|---|
statement | dict | Our xAPI statement imported from JSON |
Returns | dict | dictionary containing verb information |
= {
test_verb "id": "http://id.tincanapi.com/verb/selected/",
"display": {
"en-US": "Selected"
}
}= get_verb(my_statement)
verb "id"], test_verb["id"])
test_eq(verb["display"]["en-US"], test_verb["display"]["en-US"]) test_eq(verb[
get_verb_str
get_verb_str (statement:dict)
Quick access to the display field of the verb, as it is the most relevant information
Type | Details | |
---|---|---|
statement | dict | Our xAPI statement imported from JSON |
Returns | str | the displayed verb |
"Selected") test_eq(get_verb_str(my_statement),
get_object
get_object (statement:dict)
Extract the object information from the statement
Type | Details | |
---|---|---|
statement | dict | Our xAPI statement imported from JSON |
Returns | dict | dictionary containing object information |
= {
test_obj "objectType": "Activity",
"id": "http://example.com/activities/student-lesson",
"definition": {
"name": {
"en-US": "Lesson"
},"description": {
"en-US": "Level 1 Module8 started"
}
}
}= get_object(my_statement)
obj "id"], test_obj["id"])
test_eq(obj["objectType"], test_obj["objectType"])
test_eq(obj["definition"]["name"]["en-US"], test_obj["definition"]["name"]["en-US"])
test_eq(obj["definition"]["description"]["en-US"], test_obj["definition"]["description"]["en-US"]) test_eq(obj[
get_object_definition
get_object_definition (statement:dict)
Quick access to the object definition
Type | Details | |
---|---|---|
statement | dict | Our xAPI statement imported from JSON |
Returns | str | the object definition |
"Lesson") test_eq(get_object_definition(my_statement),
get_object_description
get_object_description (statement:dict)
Quick access to the object description
Type | Details | |
---|---|---|
statement | dict | Our xAPI statement imported from JSON |
Returns | str | the object description |
"Level 1 Module8 started") test_eq(get_object_description(my_statement),
Extract the metadata information
The following methods are used to extract the metadata fields we may be interested in
Get general metadata information
get_stored
get_stored (statement:dict)
Extract the date and time information of when the statement was stored in the database
Type | Details | |
---|---|---|
statement | dict | Our xAPI statement imported from JSON |
Returns | datetime | datetime object representing the time the statement was stored in the database |
= datetime.strptime("2022-09-30T13:34:35.959Z", "%Y-%m-%dT%H:%M:%S.%f%z")
my_date test_eq(get_stored(my_statement), my_date)
get_timestamp
get_timestamp (statement:dict)
Extract the date and time information of when the statement was created
Type | Details | |
---|---|---|
statement | dict | Our xAPI statement imported from JSON |
Returns | datetime | datetime object representing the time the statement was generated |
= datetime.strptime("2022-09-30T13:34:35.959Z", "%Y-%m-%dT%H:%M:%S.%f%z")
my_ts test_eq(get_timestamp(my_statement), my_date)
get_time_diff
get_time_diff (statement:dict)
Compute the time difference between when a statement was sent and when it was stored in the database
Type | Details | |
---|---|---|
statement | dict | Our xAPI statement imported from JSON |
Returns | timedelta | Time difference between when the statement was sent and when it was stored |
# In our example statement the timestamps are the same test_eq(get_time_diff(my_statement), timedelta())
Get Boolean metadata information
These methods return the metadata providing boolean information related to the statement
is_active
is_active (statement:dict)
Extract the Active field from the statement
Type | Details | |
---|---|---|
statement | dict | Our xAPI statement imported from JSON |
Returns | bool | Boolean representive whether active or not |
True) test_eq(is_active(my_statement),
is_voided
is_voided (statement:dict)
Extract the Active field from the statement
Type | Details | |
---|---|---|
statement | dict | Our xAPI statement imported from JSON |
Returns | bool | Boolean representive whether statement is voided or not |
False) test_eq(is_voided(my_statement),
has_generated_id
has_generated_id (statement:dict)
Extract the Active field from the statement
Type | Details | |
---|---|---|
statement | dict | Our xAPI statement imported from JSON |
Returns | bool | Boolean representive whether statement has generated id |
False) test_eq(has_generated_id(my_statement),
Get ID metadata information
These methods return the metadata providing ID information
get_client
get_client (statement:dict)
Extract the client field from the statement
Type | Details | |
---|---|---|
statement | dict | Our xAPI statement imported from JSON |
Returns | str | ID of the client |
"60ffcf8d448b2d059a63e3c4") test_eq(get_client(my_statement),
get_LRS
get_LRS (statement:dict)
Extract the Learning Record Store ID field from the statement
Type | Details | |
---|---|---|
statement | dict | Our xAPI statement imported from JSON |
Returns | str | ID of the Learning Record Store |
"60ffcf8d448b2d059a63e3c3") test_eq(get_LRS(my_statement),
get_id
get_id (statement:dict)
Extract the ID field from the statement
Type | Details | |
---|---|---|
statement | dict | Our xAPI statement imported from JSON |
Returns | str | ID of the statement |
"6336f06c6ce79d05ebef40a7") test_eq(get_id(my_statement),
get_persona_id
get_persona_id (statement:dict)
Extract the persona identifier
Type | Details | |
---|---|---|
statement | dict | Our xAPI statement imported from JSON |
Returns | str | id of the persona associated to the statement |
"6103e17eaed02c30c695bffb") test_eq(get_persona_id(my_statement),
get_organisation
get_organisation (statement:dict)
Extract the persona identifier
Type | Details | |
---|---|---|
statement | dict | Our xAPI statement imported from JSON |
Returns | str | id of the organization to the statement |
"60faab70448b2d059a63e375") test_eq(get_organisation(my_statement),
get_hash
get_hash (statement:dict)
Extract the hash
Type | Details | |
---|---|---|
statement | dict | Our xAPI statement imported from JSON |
Returns | str | hash of the statement |
"3268dd76c35a6077796979e0613654ecf449c46e") test_eq(get_hash(my_statement),
Get queues metadata information
These methods return the metadata related to the queues information in the statement
get_completed_fw_queues
get_completed_fw_queues (statement:dict)
Extract the List of completed forwarding queues in the statement
Type | Details | |
---|---|---|
statement | dict | Our xAPI statement imported from JSON |
Returns | typing.List | List of completed forwarding queues in the statement |
list()) test_eq(get_completed_fw_queues(my_statement),
get_failed_fw_log
get_failed_fw_log (statement:dict)
Extract the List of failed forwarding log messages in the statement
Type | Details | |
---|---|---|
statement | dict | Our xAPI statement imported from JSON |
Returns | typing.List | List of failed forwarding log messages in the statement |
list()) test_eq(get_failed_fw_log(my_statement),
get_completed_queues
get_completed_queues (statement:dict)
Extract the List of completed queues in the statement
Type | Details | |
---|---|---|
statement | dict | Our xAPI statement imported from JSON |
Returns | typing.List | List of completed queues in the statement |
= ["STATEMENT_FORWARDING_QUEUE", "STATEMENT_PERSON_QUEUE", "STATEMENT_QUERYBUILDERCACHE_QUEUE"]
COMPL_QS test_eq(get_completed_queues(my_statement), COMPL_QS)
get_completed_queues
get_completed_queues (statement:dict)
Extract the List of completed queues in the statement
Type | Details | |
---|---|---|
statement | dict | Our xAPI statement imported from JSON |
Returns | typing.List | List of completed queues in the statement |
list()) test_eq(get_completed_fw_queues(my_statement),
get_dead_forwarding_queues
get_dead_forwarding_queues (statement:dict)
Extract the List of dead forwarding queues in the statement
Type | Details | |
---|---|---|
statement | dict | Our xAPI statement imported from JSON |
Returns | typing.List | List of dead forwarding queues in the statement |
list()) test_eq(get_dead_forwarding_queues(my_statement),
get_pending_forwarding_queues
get_pending_forwarding_queues (statement:dict)
Extract the List of pending forwarding queues in the statement
Type | Details | |
---|---|---|
statement | dict | Our xAPI statement imported from JSON |
Returns | typing.List | List of completed queues in the statement |
list()) test_eq(get_pending_forwarding_queues(my_statement),
get_processing_queues
get_processing_queues (statement:dict)
Extract the List of processing queues in the statement
Type | Details | |
---|---|---|
statement | dict | Our xAPI statement imported from JSON |
Returns | typing.List | List of processing queues in the statement |
list()) test_eq(get_processing_queues(my_statement),
get_registrations
get_registrations (statement:dict)
Extract the List of registrations in the statement
Type | Details | |
---|---|---|
statement | dict | Our xAPI statement imported from JSON |
Returns | typing.List | List of registrations in the statement |
list()) test_eq(get_registrations(my_statement),