Skip to main content

What does the colon before the name mean in this JSON like string? [Resolved]

I encountered this string that looks like JSON syntax but I've never seen a property with a colon before it and not with a backwards greater than or equals sign:

Badge {

Is it a JSON string and if so what does the colon do?

Question Credit: 1.21 gigawatts
Question Reference
Asked April 16, 2019
Posted Under: Programming
2 Answers

Colon variable refers to :abc type variables you might have seen in Ruby. They are called Ruby symbols.

A Ruby symbol is not a variable because it cannot be assigned a value.

Also, a Ruby symbol is not a reference to another variable nor is it a pointer to a memory location.

Still a symbol has a value and whenever the name of the symbol is same, its value is the same.

An example will make it more clear.

# It is trivial to assign a value to a variable.
abc = "1"
=> "1"

# But a symbol cannot be assigned any value.
:a = "1"
# SyntaxError: A symbol cannot be assigned a value

# Can use a variable as a map-key 
m = {abc => "1"}
=> {"1"=>"1"}

# Can use a string as a map-key 
=> {"def"=>"1"}

# Can also use a symbol as a map-key 
m = {:a => "1"}
=> {:a=>"1"}

# Can use same symbol as key in another map
m2 = {:a => "2"}
=> {:a=>"2"}

# And it won't affect the previous map.
=> {:a=>"1"}

=> {:a=>"2"}

credit: Purvi Barot
Answered April 16, 2019
Your Answer