Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Improve 'json-prune' — match key with specific value, remove array or object if it contains specific item #183

Closed
slavaleleka opened this issue Jan 24, 2022 · 19 comments

Comments

@slavaleleka
Copy link
Contributor

there are few issues:

1 current json-prune does not match values if we need specify pruning, only keys — obligatoryProps — are checked

2 if specific array item has to be pruned and we can target such items by their extra fields

#%#//scriptlet('json-prune', 'recs_group.[].tiles.[].[].*', 'recs_group.[].tiles.[].[].ad_origin')

but whole recs_group.[].tiles.[].[] array items will be pruned, not just ones with ad_origin.
check on fontanka.ru
https://uploads.adguard.com/slbdzdrv7cfxo.png

@ameshkov
Copy link
Member

@slavaleleka what do you think about implementing a scriptlet that supports jq-like syntax for that? At least a subset of it.
https://stedolan.github.io/jq/tutorial/

Alternatively, there is this: https://jmespath.org/

I do not suggest supporting it fully, but we could take that syntax as a base and build our limited subset of it. This way it'd be easier for the scriptlet users to understand the capabilities.

PS: we'll probably need to reassign this task to a later milestone.

@slavaleleka slavaleleka changed the title Improve json-prune or implement new scriptlet Improve json-prune or implement new scriptlet Apr 11, 2022
@ngorskikh
Copy link
Member

ngorskikh commented Jun 20, 2022

No description provided.

@ameshkov
Copy link
Member

@ngorskikh the spec should've been posted here I think: AdguardTeam/CoreLibs#1447

We'll also need a filter expression for checking whether the value contains the specified string: ?(key-contains <key> <value>)

@piquark6046
Copy link
Member

Currently, $jsonprune modifier can modify only response of a request.
However, during maintaining List-KR filter, I often need its scriptlet version.

Example:

Gmarket.co.kr

URL: https://gmarket.co.kr/
Description: Extended CSS can hide the elements having AD label. However, their network requests cannot be blocked with the previous modifiers and URL rules (including a RegExp)
Related Issue: List-KR/List-KR#563
Targeted array items: $props.pageProps.serverData.topWideBannerSection.banners[?(@.isAd == true)]

@ameshkov
Copy link
Member

ameshkov commented Dec 4, 2022

Could you please explain what you're trying to do with that request?

Do you need to block a request that contains such a JSON path?

@piquark6046
Copy link
Member

piquark6046 commented Dec 4, 2022

#183 (comment)
Oh, I forgot to add what i want.
What i want is a feature removing a property with a JSON path from result of calling JSON.parse.

@ameshkov
Copy link
Member

ameshkov commented Dec 4, 2022

So you'd like a scriptlet alternative to $jsonprune?

@piquark6046
Copy link
Member

Yes.

@ameshkov
Copy link
Member

ameshkov commented Dec 4, 2022

Makes sense, $jsonprune syntax is more powerful and the end goal was indeed to provide a scriptlet version of it.

Moved it to Scriptlets v1.9 for now. @slavaleleka @stanislav-atr what do you think about it? To understand better what's required please check out this issue in CL: AdguardTeam/CoreLibs#1447

@slavaleleka
Copy link
Contributor Author

we shall return to the issue later

@adguard-bot adguard-bot changed the title Improve json-prune or implement new scriptlet Improve 'json-prune' or implement new scriptlet Jan 16, 2023
@Alex-302
Copy link
Member

Later is now, @slavaleleka :)

@slavaleleka
Copy link
Contributor Author

slavaleleka commented May 18, 2023

the same is needed in scriptlets as well:
AdguardTeam/CoreLibs#1717

UPD: jsonp cannot be handled by the json-prune scriptlet

@slavaleleka
Copy link
Contributor Author

second issue in first issue post cannot be fixed in current syntax implementation — obligatoryProps is checked for the whole json and there is no way to check exact inner array item child (which is object) and conditionally prune it while other array items remains unchanged

@ameshkov
Copy link
Member

@slavaleleka not sure what you mean, aren't filter expressions supposed to solve this?

@maximtop
Copy link
Contributor

maximtop commented Jul 11, 2023

I couldn't figure out how we can handle JSONP requests, apart from one method. For example, let's say there's a JSONP callback like this:

...
<script>
    function handleResponse(data) {
        console.log(JSON.stringify(data));
    }
</script>
<script src="http://server.com/jsonp?callback=handleResponse"></script>
...

We can find the matching callback function from the page and proxify the call to it.

@ameshkov
Copy link
Member

Probably jsonp shouldn't be in scope of this issue since there's no clear solution.

@piquark6046
Copy link
Member

piquark6046 commented Sep 7, 2023

Please take a look for json-prune scriptlet version: gorhill/uBlock@3152896 and gorhill/uBlock@14d60ac.

@Alex-302
Copy link
Member

Another case
AdguardTeam/AdguardFilters#195194
@maximtop

adguard pushed a commit that referenced this issue Feb 10, 2025
Squashed commit of the following:

commit 24b1ee3
Merge: 9c5488c 1324cfa
Author: Adam Wróblewski <adam@adguard.com>
Date:   Mon Feb 10 13:55:44 2025 +0100

    Merge branch 'master' into feature/AG-12363

commit 9c5488c
Author: Adam Wróblewski <adam@adguard.com>
Date:   Mon Feb 10 13:52:41 2025 +0100

    Remove some tests from json-prune.test.js

commit 38495b1
Author: Adam Wróblewski <adam@adguard.com>
Date:   Mon Feb 10 13:51:22 2025 +0100

    Add unit tests

commit b1e6941
Author: Adam Wróblewski <adam@adguard.com>
Date:   Mon Feb 3 16:14:35 2025 +0100

    Update JSDoc

commit 950dac0
Author: Adam Wróblewski <adam@adguard.com>
Date:   Mon Feb 3 15:55:34 2025 +0100

    Fix splitting props which have regex with spaces as value to match
    Add tests

commit dd352d9
Author: Adam Wróblewski <adam@adguard.com>
Date:   Mon Feb 3 14:16:56 2025 +0100

    Fix typo
    prunned -> pruned

commit 954e534
Author: Adam Wróblewski <adam@adguard.com>
Date:   Mon Jan 27 09:10:00 2025 +0100

    Revert unnecessary changes

commit 3eb6d9e
Author: Adam Wróblewski <adam@adguard.com>
Date:   Mon Jan 27 08:16:32 2025 +0100

    Add test

commit 8944822
Author: Adam Wróblewski <adam@adguard.com>
Date:   Sun Jan 26 17:38:46 2025 +0100

    Use splice instead of delete operator when removing elements from array
    Update changelog
    Add examples to documentation

commit a9221b3
Author: Adam Wróblewski <adam@adguard.com>
Date:   Sat Jan 25 16:47:16 2025 +0100

    Export isKeyInObject
    Check type of the removed element
    Add more tests

commit 3bdf055
Author: Adam Wróblewski <adam@adguard.com>
Date:   Sat Jan 25 01:41:43 2025 +0100

    Improve json-prune
    Add ability to remove property and array if it matches specified item
    And add ability to prune object only when it match specific value
@adguard-bot adguard-bot changed the title Improve 'json-prune' or implement new scriptlet Improve 'json-prune' — remove array or object if it contains specific item Feb 10, 2025
@adguard-bot adguard-bot changed the title Improve 'json-prune' — remove array or object if it contains specific item Improve 'json-prune' — match key with specific value, remove array or object if it contains specific item Feb 10, 2025
@slavaleleka slavaleleka added the enhancement Improvement of existent feature label Feb 10, 2025
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

9 participants