Synonyms via JSON

While writing last week’s post on the built-in Apple thesaurus, I looked around for other ways to get a list of synonyms. I came across the Merriam-Webster API, which gives you free access (for light non-commercial use) to the M-W dictionary and thesaurus. I didn’t spend too much time with it—the Apple thesaurus is just so handy—but it was interesting, and M-W is certainly authoritative.

To use the M-W API, you have to register for a developer account. I don’t understand why they ask for a company name when it’s for non-commercial use (I entered “None”), but maybe the registration page covers commercial apps, too. Whatever. I got my API key almost immediately after submitting the form.

The thesaurus API works by returning JSON in response to a URL in this form:

https://www.dictionaryapi.com/api/v3/references/thesaurus/json/word?key=your-api-key

There’s extensive documentation for the API, but I just sort of looked through some output to see how easy it would be to extract what I might want. Here’s the JSON output for component. Get your scrolling finger limbered up.

[
  {
    "meta": {
      "id": "component",
      "uuid": "5989b740-1791-4d9f-9ce1-d4a8f6a49f11",
      "src": "coll_thes",
      "section": "alpha",
      "target": {
        "tuuid": "60760bc0-146a-4f7c-b424-debfb361e283",
        "tsrc": "collegiate"
      },
      "stems": [
        "component",
        "componential",
        "components"
      ],
      "syns": [
        [
          "building block",
          "constituent",
          "element",
          "factor",
          "ingredient",
          "member"
        ]
      ],
      "ants": [
        [
          "whole"
        ]
      ],
      "offensive": false
    },
    "hwi": {
      "hw": "component"
    },
    "fl": "noun",
    "def": [
      {
        "sseq": [
          [
            [
              "sense",
              {
                "dt": [
                  [
                    "text",
                    "one of the parts that make up a whole "
                  ],
                  [
                    "vis",
                    [
                      {
                        "t": "each set is composed of several distinct {it}components{/it}"
                      }
                    ]
                  ]
                ],
                "syn_list": [
                  [
                    {
                      "wd": "building block"
                    },
                    {
                      "wd": "constituent"
                    },
                    {
                      "wd": "element"
                    },
                    {
                      "wd": "factor"
                    },
                    {
                      "wd": "ingredient"
                    },
                    {
                      "wd": "member"
                    }
                  ]
                ],
                "rel_list": [
                  [
                    {
                      "wd": "basis"
                    },
                    {
                      "wd": "part and parcel"
                    }
                  ],
                  [
                    {
                      "wd": "detail"
                    },
                    {
                      "wd": "item"
                    },
                    {
                      "wd": "particular"
                    },
                    {
                      "wd": "point"
                    }
                  ],
                  [
                    {
                      "wd": "aspect"
                    },
                    {
                      "wd": "characteristic"
                    },
                    {
                      "wd": "facet"
                    },
                    {
                      "wd": "feature"
                    },
                    {
                      "wd": "trait"
                    }
                  ],
                  [
                    {
                      "wd": "division"
                    },
                    {
                      "wd": "fragment"
                    },
                    {
                      "wd": "particle"
                    },
                    {
                      "wd": "partition"
                    },
                    {
                      "wd": "piece"
                    },
                    {
                      "wd": "portion"
                    },
                    {
                      "wd": "section"
                    },
                    {
                      "wd": "sector"
                    },
                    {
                      "wd": "segment"
                    }
                  ],
                  [
                    {
                      "wd": "subcomponent"
                    }
                  ]
                ],
                "near_list": [
                  [
                    {
                      "wd": "aggregate"
                    },
                    {
                      "wd": "composite"
                    },
                    {
                      "wd": "compound"
                    },
                    {
                      "wd": "mass"
                    }
                  ],
                  [
                    {
                      "wd": "entirety"
                    },
                    {
                      "wd": "sum"
                    },
                    {
                      "wd": "summation"
                    },
                    {
                      "wd": "total"
                    },
                    {
                      "wd": "totality"
                    }
                  ],
                  [
                    {
                      "wd": "admixture"
                    },
                    {
                      "wd": "amalgam"
                    },
                    {
                      "wd": "amalgamation"
                    },
                    {
                      "wd": "blend"
                    },
                    {
                      "wd": "combination"
                    },
                    {
                      "wd": "intermixture"
                    },
                    {
                      "wd": "mix"
                    },
                    {
                      "wd": "mixture"
                    }
                  ]
                ],
                "ant_list": [
                  [
                    {
                      "wd": "whole"
                    }
                  ]
                ]
              }
            ]
          ]
        ]
      }
    ],
    "shortdef": [
      "one of the parts that make up a whole"
    ]
  },
  {
    "meta": {
      "id": "component",
      "uuid": "0bb8d43d-b0c8-4869-81e7-535094385289",
      "src": "CTcompile",
      "section": "alpha",
      "stems": [
        "component"
      ],
      "syns": [
        [
          "constituent",
          "individual",
          "particular",
          "cross-sectional",
          "divisional",
          "fragmentary",
          "partial",
          "local",
          "localized",
          "regional",
          "sectional"
        ]
      ],
      "ants": [
        [
          "across-the-board",
          "blanket",
          "broad-brush",
          "common",
          "general",
          "generic",
          "global",
          "overall",
          "universal",
          "all-embracing",
          "broad",
          "broad-gauge",
          "broadscale",
          "comprehensive",
          "extensive",
          "inclusionary",
          "overarching",
          "pervasive",
          "sweeping",
          "ubiquitous",
          "wholesale",
          "wide",
          "widespread",
          "aggregate",
          "collective",
          "complete",
          "full",
          "plenary"
        ]
      ],
      "offensive": false
    },
    "hwi": {
      "hw": "component"
    },
    "fl": "adjective",
    "def": [
      {
        "sseq": [
          [
            [
              "sense",
              {
                "dt": [
                  [
                    "text",
                    "as in {it}constituent{/it}"
                  ]
                ],
                "sim_list": [
                  [
                    {
                      "wd": "constituent"
                    }
                  ],
                  [
                    {
                      "wd": "individual"
                    },
                    {
                      "wd": "particular"
                    }
                  ],
                  [
                    {
                      "wd": "cross-sectional"
                    },
                    {
                      "wd": "divisional"
                    },
                    {
                      "wd": "fragmentary"
                    },
                    {
                      "wd": "partial"
                    }
                  ],
                  [
                    {
                      "wd": "local"
                    },
                    {
                      "wd": "localized"
                    },
                    {
                      "wd": "regional"
                    },
                    {
                      "wd": "sectional"
                    }
                  ]
                ],
                "opp_list": [
                  [
                    {
                      "wd": "across-the-board"
                    },
                    {
                      "wd": "blanket"
                    },
                    {
                      "wd": "broad-brush"
                    },
                    {
                      "wd": "common"
                    },
                    {
                      "wd": "general"
                    },
                    {
                      "wd": "generic"
                    },
                    {
                      "wd": "global"
                    },
                    {
                      "wd": "overall"
                    },
                    {
                      "wd": "universal"
                    }
                  ],
                  [
                    {
                      "wd": "all-embracing"
                    },
                    {
                      "wd": "broad"
                    },
                    {
                      "wd": "broad-gauge",
                      "wvrs": [
                        {
                          "wvl": "or",
                          "wva": "broad-gauged"
                        }
                      ]
                    },
                    {
                      "wd": "broadscale"
                    },
                    {
                      "wd": "comprehensive"
                    },
                    {
                      "wd": "extensive"
                    },
                    {
                      "wd": "inclusionary"
                    },
                    {
                      "wd": "overarching"
                    },
                    {
                      "wd": "pervasive"
                    },
                    {
                      "wd": "sweeping"
                    },
                    {
                      "wd": "ubiquitous"
                    },
                    {
                      "wd": "wholesale"
                    },
                    {
                      "wd": "wide"
                    },
                    {
                      "wd": "widespread"
                    }
                  ],
                  [
                    {
                      "wd": "aggregate"
                    },
                    {
                      "wd": "collective"
                    },
                    {
                      "wd": "complete"
                    },
                    {
                      "wd": "full"
                    },
                    {
                      "wd": "plenary"
                    }
                  ]
                ]
              }
            ]
          ]
        ]
      }
    ],
    "shortdef": [
      "as in constituent"
    ]
  }
]

