Anchors and Aliases

Anchors, in YAML, is a feature that allows us to identify an item that can be referenced elsewhere in our .yaml or .yml file. At the same time, the alias is a feature that follows the preceding anchor.

Sometimes when we’re writing a data structure using YAML some of the data can be repetitive. The function of anchors and aliases is to help us reduce the repeating syntax.

To understand the basics of how anchors and aliases work in the data that we are writing, please take a look at the case below.

Case example:

student_grade:
        Adam:
           grade: B
           status: Good
           comment: You can do better!
        Ariana:
           grade: A
           status: Excellent
           comment: Keep it up!
        Brian:
           grade: A
           status: Excellent
           comment: Keep it up!
        Celeste:
           grade: B
           status: Good
           comment: You can do better!
        Dean:
           grade: C
           status: Enough
           comment: It's okay, work harder!
        Kiara:
           grade: C
           status: Enough
           comment: It's okay, work harder!
        Tyler:
           grade: C
           status: Enough
           comment: It's okay, work harder!
        Zayn:
           grade: B
           status: Good
           comment: You can do better!

In the data above, we have 8 students with grades A, B, and C. There is nothing wrong with writing the data like the example above. However, it can be a hassle and time-consuming if we have hundreds more students’ data we need to process. Therefore, we want to try to make our work more time-efficient.

First, we will need to create a key followed by anchors using the ampersand & symbol and its alias. Then, we can add the attribute referenced by the anchors that we have created. The attribute will contain the value of grade, status, and comment of the students.

Creating anchors example:

gradeA: &A
    grade: A
    status: Excellent
    comment: Keep it up!
    
gradeB: &B  
    grade: B
    status: Good
    comment: You can do better!
       
gradeC: &C       
    grade: C
    status: Enough
    comment: It's okay, work harder!

In the solution that we have written above, we have created 3 anchors followed by its aliases &A, &B , and &C. Now referencing the case example above, we would write the data in the example below.

Referencing anchors example:

gradeA: &A
    grade: A
    status: Excellent
    comment: Keep it up!
   
gradeB: &B 
    grade: B
    status: Good
    comment: You can do better!
      
gradeC: &C      
    grade: C
    status: Enough
    comment: It’s okay, work harder!
 
student_grade:
  Adam: *B
  Ariana: *A
  Brian: *A
  Celeste: *B
  Dean: *C
  Kiara: *C
  Tyler: *C
  Zayn: *B

In the example above, we see that  to reference the anchors that we have already made in creating anchors example, we need to use the asterisk * symbol followed by the alias of the referenced anchor.

Case example output:

 
▾ object {4}
▾ gradeA {3}
   ▾ gradeB {3}
   ▾ gradeC {3}
   
 
 ▾ student_grade {8}
   ▾ Adam {3}
    grade: B
    status: Good
    comment: You can do better!
   ▾ Ariana {3}
    grade: A
    status: Excellent
    comment: Keep it up!
   ▾ Brian {3}
    grade: A
    status: Excellent
    comment: Keep it up!
   ▾ Celeste {3}
    grade: B
    status: Good
    comment: You can do better!
   ▾ Dean {3}
    grade: C
    status: Enough
    comment: It’s okay, work harder!
   ▾ Kiara {3}
    grade: C
    status: Enough
    comment: It’s okay, work harder!
   ▾ Tyler {3}
    grade: C
    status: Enough
    Comment: It’s okay, work harder!
   ▾ Zayn {3}
    grade: B
    status: Good
    comment: You can do better!
%d bloggers like this: