Community
    • Login

    how to view the line separately where curly bracket are used

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    8 Posts 3 Posters 756 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • AbdullahAlAminA
      AbdullahAlAmin
      last edited by

      curly.PNG
      Hello, I’m a newbie using this, so the words I use here might not express the way it should goes.
      So, under the fieldMap the object are separated by curly bracket (first one is red color, I know you can identify but I’m just making sure that my message make sense). I want to see the text within each brackets in a separate line, that way I’ll be able to identify each object for editing.

      PeterJonesP 2 Replies Last reply Reply Quote 0
      • PeterJonesP
        PeterJones @AbdullahAlAmin
        last edited by

        @mynameinvein ,

        Interesting: you’ve got JSON embedded in your XML. (“You got XML wrapped around my JSON!” “Two great tastes that taste great together.” – With apologies to Reese’s.)

        If it’s just in that one place, and you have the JsonTools plugin installed, you could copy the contents of the fieldMap into a new tab, Plugins > JsonTools > Pretty-print current JSON file; or, if you have JSON Viewer plugin installed, then Plugins > JSON Viewer > Format JSON is equivalent. With either of those plugins, you could either copy the JSON entry into an empty file, format it, then copy it back into your XML, or you could just select the JSON inside the XML and then the plugin will just pretty-print the selected text. (Just don’t try to run those JSON pretty-print commands in the XML without a selection, as who knows what horrors they would inflict on the XML itself.)

        If you’ve got that a lot, then you might be able to get a regular expression to be able to do some pretty-printing of the embedded JSON, though it won’t be as good as a true JSON-enabled pretty-printer would do. Normally, I would warn against regex for JSON or for XML. But when you’ve got the two mixed, no one tool is going to be able to handle your needs. And in your case, you seem to have the <fieldMap>...</fieldMap> as a consistent wrapper, so it might work in these limited situations. To do this, you would have to use at least one (and maybe two) of our generic regex formulas – specifically, replace in a specific zone, with the start-of-zone (BSR) being <fieldMap, end-of-zone (ESR) being </fieldMap; to keep things simple, it would be easiest (but not perfectly pretty-printed) to set your text-to-replace (FR) to { and your replacement-text (RR) to \r\n\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20{ – that would at least come close to what you want. (I used \x20 to indicate spaces, because that works better in the Forum for you to copy/paste; in the regex, you could use either \x20 or the literal space character for each of those to do the indentation.)

        AbdullahAlAminA 1 Reply Last reply Reply Quote 4
        • Mark OlsonM
          Mark Olson
          last edited by

          JsonTools version 5.5. or newer has a Select every valid JSON in selection command that you might find useful.

          However, while JsonTools can handle comments and many kinds of errors in general, it cannot parse JSON with errors when that command is running. Here’s an example:

          b64e9477-46d0-41db-a65f-638b19378baf-image.png

          and here’s the text of that file, so you can experiment with it:

          <foo>
              <bar baz="1">
                  {"is this json": true,
                  "can jsontools find it with \"Select every valid JSON in selection\"": "yes",
                  "what is the lowest version that can?": 5.5}
              </bar>
              <bar baz="2">
                  {"this is json too": "yep!",
                  // but this is a comment
                  "and jsontools can't find this with \"Select every valid JSON in selection\"": "sadly, no."}
              </bar>
              <bar baz="3">
              [
                  "some more json",
                  null,
                  1,
                  -2.5
              ]
              </bar>
          </foo>
          
          PeterJonesP AbdullahAlAminA 2 Replies Last reply Reply Quote 2
          • PeterJonesP
            PeterJones @AbdullahAlAmin
            last edited by

            @mynameinvein ,

            Alternately, if you have the PythonScript plugin installed, along with JsonTools plugin, you can run the following script, and it will run the “Pretty-print” command on the contents in each of the <fieldMap>...</fieldMap> entries in the active file. (See this FAQ for instructions on how to use the script I’m showing in the PythonScript plugin.)

            # encoding=utf-8
            """in response to https://community.notepad-plus-plus.org/topic/25394/
            
            Runs a plugin command on each match.
            The RunPluginCommandOnEachMatch() call at the end takes three arguments:
            - a r'' string containing the regex to use
            - a plugin name (must match the menu spelling exactly)
            - an action from that plugin's sub-menu (must match the menu spelling exactly)
            
            Defaults to acting on what's in between <fieldMap>...</fieldMap>,
            using the JsonTools > Pretty-print current JSON file action
            Edit that call to get
            """
            from Npp import editor,notepad,console
            import re
            
            class RunPluginCommandOnEachMatch(object):
                def cb(self, m):
                    editor.setSel(m.start(0), m.end(0))
                    notepad.runPluginCommand(self.strPlugin, self.strCommand)
                    self.startPos = m.end(0)
                    editor.setSel(self.startPos,self.startPos)
            
                def __init__(self, rawRE, strPlugin, strCommand):
                    self.strPlugin = strPlugin
                    self.strCommand = strCommand
                    self.startPos = 0
                    oldStart = editor.getSelectionStart()
                    oldEnd = editor.getSelectionEnd()
                    editor.beginUndoAction()
                    while self.startPos < editor.getLength():
                        editor.research(rawRE, self.cb, re.MULTILINE, self.startPos, -1, 1);
                    editor.endUndoAction()
            
            RunPluginCommandOnEachMatch(r'(?s)(?<=<fieldMap>).*?(?=</fieldMap>)', 'JsonTools', 'Pretty-print current JSON file')
            

            When I had the test file

            <blah>blah</blah>
            <fieldMap>[{"deep":{"first":"a","fourth":null,"second":2,"third":false},"df":"key","ff":"id"},{"deep":{"first":1,"fourth":null,"second":"b","third":true},"df":"num","ff":"session"}]</fieldMap>
            <blah>blah</blah>
            <fieldMap>[{"deep":{"first":"a","fourth":null,"second":2,"third":false},"df":"key","ff":"id"},{"deep":{"first":1,"fourth":null,"second":"b","third":true},"df":"num","ff":"session"}]</fieldMap>
            <blah>blah</blah>
            <fieldMap>[{"deep":{"first":"a","fourth":null,"second":2,"third":false},"df":"key","ff":"id"},{"deep":{"first":1,"fourth":null,"second":"b","third":true},"df":"num","ff":"session"}]</fieldMap>
            <blah>blah</blah>
            <fieldMap>[{"deep":{"first":"a","fourth":null,"second":2,"third":false},"df":"key","ff":"id"},{"deep":{"first":1,"fourth":null,"second":"b","third":true},"df":"num","ff":"session"}]</fieldMap>
            

            and ran the script, I ended up with

            <blah>blah</blah>
            <fieldMap>[
                {
                    "deep": {
                        "first": "a",
                        "fourth": null,
                        "second": 2,
                        "third": false
                    },
                    "df": "key",
                    "ff": "id"
                },
                {
                    "deep": {
                        "first": 1,
                        "fourth": null,
                        "second": "b",
                        "third": true
                    },
                    "df": "num",
                    "ff": "session"
                }
            ]</fieldMap>
            <blah>blah</blah>
            <fieldMap>[
                {
                    "deep": {
                        "first": "a",
                        "fourth": null,
                        "second": 2,
                        "third": false
                    },
                    "df": "key",
                    "ff": "id"
                },
                {
                    "deep": {
                        "first": 1,
                        "fourth": null,
                        "second": "b",
                        "third": true
                    },
                    "df": "num",
                    "ff": "session"
                }
            ]</fieldMap>
            <blah>blah</blah>
            <fieldMap>[
                {
                    "deep": {
                        "first": "a",
                        "fourth": null,
                        "second": 2,
                        "third": false
                    },
                    "df": "key",
                    "ff": "id"
                },
                {
                    "deep": {
                        "first": 1,
                        "fourth": null,
                        "second": "b",
                        "third": true
                    },
                    "df": "num",
                    "ff": "session"
                }
            ]</fieldMap>
            <blah>blah</blah>
            <fieldMap>[
                {
                    "deep": {
                        "first": "a",
                        "fourth": null,
                        "second": 2,
                        "third": false
                    },
                    "df": "key",
                    "ff": "id"
                },
                {
                    "deep": {
                        "first": 1,
                        "fourth": null,
                        "second": "b",
                        "third": true
                    },
                    "df": "num",
                    "ff": "session"
                }
            ]</fieldMap>
            

            I think this is a reasonble solution to your problem.

            Mark OlsonM 1 Reply Last reply Reply Quote 2
            • PeterJonesP PeterJones referenced this topic on
            • PeterJonesP
              PeterJones @Mark Olson
              last edited by

              @Mark-Olson ,

              Nice. That’s even simpler for this particular need than doing my script. It’s almost like you’ve seen other instances of JSON-in-XML or something. :-) I tried it using my example data, and it worked similarly to my script (but doesn’t require confusing the OP with the PythonScript plugin).

              (Still, I’m glad I published my version, because it is actually a general solution for running any plugin’s command with each match as a selection, which may be useful for other situations than just this exact problem.)

              1 Reply Last reply Reply Quote 2
              • AbdullahAlAminA
                AbdullahAlAmin @PeterJones
                last edited by

                @PeterJones Thank you, this one worked for me. As a new user, I must say it was easy to follow.

                1 Reply Last reply Reply Quote 1
                • AbdullahAlAminA
                  AbdullahAlAmin @Mark Olson
                  last edited by AbdullahAlAmin

                  @Mark-Olson Thank you, I didn’t have the time to try this (I promise to try), but really appreciate this. You’re the author of JSON Tools, right? it’s great to meet you.

                  1 Reply Last reply Reply Quote 1
                  • Mark OlsonM
                    Mark Olson @PeterJones
                    last edited by

                    @PeterJones
                    FWIW, if all you want is to pretty-print or compress the results of a regex search with PythonScript, you could replace the call to JsonTool’s pretty-print faculty with a quite simple script using Python’s json library.

                    import json
                    from Npp import editor
                    
                    PRETTY_PRINT_INDENT = 4
                    
                    class CompressOrPrettyPrintRegexSearchResult:
                        def __init__(self, compress):
                            self.compress = compress
                    
                        def callback(self, m):
                            parsed_group_2 = json.loads(m.group(2))
                            indent = None if self.compress else PRETTY_PRINT_INDENT 
                            formatted_group_2 = json.dumps(parsed_group_2, indent=indent)
                            return m.group(1) + formatted_group_2 + m.group(3)
                    
                    if __name__ == '__main__':
                        try:
                            COPPRSR
                        except NameError:
                            COPPRSR = CompressOrPrettyPrintRegexSearchResult(True)
                        editor.rereplace('(?s-i)(<fieldMap[^<>]*>)(.*?)(</fieldMap>)', lambda m: COPPRSR.callback(m))
                        # this will toggle between compressing and pretty-printing
                        COPPRSR.compress = not COPPRSR.compress
                    
                    1 Reply Last reply Reply Quote 2
                    • First post
                      Last post
                    The Community of users of the Notepad++ text editor.
                    Powered by NodeBB | Contributors