Read an input JSON statement

The methods in this notebook implement the functionalities for reading an xAPI statement stored in JSON format.

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

json_file = '../example_single_statement.json'

Load an xAPI statement

Let’s start parsing the json file


source

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
my_statement = load_statement(json_file)

source

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
sample_json = json.loads('["foo", {"bar": ["baz", null, 1.0, 2]}]')
pretty_print_statement(sample_json, indent=2)
[
  "foo",
  {
    "bar": [
      "baz",
      null,
      1.0,
      2
    ]
  }
]

source

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
test_eq(get_value(my_statement, "not_a_key"), None)
test_eq(get_value(my_statement, "stored"), "2022-09-30T13:34:35.959Z")

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


source

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"
    }
actor = get_actor(my_statement)
test_eq(actor["objectType"], test_actor["objectType"])
test_eq(actor["name"], test_actor["name"])
test_eq(actor["mbox"], test_actor["mbox"])

source

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
test_eq(get_actor_name(my_statement), "1s1116")

source

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"
      }
}
verb = get_verb(my_statement)
test_eq(verb["id"], test_verb["id"])
test_eq(verb["display"]["en-US"], test_verb["display"]["en-US"])

source

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
test_eq(get_verb_str(my_statement), "Selected")

source

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"
        }
      }
}
obj = get_object(my_statement)
test_eq(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"])

source

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
test_eq(get_object_definition(my_statement), "Lesson")

source

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
test_eq(get_object_description(my_statement), "Level 1 Module8 started")

Extract the metadata information

The following methods are used to extract the metadata fields we may be interested in

Get general metadata information

source

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
my_date = datetime.strptime("2022-09-30T13:34:35.959Z", "%Y-%m-%dT%H:%M:%S.%f%z")
test_eq(get_stored(my_statement), my_date)

source

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
my_ts = datetime.strptime("2022-09-30T13:34:35.959Z", "%Y-%m-%dT%H:%M:%S.%f%z")
test_eq(get_timestamp(my_statement), my_date)

source

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
test_eq(get_time_diff(my_statement), timedelta()) # In our example statement the timestamps are the same
Get Boolean metadata information

These methods return the metadata providing boolean information related to the statement


source

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
test_eq(is_active(my_statement), True)

source

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
test_eq(is_voided(my_statement), False)

source

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
test_eq(has_generated_id(my_statement), False)
Get ID metadata information

These methods return the metadata providing ID information


source

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
test_eq(get_client(my_statement), "60ffcf8d448b2d059a63e3c4")

source

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
test_eq(get_LRS(my_statement), "60ffcf8d448b2d059a63e3c3")

source

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
test_eq(get_id(my_statement), "6336f06c6ce79d05ebef40a7")

source

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
test_eq(get_persona_id(my_statement), "6103e17eaed02c30c695bffb")

source

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
test_eq(get_organisation(my_statement), "60faab70448b2d059a63e375")

source

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
test_eq(get_hash(my_statement), "3268dd76c35a6077796979e0613654ecf449c46e")
Get queues metadata information

These methods return the metadata related to the queues information in the statement


source

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
test_eq(get_completed_fw_queues(my_statement), list())

source

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
test_eq(get_failed_fw_log(my_statement), list())

source

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
COMPL_QS = ["STATEMENT_FORWARDING_QUEUE", "STATEMENT_PERSON_QUEUE", "STATEMENT_QUERYBUILDERCACHE_QUEUE"]
test_eq(get_completed_queues(my_statement), COMPL_QS)

source

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
test_eq(get_completed_fw_queues(my_statement), list())

source

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
test_eq(get_dead_forwarding_queues(my_statement), list())

source

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
test_eq(get_pending_forwarding_queues(my_statement), list())

source

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
test_eq(get_processing_queues(my_statement), list())

source

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
test_eq(get_registrations(my_statement), list())