Skip to content

Instantly share code, notes, and snippets.

@ahmedazhar05
Last active November 6, 2023 13:01
Show Gist options
  • Save ahmedazhar05/a71b6c4594b92cf5018e63964396b963 to your computer and use it in GitHub Desktop.
Save ahmedazhar05/a71b6c4594b92cf5018e63964396b963 to your computer and use it in GitHub Desktop.
Standardizing the parsing of complex URL query strings

Rules:

variable[] = [] # append new item in the array
variable[integer] = [] # alter the (already-available/pre-existing) index in the array
variable[non-integer] = {} # object
y[][non-integer] = [{}] # create an object as a newly created array element
variable[integer][non-integer] = [{}] # alter the object at the pre-existing indexed position
y[non-integer][] = {[]} # append a new array element to the key
variable[non-integer][integer] = {[]} # alter the pre-existing array index of the key-value object

Examples:

  1. Query string: y[]=1&y[]=2&y[]=3

    {
        "y": [
            "1",
            "2",
            "3"
        ] // auto append/push
    }
  2. Query string: y[]=1&y[]=2&y[]=2&y[1]=3

    {
        "y": [ // zero-based indexing
            "1",
            "3",
            "2"
        ] // notice the values
    }
  3. Query string: y[a]=1&y[b]=2

    {
        "y": {
            "a": "1",
            "b": "2"
        }
    }
  4. Query string: y[a][]=1&y[a][]=2&y[a][]=3

    {
        "y": {
            "a": [
                "1",
                "2",
                "3"
            ]
        }
    }
  5. Query string: y[][a]=1&y[][b]=2&y[][c]=3

    {
        "y": [
            {
                "a": "1"
            },
            {
                "b": "2"
            },
            {
                "c": "3"
            }
        ]
    }
  6. Query string: y[0][a]=1&y[0][b]=2&y[][c]=3&y[1][d]=4&y[0][e]=5&y[][f]=6

    {
        "y": [
            {
                "c": "3",
                "e": "5"
            },
            {
                "f": "6"
            }
        ]
    }
  7. Query string: y[a]=1&y[a]=2

    {
        "y": {
            "a": "2"
        }
    } // overwritten
  8. Query string: y[0]=1&y[0]=2

    {
        "y": [
            "2"
        ]
    } // overwritten
  9. Query string: y[a][][]=1.0&y[a][0][]=1.5&y[a][0][]=1.9&y[a][]=2&y[a][]=3&y[b]=4&y[c]=5

    {
        "y": {
            "a": [
                [
                    "1.0",
                    "1.5",
                    "1.9"
                ],
                "2",
                "3"
            ],
            "b": "4",
            "c": "5"
        }
    }
  10. Query string: y[0]=1&y[1]=2&y[5]=3

    {
        "y": [
        ] // non-existing indices are simply ignored
    }
  11. Query string: y[5]=1&y[]=0&y[3]=3&y[0]=2

    {
        "y": [
            "2"
        ]
    }
  12. Query string: y[4]=1&y[]=0&y[]=1&y[]=2&y[]=3&y[]=4&y[]=5

    {
        "y": [
            "0",
            "1",
            "2",
            "3",
            "4", // no effect since it was declared before this index-position ever existed
            "5"
        ]
    }
  13. Query string: y=0&y=1&y=2&y=3&y=4&y=5

    {
        "y": "5"
    }
  14. Query string: y[][a]=1&y[][c]=3&y[1][d]=4&y[2][g]=5&y[][f]=6

    {
        "y": [
            {
                "a": "1",
            },
            {
                "c": "3",
                "d": "4"
            },
            {
                "f": "6"
            }
        ]
    }
  15. Query string: y[]=1&y[a]=2

    {
        "y": [
            "1"
        ] // transformation of array to object and vice versa is not possible
    }
  16. Query string: y[a]=1&y[]=2

    {
        "y": {
            "a": "1"
        } // transformation of object to array and vice versa is not possible
    }
  17. Query string: y[a]=1&y[0]=2

    {
        "y": {
            "a": "1"
        } // transformation of object to array and vice versa is not possible
    }
  18. Query string: y[a]=1&y=2

    {
        "y": {
            "a": "1"
        } // transformation of object to static value and vice versa is not possible
    }
  19. Query string: y[]=1&y=2

    {
        "y": [
            "1"
        ] // transformation of array to static value and vice versa is not possible
    }
  20. Query string: y=2&y[]=1

    {
        "y": "2" // transformation of static value to object and vice versa is not possible
    }
@ahmedazhar05
Copy link
Author

Open to scrutiny and correction

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment