# How to exclude XML & json key-value if key length is greater than 15 char and value length is greater than 100 char

**URL:** https://discuss.elastic.co/t/how-to-exclude-xml-json-key-value-if-key-length-is-greater-than-15-char-and-value-length-is-greater-than-100-char/270248
**Category:** Logstash
**Created:** [April 15, 2021, 2:11pm UTC](https://discuss.elastic.co/t/how-to-exclude-xml-json-key-value-if-key-length-is-greater-than-15-char-and-value-length-is-greater-than-100-char/270248 "2021-04-15T14:11:17Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![mangeshmj1992](https://avatars.discourse-cdn.com/v4/letter/m/d9b06d/32.png) [@mangeshmj1992](https://discuss.elastic.co/u/mangeshmj1992)
#### Post date: [April 15, 2021, 2:11pm UTC](https://discuss.elastic.co/t/how-to-exclude-xml-json-key-value-if-key-length-is-greater-than-15-char-and-value-length-is-greater-than-100-char/270248/1 "2021-04-15T14:11:17Z")

</div>

@Badger  
I want to exclude XML & json key-value if key length is greater than 15 char and value length is greater than 100 char. Is this possible can you please suggest way for it.

---

<div class="post-metadata">

### Author: ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)
#### Post date: [April 15, 2021, 2:57pm UTC](https://discuss.elastic.co/t/how-to-exclude-xml-json-key-value-if-key-length-is-greater-than-15-char-and-value-length-is-greater-than-100-char/270248/2 "2021-04-15T14:57:23Z")

</div>

This will be very similar to the [question](https://discuss.elastic.co/t/how-to-exclude-key-value-pair-from-kv-filter-if-key-size-greater-than-15-char/269566/2) you asked about filtering on just the key length

```
ruby {
    code => '
        event.to_hash.each { |k, v|
            if k.length > 15 and v.length > 100
                event.remove(k)
            end
        }
    '
}
```

---

<div class="post-metadata">

### Author: ![mangeshmj1992](https://avatars.discourse-cdn.com/v4/letter/m/d9b06d/32.png) [@mangeshmj1992](https://discuss.elastic.co/u/mangeshmj1992)
#### Post date: [April 15, 2021, 3:04pm UTC](https://discuss.elastic.co/t/how-to-exclude-xml-json-key-value-if-key-length-is-greater-than-15-char-and-value-length-is-greater-than-100-char/270248/3 "2021-04-15T15:04:50Z")

</div>

@Badger ,  
It is not working on data which is parsed through XML and JSON filter

---

<div class="post-metadata">

### Author: ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)
#### Post date: [April 15, 2021, 3:09pm UTC](https://discuss.elastic.co/t/how-to-exclude-xml-json-key-value-if-key-length-is-greater-than-15-char-and-value-length-is-greater-than-100-char/270248/4 "2021-04-15T15:09:09Z")

</div>

That really does not provide enough information for me to suggest a solution. What does the data look like? What do you mean by "not working"?

---

<div class="post-metadata">

### Author: ![mangeshmj1992](https://avatars.discourse-cdn.com/v4/letter/m/d9b06d/32.png) [@mangeshmj1992](https://discuss.elastic.co/u/mangeshmj1992)
#### Post date: [April 15, 2021, 3:37pm UTC](https://discuss.elastic.co/t/how-to-exclude-xml-json-key-value-if-key-length-is-greater-than-15-char-and-value-length-is-greater-than-100-char/270248/5 "2021-04-15T15:37:51Z")

</div>

@Badger ,  
I trying to parse below XML message with XML filter:

```
2021-01-07 05:55:06,931 BLZMUSTANG MONITOR Method=gk.RiskManagement.rdVendorGatewayCaller::Dispose SoapAction=gk.RiskManagement/IBlazeWcfService002/LogonRiskSessionInit PCPNumber=424049880 SessionID=iph-5a86f20c-3f68-4eeb-ab8a-cf9eb6f639c0 ChannelVersion=9811 Message=Performance MethodName=ParseCustomFieldsByVendor ResponseTimeInMS=0 Thread=7216 ProcessId=64624 /LM/W3SVC/2/ROOT-1-132542254620449814 WUWVC9ASBLZ01 
2021-01-07 05:55:06,931 BLZMUSTANG MONITOR Method=gk.RiskManagement.rdVendorGatewayCaller::CallVendor SoapAction=gk.RiskManagement/IBlazeWcfService002/LogonRiskSessionInit PCPNumber=424049880 SessionID=iph-5a86f20c-3f68-4eeb-ab8a-cf9eb6f639c0 ChannelVersion=9811 Message=PERFMON MessageCategory=VendorCalls Source=Mustang Status=Success VIC=Iovation reqTime=2021-01-07 05:55:06.6455223 respTime=2021-01-07 05:55:06.9310857 reqCollPath=VendorCalls,Iovation,Current,Request respCollPath=VendorCalls,Iovation,Current,Response VGRequest=<?xml version="1.0"?>
<requestType xmlns:w="gk.RiskManagement">
  <header xmlns="http://www.gk.com/eai/custom/VendorGatewayCDM.xsd">
    <applicationID>BLAZE001</applicationID>
    <hostname>WUWVC9ASBLZ01</hostname>
    <timestamp>2021-01-07T05:55:06.6455223-05:00</timestamp>
    <transactionID>8143bd16-d21f-41ae-a30f-084505030d9a</transactionID>
    <correlationID>iph-5a86f20c-3f68-4eeb-ab8a-cf9eb6f639c0</correlationID>
  </header>
  <credential xmlns="http://www.gk.com/eai/custom/VendorGatewayCDM.xsd">
    <userName> ****** </userName>
    <password> ****** </password>
  </credential>
  <service xmlns="http://www.gk.com/eai/custom/VendorGatewayCDM.xsd">GetDeviceFingerPrints</service>
  <serviceFunction xmlns="http://www.gk.com/eai/custom/VendorGatewayCDM.xsd">CheckTransactionDetails</serviceFunction>
  <provider xmlns="http://www.gk.com/eai/custom/VendorGatewayCDM.xsd">Iovation</provider>
  <inputParameter xmlns="http://www.gk.com/eai/custom/VendorGatewayCDM.xsd">
    <parameter>
      <name>SessionID</name>
      <value>iph-5a86f20c-3f68-4eeb-ab8a-cf9eb6f639c0</value>
      <type>STRINGTYPE</type>
    </parameter>
    <parameter>
      <name>blackboxdata</name>
      <value>0660BZA4AsybKO6AwZ3IfUmXUnnh7k2L1Q7wEKRbe1x+WzgANSgC/b5QG/VscFElEKyxRTxRG5VO083k9gXFOvAPwOspt5YyZjrWawBiqhhOisaHhTDX1hvhwEExwcqApi6JYpCkUDK2gK2obeliktMaQOmBUiU6gy5PC28UZZeD18KGQwkDh/NoUOkZBQhPxw57N8UOqGKFSqrH0QunbPb/GShjQ7UxIk+3DATp+z7gr7B3vxuiJYKAtZu+5WH0P38+DuMba1CMtzgEPJ9YvtAgZnZNG3ZcS6v4/NwZhh5C9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Y/NYMUXvctZ+NP5YFj2HUEJPdXBLU/ZtdobUZmuFV7volCMKYgchR90fQflnXADAc7GpltmV3R1CgNm7EPkjTTJkFyo2P7zRbCFLXRHaG87aMF1M2mFpcE2hYFazCs3A</value>
      <type>STRINGTYPE</type>
    </parameter>
    <parameter>
      <name>customer_number</name>
      <value>424049880</value>
      <type>STRINGTYPE</type>
    </parameter>
    <parameter>
      <name>enduser_ip</name>
      <value>158.14 *****.232</value>
      <type>STRINGTYPE</type>
    </parameter>
    <parameter>
      <name>call_type</name>
      <value>transfer</value>
      <type>STRINGTYPE</type>
    </parameter>
    <parameter>
      <name>PCPNumber</name>
      <value>424049880</value>
      <type>STRINGTYPE</type>
    </parameter>
  </inputParameter>
</requestType>

```

Grok Pattern:

```
                     if "VGRequest" in [message]{
	grok{
		match => { "message" => "(?m)%{TIMESTAMP_ISO8601:appTimestamp}\s*%{WORD:AppName}\s*%{WORD:Loglevel}\s%{GREEDYDATA:logmessage}\s*VGRequest=%{GREEDYDATA:xmlmessage}"}	 
		 
	}
	xml {
		source => "xmlmessage"
		target => "xml"
	}
	
  }

```

Here I want to exclude fields (Parsed by xml filter) if char are greater than 15 and fields value greater than 100

---

<div class="post-metadata">

### Author: ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)
#### Post date: [April 15, 2021, 3:44pm UTC](https://discuss.elastic.co/t/how-to-exclude-xml-json-key-value-if-key-length-is-greater-than-15-char-and-value-length-is-greater-than-100-char/270248/6 "2021-04-15T15:44:39Z")

</div>

Please edit your post, select the "XML" and click on \</\> in the tool bar above the edit pane. If you look at your post you will see that there are no XML tags at the moment.

---

<div class="post-metadata">

### Author: ![mangeshmj1992](https://avatars.discourse-cdn.com/v4/letter/m/d9b06d/32.png) [@mangeshmj1992](https://discuss.elastic.co/u/mangeshmj1992)
#### Post date: [April 15, 2021, 4:00pm UTC](https://discuss.elastic.co/t/how-to-exclude-xml-json-key-value-if-key-length-is-greater-than-15-char-and-value-length-is-greater-than-100-char/270248/7 "2021-04-15T16:00:04Z")

</div>

@Badger ,  
Can you please check it now

---

<div class="post-metadata">

### Author: ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)
#### Post date: [April 15, 2021, 4:44pm UTC](https://discuss.elastic.co/t/how-to-exclude-xml-json-key-value-if-key-length-is-greater-than-15-char-and-value-length-is-greater-than-100-char/270248/8 "2021-04-15T16:44:15Z")

</div>

OK, so you want to recursively remove long keys/values from your event. That will require a ruby function. That is going to be similar to [this](https://discuss.elastic.co/t/to-exclude-around-350-fields-in-json-in-logstash/239019/6).

```
    xml { source => "message" target => "xml" force_array => false }
    ruby {
        code => '
            def removeBigThings(object, name, event)
                if object
                    if object.kind_of?(Hash) and object != {}
                        object.each { |k, v| removeBigThings(v, "#{name}[#{k}]", event) }
                    elsif object.kind_of?(Array) and object != []
                        object.each_index { |i|
                            removeBigThings(object[i], "#{name}[#{i}]", event)
                        }
                    else
                        lastElement = name.gsub(/^.*\[/, "").gsub(/\]$/, "")
                        if lastElement.length > 15 or object.to_s.length > 100
                            event.remove(name)
                        end
                    end
                end
            end

            event.to_hash.each { |k, v|
                removeBigThings(v, "[#{k}]", event)
            }
        '
    }

```

Note that I have used force\_array =\> false. You do not have to, I just do not like everything being an array with one member.

Note also that I used "or" in the test `lastElement.length > 15 or object.to_s.length > 100`. You said "and" but that would result in nothing being removed.

Note also that this will delete [message], since that is more than 100 characters long. If you only want to modify things in [xml] you could change

```
            event.to_hash.each { |k, v|
                removeBigThings(v, "[#{k}]", event)
            }

```

to

```
removeBigThings(event.get("xml"), "[xml]", event)
```

---

<div class="post-metadata">

### Author: ![mangeshmj1992](https://avatars.discourse-cdn.com/v4/letter/m/d9b06d/32.png) [@mangeshmj1992](https://discuss.elastic.co/u/mangeshmj1992)
#### Post date: [April 15, 2021, 5:08pm UTC](https://discuss.elastic.co/t/how-to-exclude-xml-json-key-value-if-key-length-is-greater-than-15-char-and-value-length-is-greater-than-100-char/270248/9 "2021-04-15T17:08:18Z")

</div>

@Badger ,  
I used above code as it is but is not dropping keys if greater than 15 . Worked for values

---

<div class="post-metadata">

### Author: ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)
#### Post date: [April 15, 2021, 5:21pm UTC](https://discuss.elastic.co/t/how-to-exclude-xml-json-key-value-if-key-length-is-greater-than-15-char-and-value-length-is-greater-than-100-char/270248/10 "2021-04-15T17:21:06Z")

</div>

If, after

```
    def removeBigThings(object, name, event)

```

you add a line

```
puts "Called for #{name}"

```

do you see the function being called for each object in the event?

---

<div class="post-metadata">

### Author: ![mangeshmj1992](https://avatars.discourse-cdn.com/v4/letter/m/d9b06d/32.png) [@mangeshmj1992](https://discuss.elastic.co/u/mangeshmj1992)
#### Post date: [April 16, 2021, 5:51am UTC](https://discuss.elastic.co/t/how-to-exclude-xml-json-key-value-if-key-length-is-greater-than-15-char-and-value-length-is-greater-than-100-char/270248/11 "2021-04-16T05:51:45Z")

</div>

@Badger ,  
I am using following code but it is not drooping keys which are greater than 15.  
fields created:

Sample filed: xml.outputParameter.vendorResponse.provider.keyword

```
ruby {
        code => '
            def removeBigThings(object, name, event)
			puts "Called for #{name}"
                if object
                    if object.kind_of?(Hash) and object != {}
                        object.each { |k, v| removeBigThings(v, "#{name}[#{k}]", event) }
                    elsif object.kind_of?(Array) and object != []
                        object.each_index { |i|
                            removeBigThings(object[i], "#{name}[#{i}]", event)
                        }
                    else
                        lastElement = name.gsub(/^.*\[/, "").gsub(/\]$/, "")
                        if lastElement.length > 15 or object.to_s.length > 30
                            event.remove(name)
                        end
                    end
                end
            end

           removeBigThings(event.get("xml"), "[xml]", event)
            
        '
    }
```

---

<div class="post-metadata">

### Author: ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)
#### Post date: [April 16, 2021, 2:39pm UTC](https://discuss.elastic.co/t/how-to-exclude-xml-json-key-value-if-key-length-is-greater-than-15-char-and-value-length-is-greater-than-100-char/270248/12 "2021-04-16T14:39:31Z")

</div>

So if you have an [a2345678] field inside a [b2345678] object, for a total of 17 characters when represented as a2345678.b2345678 you want to drop it?

If so you will need to change

```
                    lastElement = name.gsub(/^.*\[/, "").gsub(/\]$/, "")
                    if lastElement.length > 15

```

to something more like

```
                    dottedName = name.gsub(/\]\[/, ".").gsub(/\]$/, "").gsub(/^\[/, "")
                    if dottedName.length > 15
```

---

<div class="post-metadata">

### Author: ![mangeshmj1992](https://avatars.discourse-cdn.com/v4/letter/m/d9b06d/32.png) [@mangeshmj1992](https://discuss.elastic.co/u/mangeshmj1992)
#### Post date: [April 16, 2021, 3:01pm UTC](https://discuss.elastic.co/t/how-to-exclude-xml-json-key-value-if-key-length-is-greater-than-15-char-and-value-length-is-greater-than-100-char/270248/13 "2021-04-16T15:01:54Z")

</div>

@Badger ,  
Yes above understanding is correct  
I have added following script but still it is not providing  
desired result.

```
ruby {
        code => '
            def removeBigThings(object, name, event)
			puts "Called for #{name}"
                if object
                    if object.kind_of?(Hash) and object != {}
                        object.each { |k, v| removeBigThings(v, "#{name}[#{k}]", event) }
                    elsif object.kind_of?(Array) and object != []
                        object.each_index { |i|
                            removeBigThings(object[i], "#{name}[#{i}]", event)
                        }
                    else
                        dottedName = name.gsub(/\]\[/, ".").gsub(/\]$/, "").gsub(/^\[/, "")
                    if dottedName.length > 15 or object.to_s.length > 30
                            event.remove(name)
                        end
                    end
                end
            end

           removeBigThings(event.get("xml"), "[xml]", event)
            
        '
    }
```

---

<div class="post-metadata">

### Author: ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)
#### Post date: [April 16, 2021, 3:25pm UTC](https://discuss.elastic.co/t/how-to-exclude-xml-json-key-value-if-key-length-is-greater-than-15-char-and-value-length-is-greater-than-100-char/270248/14 "2021-04-16T15:25:49Z")

</div>

I do not know what to say. If I add a line

```
puts "Removing #{dottedName}"

```

immediately before the event.remove then I can see it removes everything that meets the condition

```
Called for [xml]
Called for [xml][service]
Called for [xml][service][xmlns]
Removing xml.service.xmlns
Called for [xml][service][content]
Removing xml.service.content
Called for [xml][xmlns:w]
Called for [xml][inputParameter]
Called for [xml][inputParameter][xmlns]
Removing xml.inputParameter.xmlns

```

etc. That leaves

```
       "xml" => {
         "credential" => {},
     "inputParameter" => {
        "parameter" => [
            [0] {},
            [1] {},
            [2] {},
            [3] {},
            [4] {},
            [5] {}
        ]
    },
           "provider" => {},
            "service" => {},
    "serviceFunction" => {},
            "xmlns:w" => "gk.RiskManagement",
             "header" => {}
},
```

---

<div class="post-metadata">

### Author: ![mangeshmj1992](https://avatars.discourse-cdn.com/v4/letter/m/d9b06d/32.png) [@mangeshmj1992](https://discuss.elastic.co/u/mangeshmj1992)
#### Post date: [April 16, 2021, 4:19pm UTC](https://discuss.elastic.co/t/how-to-exclude-xml-json-key-value-if-key-length-is-greater-than-15-char-and-value-length-is-greater-than-100-char/270248/15 "2021-04-16T16:19:18Z")

</div>

@Badger ,  
Really appreciate your quick help every time. it is working as expected.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [May 14, 2021, 4:19pm UTC](https://discuss.elastic.co/t/how-to-exclude-xml-json-key-value-if-key-length-is-greater-than-15-char-and-value-length-is-greater-than-100-char/270248/16 "2021-05-14T16:19:57Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