That output came from running

curl https://dictionaryapi.com/api/v3/references/thesaurus/json/component?key=my-key | jq

Piping the output through jq put it into the nicely indented format. Without that, you just get a long inpenetrable string.

It was relatively easy to whip up a short Python script that turned the JSON into a more compact form:

 1  #!/usr/bin/env python3
 2  
 3  import json
 4  import requests
 5  from textwrap import fill
 6  import sys
 7  
 8  word = sys.argv[1]
 9  myKey = 'my-key'
10  mwURL = f'https://dictionaryapi.com/api/v3/references/thesaurus/json/{word}?key={myKey}'
11  
12  r = requests.get(mwURL)
13  j = json.loads(r.text)
14  
15  for m in j:
16    count = len(m['shortdef'])
17    for i, defn in enumerate(m['shortdef']):
18      print(fill(m['fl'] + ': ' + defn, width=70))
19      print(fill(', '.join(m['meta']['syns'][i]), width=70))
20      print()

There are no comments because this was just a quick proof of concept. Basically, it uses requests to get the API’s response, decodes the JSON into a Python data structure using the json module’s loads function, and then pulls out the parts of the response that I thought would be useful. Because the output for each part could be long, I used the fill function from the textwrap library, to make the lines no more than 70 characters long. The output for component is

noun: one of the parts that make up a whole
building block, constituent, element, factor, ingredient, member

adjective: as in constituent
constituent, individual, particular, cross-sectional, divisional,
fragmentary, partial, local, localized, regional, sectional

How it is that part can appear in the short definition but not in this list of synonyms is beyond me. Maybe it’s good that I didn’t pursue this further.

Update 2 Apr 2025 2:25 PM
Leon Cowle tells me that requests has a json method that does the work of loads. That sounds familiar, but it’s been so long since I used requests that I didn’t even bother to look for it. When I want to limit the time I spend on a script, I typically stick with what’s already in my brain and don’t go looking for improvements and efficiencies. With luck, though, this will remind me of json the next time I use requests. Thanks, Leon!