jsonparsingnotepad++

How to reformat JSON in Notepad++


I need Notepad++ to take a json string from this

{"menu": {"id": "file","value": "File","popup": {"menuitem": [{"value": "New", "onclick": "CreateNewDoc()"},{"value": "Open", "onclick": "OpenDoc()"},{"value": "Close", "onclick": "CloseDoc()"}]}}}

to this...

{"menu": {
  "id": "file",
  "value": "File",
  "popup": {
    "menuitem": [
      {"value": "New", "onclick": "CreateNewDoc()"},
      {"value": "Open", "onclick": "OpenDoc()"},
      {"value": "Close", "onclick": "CloseDoc()"}
    ]
  }
}}

I looked around at all the TextFX options but couldn't find anything that worked.


Solution

  • Use JSTool

    As of Notepad++ v7.6, you can use the Plugin Admin option in Notepad++ to install JSTool.

    Manual Install

    1. Download it from https://www.sunjw.us/jstool/npp/#download
    2. Copy JSMinNpp.dll to the plugin directory of Notepad++. You may need to create a directory named "JSMinNPP" in the plugin directory after version 7.6.3

    Here's an example of JSON that's been formatted using the tool:

    {
      "menu" : {
        "id" : "file",
        "value" : "File",
        "popup" : {
          "menuitem" : [{
          "value" : "New",
              "onclick" : "CreateNewDoc()"
            }, {
              "value" : "Open",
              "onclick" : "OpenDoc()"
            }, {
              "value" : "Close",
              "onclick" : "CloseDoc()"
            }
          ]
        }
      }
    }
    

    enter image description here

    Tip: Select the code you want to reformat, then Plugins | JSTool | JSFormat.