Giter Club home page Giter Club logo

objectpanther's Introduction

ObjectPanther

This script helps with querying hashtables (mainly JSON) in PowerShell.

It was created so I could perform some basic filtering on large json objects that come from an external API, where the documentation, while being fairly complete, could not explain how it was being used in practice.

There are bugs. However, as this is a personal project, I will only be fixing those that impede my own use of it.

Known Issues

  • Filters cannot be combined with and/or, they perform their filtering individually, one at a time. I.e. if a value is filtered out by one filter, it cannot be retrieved by another filter
  • Does not handle arrays as input

Workaround - wrap array in an object:

@{Value=(Get-ChildItem -File | Select-Object -First 5)} |
  ConvertTo-Json -Depth 2 |
    Get-ObjectPath "Value/Name" -AsJson
# [
#   "ObjectPanther.ps1",
#   "README.md"
# ]
  • Does not take hashtables directly

Workaround - convert object to JSON:

$MyObject | ConvertTo-Json -Depth 2 | Get-ObjectPath "." -AsJson

Examples

Parsing a json file, selecting the nested objects "Items" and then "Steps", filtering out all "Steps" objects where the property "Completed" is false:

Get-ObjectPath "Items/Steps|Completed ne false|" -File 'pathToJsonFile.json' -AsJson # -JsonInput is the default

Parsing json from a pipeline:

Get-Content 'pathToJsonFile.json' | Get-ObjectPath "Items/Steps|Completed ne false|" -AsJson # -JsonInput is the default

Syntax/longer explanation

Selecting the current property, . :

"Value/." # select Value, then select Value
# mostly useful to index on filtered items, e.g.
"Value|Name eq X|.[0]"

Selecting a property, / :

"Value/Name" # select Value/Name
"./Value" # select current then Value

Selecting multiple properties, {arg0,arg1,...} :

"Value{Name,Water}" # select "Name" and "Water" property from the "Value" object property

Indexing into an array, [int] :

"Value[0]" # first item
"Value/.[0]" # alternative syntax (useful to get a subset of items that have been filtered, as [] requires a property)

Getting a range from an array, [int:int] :

# There's also reverse ranges, but I haven't decided how I want them to work yet.
"Value[0:1]" # start at first item, take 1 item
"Value[1:3]" # start at second item, take 3 items

Filtering objects in an array, |filterVariable op filterValue|:

"Value/Name|. ne ObjectPanther.ps1|" # select Value/Name and filter all values where Name is not "ObjectPanther.ps1"
"Value|Name ne ObjectPanther.ps1|" # select Value and filter all values where Name is not "ObjectPanther.ps1"

Filtering nested objects in an array, |filterVariable/nestedVar op filterValue|:

"Value|Thing/Other. ne Y|" # select Value and filter all values where Thing->Other is not "Y"

Filtering and selecting objects in an array, |filterVariable .op filterValue|:

"Value|Name .ne ObjectPanther.ps1|" # filter all values where Name is not "ObjectPanther.ps1"

Filter after a filter, |filterVar1 op filterVal1||filterVar2 op filterVar2|:

"Value/Name|. ne ObjectPanther.ps1|" # select Value/Name and filter all values where Name is not "ObjectPanther.ps1"
"Value|Name ne ObjectPanther.ps1|" # select Value and filter all values where Name is not "ObjectPanther.ps1"

Implicitly selecting a property:

"Value" # first part of the path (unless it's a filter)
"Value|Name eq Z|Name" # after a filter

Filter operators

Op Function
eq Equals
ne Not equals
le Less than or equals
lt Less than
ge Greater than or equals
gt Greater than
like Wildcard match
notlike Not wildcard match

Implicit filter values

The following filter values have to be surrounded with " to be interpreted literally.

I.e. null == $null, "null" == null

Name Value
null $null
true $true
false $false

objectpanther's People

Contributors

szune avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.