monkey intern
monkey intern

Reputation: 726

How to delete all data of a json document except the key/value I want

So I have a rather big document with entries like:

"kff126d":"Infinity"

And others like:

"kff126d":"0.03702955"

And a few dozens of other keys like kff126e and so on, and I would like to remove everything that isn't the one key I want to keep, and whatever value is inside the second set of quotation marks, so I can take a look at abnormal values.

Could someone help me out? I've tried looking arround but it's quite complex compared to the few things I've done with regex before this. Thanks in advance.

Upvotes: 1

Views: 5053

Answers (3)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626738

I can suggest the following regex:

("kff126d":"[^"]*")|(?:(?!"kff126d":).)+

with . matches newline option ON, and replace with $1\n.

See the regex demo

This will insert redundant empty lines that you can remove with Edit > Line Operations > Remove Empty Lines.

The point is to match and capture with (...) the part that you need to keep, and remove all the rest. All the rest can be matched with a tempered greedy token (?:(?!"kff126d":).)+ (matches any character (with .) that does not start the "kff126d": sequence).

enter image description here
                                                                                  V
enter image description here

And here is the screen of the replacement result with your updated sample data:

enter image description here

Upvotes: 3

Ro Yo Mi
Ro Yo Mi

Reputation: 15000

Description

Using Notepad++:

  • find all characters that is not the desired key name
  • which are also not the value Infinity
  • work with json strings which are not neatly padded, aka compressed json
  • leave behind a vertical column of values

The Regex

Find What:

.*?(?:"kff126e":"((?!Infinity)[^"]*)"|\Z)

Replace with:

$1\n

How To

From Notepad++

  1. press the ctrlh to enter the find and replace mode

  2. Select the Regular Expression option

  3. Select the ". matches newline" option

  4. In the "Find what" field place the following regex .*?(?:"kff126e":"((?!Infinity)[^"]*)"|\Z)

  5. In the Replace with field enter $1\n Click Replace All

  6. Goto pub for favorite beverage.

Explanation

Regular expression visualization

NODE                     EXPLANATION
----------------------------------------------------------------------
  .*?                      any character (0 or more times (matching
                           the least amount possible))
----------------------------------------------------------------------
  (?:                      group, but do not capture:
----------------------------------------------------------------------
    "kff126e":"              '"kff126e":"'
----------------------------------------------------------------------
    (                        group and capture to \1:
----------------------------------------------------------------------
      (?!                      look ahead to see if there is not:
----------------------------------------------------------------------
        Infinity                 'Infinity'
----------------------------------------------------------------------
      )                        end of look-ahead
----------------------------------------------------------------------
      [^"]*                    any character except: '"' (0 or more
                               times (matching the most amount
                               possible))
----------------------------------------------------------------------
    )                        end of \1
----------------------------------------------------------------------
    "                        '"'
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    \Z                       before an optional \n, and the end of
                             the string
----------------------------------------------------------------------
  )                        end of grouping

What's happening

The desired value will be placed into capture group 1 aka $1, which will replace the matched text. The \n inserts a return character. The regex engine will continue parsing your text after the last replace operation until it reaches the end of the file.

Upvotes: 1

JonM
JonM

Reputation: 1374

Press ctrl+h and in the find section use : (?m)^(?!"kff126d":).*?$ and leave the replace section blank. Select the search mode 'Regular Expression' and select Replace All.

This will transform

"kff126d":"0.12345"
sdfgsdfg
sgsfgsfg
"kff126d":"0.03702955"
"kff126d":"0.03702955"
rererer
"kff126d":"0.03702955"
sfdgsfgfsg
sdfgsfg
"kff126e":"0.8888888888888"

into

"kff126d":"0.12345"


"kff126d":"0.03702955"
"kff126d":"0.03702955"

"kff126d":"0.03702955"

Afterwards, if you want to remove all the blank lines, you can replace \R+ with \n

Upvotes: 1

Related Questions