ChatGPT解决这个技术问题 Extra ChatGPT

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.

For 32-bit version, see stackoverflow.com/a/65762242.

s
scw

Update: As of Notepad++ v7.6, use Plugin Admin to install JSTool per this answer

INSTALL

Download it from http://sourceforge.net/projects/jsminnpp/ and copy JSMinNpp.dll to plugin directory of Notepad++. Or you can just install "JSTool" from Plugin Manager in Notepad++.

New Notepad++ install and where did PluginManager go? See How to view Plugin Manager in Notepad++

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

https://i.stack.imgur.com/disZy.png


Seems to be listed as JSTool now
Thanks. I checked. I ended up using JSONViewer as it is lets format the JSON data and not just view it in nested tree format.
JSTool can also format the JSON data. Just press Ctrl-Alt-M. I tried a few plugins now and I find this is the best one.
This can be installed directly from the built in "Plugin Manager" in Notepad++. Go "Plugins > Plugin Manager > Show Plugin Manager > Available tab"
A 64-bit version can be downloaded directly from here: notepad-plus-plus.org/community/topic/13064/…
H
Hardik Leuwa

For Notepad++ v.7.6 and above Plugins Admin... is available.

Open Menu Plugins > Plugins Admin... Search JSON Viewer Check JSON Viewer in List Click on Install Button Restart Notepad++ Select JSON text Go to Plugins > JSON Viewer > Format JSON ( Ctrl + Alt + Shift + M )

We can install any Notepad++ supported plugins using Plugins Admin...


This is a very simple technique. Thanks. I just want to add in order to format after installing the plugin one needs 1) Select JSON objects 2) Go to plugins ** 3)**JSON Viewer accordion 4) Lastly, Format JSON. OR Just hit Cntrl + Shift + Alt + M
plugins > Plugins admin no longer works in the current version.
@Michael Tuchman, May i know in which version Plugins > Plugins Admin... not work?
I have test Plugins > Plugins Admin... by JSON Viewer in latest version Notepad++ v7.8 and its working properly
@Michael Tuchman, I have tested Plugins Admin.. in Notepad ++ 7.6.6 (64 bit) by this same JSON sample asked in question. and its working properly.
D
Dan Atkinson

You require a plugin to format JSON. To install the plugin do the following steps:

Open notepad++ -> ALT+P -> Plugin Manager -> Selcet JSON Viewer -> Click Install Restart notepad++ Now you can use shortcut to format json as CTRL + ALT +SHIFT + M or ALT+P -> Plugin Manager -> JSON Viewer -> Format JSON

https://i.stack.imgur.com/eOrtv.png


really? picture which mainly empty? :-/
This format algorithm is not robust and fails often, the JSTool (see answer of ibr) is much more robust.
@Stefan I can confirm. JSTool was able to parse log structured output from Serilog (.NET Core 6).
C
Community

I personally use JSON Viewer since the Notepad++ plugin doesn't work any more.

EDIT - 24th May 2012 I advise that you download the JSMin plugin for Notepad as mentioned in the answer. This works well for me in the latest version (v6.1.2 at time of writing). EDIT - 7th November 2017 As per @danday74's comment below, JSMin is now JSToolNpp. Also, please be aware that the JSON Viewer tool is on Codeplex which will likely disappear in the near future.

Given the above, this answer is no longer relevant and you should use Dan H's answer instead. My answer is simply here for posterity.


It works when I do Plugins -> JSMin -> JSMin (New File) and then JSFormat
@danday74 Thanks! I've modified the answer accordingly.
JSON Viewer works fine on np++ version 7.5.1, available in the plugin manager.
@DanAtkinson Thanks for keeping your answer updated. I appreciate knowing that answers to old questions haven't gotten stale. :)
still working on NotePad++ v7.8.2 Json viewer v1.34.0.0
P
Pang

Universal Indent GUI plugin for Notepad++ will turn your sample into:

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

unfortunately, this does not work with the newest Unicode version of Notepad++ 5.5
R
R15

As per the latest notepad++, updated answer. Install JSON Viewer

Open notepad++ go to Plugins --> click Plugins Admin..

https://i.stack.imgur.com/M25DM.png

In Plugins Admin window search for JSON Viewer and click on Install

https://i.stack.imgur.com/mLg5P.png

New after installing plugin, this is how you can view file in JSON format

https://i.stack.imgur.com/HMZ2d.png


i
ibr

simply go to this link download the dll copy and paste the dll to the plugins folder at notepad++, \Notepad++\plugins restart the notepad++, and it should be shown in the list

https://i.stack.imgur.com/Phc16.png

NOTE: this dll supports 64 bit notepade++


M
Millhouse

It's not an NPP solution, but in a pinch, you can use this online JSON Formatter and then just paste the formatted text into NPP and then select Javascript as the language.


@Brent.Longborough Thanks for that little nugget of advice! The JS highlighting has a weird gray background, python highlighting is much easier on the eyes.
Online json formatters are good but sometimes you don't want to risk sending the data you're formatting across the Internet.
U
User0106

