Skip to content

Instantly share code, notes, and snippets.

@anazawa
Last active March 11, 2025 14:38
Show Gist options
  • Save anazawa/7834b0f789ad814beed8 to your computer and use it in GitHub Desktop.
Save anazawa/7834b0f789ad814beed8 to your computer and use it in GitHub Desktop.
JSON representation of SGF (Smart Go Format)

Basic Structure

SGF's Node is JSON's object

A node is an object whose keys are property names and whose values are the corresponding values. The value types are defined later.

SGF:

;FF[4]GM[1]SZ[19]

JSON:

{
    "FF": 4,
    "GM": 1,
    "SZ": 19
}

SGF's Sequence is a list of objects (Nodes)

SGF:

;FF[4]GM[1]SZ[19];B[aa];W[bb]

JSON:

{
    "FF": 4,
    "GM": 1,
    "SZ": 19
},
{
    "B": "aa"
},
{
    "W": "bb"
}

SGF's GameTree is JSON's array of objects (Nodes)

SGF:

(;FF[4]GM[1]SZ[19];B[aa];W[bb])

JSON:

[
    {
        "FF": 4,
        "GM": 1,
        "SZ": 19
    },
    {
        "B": "aa"
    },
    {
        "W": "bb"
    }
]

SGF's Collection is JSON's array of arrays (GameTrees)

SGF:

(;FF[4]GM[1]SZ[19];B[aa];W[bb])
(;FF[4]GM[1]SZ[19];B[cc];W[dd])

JSON:

[
    [
        {
            "FF": 4,
            "GM": 1,
            "SZ": 19
        },
        {
            "B": "aa"
        },
        {
            "W": "bb"
        }
    ],
    [
        {
            "FF": 4,
            "GM": 1,
            "SZ": 19
        },
        {
            "B": "cc"
        },
        {
            "W": "dd"
        }
    ]
]

Node can contain variations

A node can contain a special property named variations whose value is an array of arrays (game trees).

SGF:

(;FF[4]C[root](;C[a];C[b])(;C[c];C[d]))

JSON:

[
    {
        "FF": 4,
        "C": "root",
        "variations": [
            [
                {
                    "C": "a"
                },
                {
                    "C": "b"
                }
            ],
            [
                {
                    "C": "c"
                },
                {
                    "C": "d"
                }
            ]
        ]
    }
]

Value Types

SGF's number, double or real is JSON's number

SGF:

;GM[1]KM[6.5]HA[9]

JSON:

{
    "GM": 1,
    "KM": 6.5,
    "HA": 9
}

SGF's text or simpletext is JSON's string

SGF:

;C[hello]

JSON:

{
    "C": "hello"
}

SGF's move or point is JSON's string

SGF:

;B[aa]

JSON:

{
    "B": "aa"
}

SGF's compressed point list is JSON's array of strings (moves/points)

A compressed point list is an array of strings (moves/points). The compressed list must be always expanded (XXX)

SGF:

;AB[aa:bb]

JSON:

{
    "AB": [ "aa", "ab", "ba", "bb" ]
}

Etc

SGF:

(;LB[aa:label];AP[CGoban:1.6.2];LN[aa:bb];AR[aa:bb])

JSON:

{
    "LB": [ "aa", "label" ],
    "AP": [ "CGoban", "1.6.2" ],
    "LN": [ "aa", "bb" ],
    "AR": [ "aa", "bb" ]
}

See Also

Author

Copyright Ryo Anazawa, 2014.

License

This document is licensed under the Creative Commons license by-sa.

@deebloo
Copy link

deebloo commented Mar 11, 2025

thank you! this will help me improve my parser for https://github.com/deebloo/go-board

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment