json-prune

json-prune

The json-prune snippet traps calls to JSON.parse and, if the result is an Object, removes the specified properties from the result before returning to the caller.

It wraps both JSON.parse and Response.prototype.json, so a payload is covered whether the page parses it itself or reads it straight off a fetch response with await response.json().

Parameters

Name

Description

Mandatory

rawPrunePaths

Space-separated properties to remove. Accepts {} and [] placeholders to iterate over nested objects and arrays. Each entry can also be a jsonpath(...) query — see below.

Yes

rawNeedlePaths

Space-separated properties which must all be present for pruning to occur.

No

rawNeedleStack

Space-separated strings or regex which must be present in the call stack.

No

JSONPath mode

When an entry in rawPrunePaths starts with jsonpath(, the query is evaluated against the parsed object and every property it matches is deleted. Use it when the placeholder syntax cannot express the shape you need — for example filtering on a sibling property's value, or matching at an unknown depth with $... For simple fixed paths, the dotted and placeholder forms remain easier to read.

  • rawPrunePaths is split on spaces, so a query must not contain spaces.

  • Quotes inside a filter expression must be written \', or the parser strips them.

  • Entries are evaluated independently, so plain properties and queries can be mixed in one filter.

  • For the supported query syntax, see JSONPath evaluator. For the escaping rules, see Snippets Overview.

Filter examples

Filter

Result

json-prune ads

Removes any ads property from every object parsed with JSON.parse.

json-prune 'ads videoAds'

Removes all ads and videoAds properties.

json-prune ads userId

Removes ads from every object with a userId property.

json-prune data.nested.[].ad

Removes all ad properties listed inside an array.

json-prune data.nested.{}.ad

Removes all ad properties listed inside nested objects.

json-prune data.nested.[-].elem.ad

Removes array elements that have an ad property.

json-prune data.nested.{-}.ad

Removes object properties that contain the property ad.

json-prune ads '' functionName

Removes ads if functionName is found in the call stack.

json-prune 'jsonpath($..ads)'

JSONPath mode. Removes every ads property at any depth.

json-prune 'jsonpath($.items[?(@.type==\'ad\')])'

Removes every element of items whose type is ad — a condition on the data that the placeholder syntax cannot express.

json-prune 'jsonpath($..ads) videoAds'

Mixes forms: prunes via the query and via the plain videoAds property in the same filter.

Debugging

Message

When

Definition

Wrapped JSON.parse for prune

After wrapping

The API was wrapped.

Wrapped Response.json for prune

After wrapping

Payloads read via await response.json() are covered too.

Found needle in stack trace: <<rawNeedleStack>>

After comparing stack trace

The needle was found.

Found <<rawPrunePaths>> and deleted.

After deletion

A property was found and deleted.

Invalid JSONPath query: <<path>>. Error: <<message>>

Right after the snippet fires

The query could not be compiled, so that entry will never match.

JSONPath match found and deleted at [<<key>>]

Per matched property

A property matched the query and was deleted. One message per match.

JSONPath evaluation failed for: <<path>>. Error: <<message>>

Evaluating a compiled query threw

That object is left untouched.