If formatting JSON is the main goal and you have VisualStudio then it is simple and easy.

Open Visual Studio File -> New -> File Select Web in left side panel Select JSON Copy paste your raw JSON value Press Ctrl + K and Ctrl + D

That's it. you will get formatted JSON Value.


The question is about Notepad++
I was searching tool for format JSON values and found this link. Later I found Visual studio steps. It may useful for someone who is looking for formatting JSON values.
I usually resort to using this since my developer environment is reset from contract to contact. I just paste it into any .json, select all, copy, then undo.
N
Nitin Lawande

Steps to add JSON viewer plugin for notepad++:

Download JSON viewer plugin for notepad++ from sourceforge. Copy NPPJSONViewer.dll into Notepad++\plugins\NPPJSONViewer folder restart the notepad++ select JSON and click on plugins > JSON Viewer > Format JSON (CTRL+ALT+SHIFT+M). Done.


Notepad++ says that plugin is not compatible with 64bit version of notepad , do we have any 64bit plugin ?
S
SUN

JSMinNpp plugin will do this job. https://sourceforge.net/projects/jsminnpp/


S
Siva

The following Notepad++ plugin worked for me as suggested by "SUN" https://sourceforge.net/projects/jsminnpp/


E
Esko Piirainen

If you don't want to install a Notepad++ plugin but you have Firefox and a JSON plugin for Firefox, you can select Run -> Launch in Firefox. You get the contents formatted as JSON using your Firefox plugin.

This is what I personally do.


o
olliaroa

You can view in Notepad++ no problem now (maybe older versions were bugged?)

for win64: You can find the latest plugin here: https://github.com/kapilratnani/JSON-Viewer/releases . The latest zip file contains a .dll file.

And then follow the github priject README instructions:

Paste the file "NPPJSONViewer.dll" to Notepad++ plugin folder open a document containing a JSON string Select JSON fragment and navigate to plugins/JSON Viewer/show JSON Viewer or press "Ctrl+Alt+Shift+J" Voila!! if the JSON is valid, it will be shown in a Treeview

It should be the same process for win32 but I cannot personally verify it.


R
Richard Lloyd

It worked for me in the latest edition to Notepad using the UniversalIndentGui.

What I did was under the plugin setting choose Enable Text Auto Update, a window popped up and I selected javascript.


D
DevBezz

Notepad 5.8.7 and jsmin 1.7.0.0 works wonderful here.

Be careful though, found out jsmin eats the comments the hard way (should have read first).


S
Stonetip

I'm using the JSON Viewer plug-in with NPP 5.9 and it seems to work well.


J
Josh

I know this thread is old but I recently ran into a problem with JSToolNPP not being compatible with my newly updated N++, I did find a replacement that seems to work. http://sourceforge.net/projects/nppjsonviewer/

Use at your own risk, ofc. (standard disclaimer from me when linking anything outside the SExchange, fyi)


j
jslmsca

For those of us behind a corporate firewall with no direct access to the internet, using the Plugins Admin won't work. To use the JSMinNpp plugin, you can't just "copy the dll to the plugins folder". It needs to live inside a folder called "JSMinNpp" inside the plugins folder. After doing that and restarting Notepad++, I was able to see the "JSTool" menu option under the Plugins menu.

Tested with Notepad++ 7.8.2 and 7.8.9.


H
Henke

I use 32-bit Notepad++ version 7.5.6. I have found that 32-bit JSToolNpp 1.20.0 does a great job.
The direct link is:
https://sourceforge.net/projects/jsminnpp/files/Uni/JSToolNPP.1.2006.0.uni.32.zip/download
which is redirected from https://sourceforge.net/projects/jsminnpp/.
VirusTotal link:
https://www.virustotal.com/gui/file/008ee0ce889dfd9e96b975cebe6faafe28bc350352e951f3dec97e8e5bec5a07

JSON-Viewer works fine too, but cannot sort the JSON data.


A
Ahmed Magdy

You can use http://www.jsonlint.com/ to edit your json online if you don't have Notepad++.


Directly from N++? (-1)
upvoted. this link was usefull after the plugin wasnt accepted due to its 32bit version.
Upvoted. Provides a correct solution even though not through notepad.
For 32-bit version, I suggest taking a look at stackoverflow.com/a/65762242.
C
Cody Caughlan

I know you asked about NotePad++ but TextMate for OS X can do it via the JSON bundle, its called the "Reformat Document" command.


s
shA.t

Your best bet is to use one of the latest versions of Eclipse (I am using Eclipse Galileo J2EE and Eclipse Ganymede J2EE). Create a JavaScript file, then create a variable:

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

Lastly, hit CTRL+SHIFT+F and voila! You have a nicely indented JSON Object. I, too, am looking for a Notepad++ JSON formatter, and I very well may be forced to develop an Npp plugin some short time in the future.


If you have Visual Studio, simply paste the json text into a new file and it will be formatted automatically.
I, too, am looking for a Notepad++ JSON formatter, If you have same question please don't add an answer like this and keep SO clean ;).