Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Filter array of subarrays: drop table columns #302

Open
PsychoTeras opened this issue Jun 14, 2024 · 1 comment
Open

Filter array of subarrays: drop table columns #302

PsychoTeras opened this issue Jun 14, 2024 · 1 comment

Comments

@PsychoTeras
Copy link

PsychoTeras commented Jun 14, 2024

Hi all,

There is a problem I can't solve. I have the following JSON:

{
    "rows": [
        [
            "A1",
            "B1",
            "C1"
        ],
        [
            "A2",
            "B2",
            "C2"
        ],
        [
            "A3",
            "B3",
            "C3"
        ]
    ],
    "columns": [
        "A",
        "B",
        "C"
    ]
}

You can see that it is essentially a table with columns and rows.
So, what I need is to drop some columns and its data. For example, I want to keep column "B" only:

{
    "rows": [
        [
            "B1"
        ],
        [
            "B2"
        ],
        [
            "B3"
        ]
    ],
    "columns": [
        "B"
    ]
}

Ideally, I would like to specify the names of the columns that I want to keep. But it is also ok to hardcode their indexes.

Thanks in advance,
Riz

@Courela
Copy link
Contributor

Courela commented Jun 22, 2024

This almost works:

{
    "rows": {
        "#loop($.rows)": [ "#currentvalueatpath($[?/(@ =~ //^B.*$///)])" ]
    },
    "columns": "#valueof($.columns[?/(@ =~ //^B.*$///)])"
}

And I say almost, because the result would be this:

{
    "rows": [["B1"], ["B2"], ["B3"]],
    "columns": "B"
}

"columns" value is not an array, only if you have more that one matching element, like this input:

{
    "rows": [["A1", "B1", "C1"], ["A2", "B2", "C2"], ["A3", "B3", "C3"]],
    "columns": ["A", "B", "C", "B1"]
}

Output:

{
    "rows": [["B1"], ["B2"], ["B3"]],
    "columns": ["B","B1"]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants