-1

I apologize if there is an answer for this somewhere, but my search skills have failed me if there is.

I'm using UltraEdit and there are lines I need to remove from some JSON schemas to make comparing them easier.

So given the following:

"PaymentMethod": {
               "$id": "/properties/PaymentMethod",
                "items": {
                    "$ref": "PaymentMethod.json"
                },
                "type": "array"
            }

Using this RegEx:

^.*\".*\"\: \".*$\r\n

Selects these lines:

"$id": "/properties/PaymentMethod",
"$ref": "PaymentMethod.json"
"type": "array"

What I need to do is skip the $ref line.

I've tried to get negative lookaround to work using (?!json) in various ways with the selection criteria and have failed miserably.

The purpose of this is to delete the selected lines.

Thanks for any help.

Update: To clarify, there are lines I want to delete that match the criteria my Regex finds, but I do not want to delete the $ref line. So I was hoping to find a relatively easy way to do this using straight up perl regex within UltraEdit.

I've created a workaround with a Python script so I can get my work done, but it would still be interesting to find out if there is a way to do this. :)

  • Are you using `$id` and `$ref` as placeholders for a different string, or are they actually `$id` and `$ref`? – ikegami Aug 01 '18 at 22:42
  • This is a snippet from a JSON Schema and $id is the URI definition while $ref specifies a different JSON file to use at that point in the schema. – user1459463 Aug 02 '18 at 16:26
  • I'm not talking about the value, but the key. Is it actually the 3 chars `$`,`i`,`d` and the 4 chars `$`,`r`,`e`,`f`, or are those strings you used to anonymize the actual data – ikegami Aug 02 '18 at 16:40
  • $ref and $id and JSON Schema keywords. – user1459463 Aug 03 '18 at 18:31
  • I take that to mean `$id` and `$ref` are used, which simplifies the answer. – ikegami Aug 03 '18 at 20:25
  • Despite the fact that UltraEdit for Windows has built-in a JSON manager for high level editing JSON files since version 24.10 it is of course possible to modify the JSON file by running a Perl regular expression replace all. But your question is unclear. Please edit it and added for the posted input data block what you want as output data block after running the regular expression replace. When you want to find a block, but keep a part of the block, you need a [backreference](https://www.ultraedit.com/support/tutorials-power-tips/ultraedit/perl-regular-expressions-backreferences.html). – Mofi Aug 04 '18 at 16:05
  • For example using as search string `"PaymentMethod":.+\r\n\K([\t ]+)(?:.+\r\n){2}[\t ]+("\$ref":.+?\r\n)(?:.+\r\n){2}` and as replace string `\1\2` could work. Or perhaps easier using as search string `(?:^[\t ]+(?:"\$id|"items|},|"type").*\r\n| {5}(?="\$ref"))` and an empty string as replace string. – Mofi Aug 04 '18 at 16:22

3 Answers3

1

Don't write your own broken parser; use an existing one.

use Cpanel::JSON::XS qw( decode_json );

my $json_utf8      = '...';
my $data           = decode_json($json_utf8);
my $payment_method = $data->{PaymentMethod};
my $id             = $payment_method->{'$id'};
my $item           = $payment_method->{items}{'$ref'};
my $type           = $payment_method->{type};
ikegami
  • 367,544
  • 15
  • 269
  • 518
0

If you don't want $ref after the initial quotes:

^.*\"(?!\$ref).*\"\: \".*$\r\n 

see it here: https://regex101.com/r/yxTwck/1

or to exclude based on .json" at the end of the line:

^.*\".*\"\: \".*(?<!\.json")$\r\n

Also note that you are using greedy quantifiers (.* vs. .*?). If your idea is to have the first .* stop at the first ", you should probably use [^\n"]* which will prevent line feeds or "s from being consumed. Your regex matches """""""""""""" "type": "array", for example.

zzxyz
  • 2,953
  • 1
  • 16
  • 31
  • I'm including the end of lines because I'm actually removing these lines by replacing them with nulls. – user1459463 Aug 02 '18 at 16:28
  • @user1459463 - but you want to do that explicitly, not anywhere in your regex. See: https://regex101.com/r/AdTHuI/3 UltraEdit probably does not have SingleLine on by default (`.` matches newline), however you can disable this setting and use `[^"]` to see that "`not "`" includes newlines. Then try `.*?` and `[^"\n]*` and `[^"\n]*?` to see how the behavior changes. – zzxyz Aug 02 '18 at 17:53
0

Using a JSON module to manipulate the data directly would be a more robust solution, but a quck and dirty way to edit the JSON file would be to run this on the command line.

Again, this is not a good way to manipulate JSON, but it may be suitable for your ad hoc case.

perl -ne 'print unless /"\$ref:"/' file.json > new_file.json
beasy
  • 1,227
  • 8
  • 16