jsonvimjson-value

Export a Vimscript Dictionary as JSON


I have a found a plugin that enables Vim to parse JSON. I need to export VimScript dictionaries as JSON. Currently I am just using:

let str = string(dict)
substitute(str, "'", '"', 'g')

This is working but is bound to break when I run into dictionaries with embedded quotes. What is a better way?


Solution

  • Since Vim 7.4.1304 (so definitely available with Vim 8.0), these functions are built into Vim as json_encode() and json_decode().

    For backwards compatibility with even older Vim versions, the WebAPI.vim plugin has a JSON parser and encoder implemented in pure Vimscript:

    :let jsonString = webapi#json#encode({...})