Giter VIP home page Giter VIP logo

blushingpenguin.jsonpath's Introduction

ci.appveyor.com codecov.io

BlushingPenguin.JsonPath

BlushingPenguin.JsonPath is a jsonpath implementation for MongoDB.Bson. It is a port of the jsonpath implementation from Newtonsoft.Json.

Packages

BlushingPenguin.JsonPath can also be installed from nuget.org.

Install with package manager:

Install-Package BlushingPenguin.JsonPath

or with nuget:

nuget install BlushingPenguin.JsonPath

Or with dotnet:

dotnet add package BlushingPenguin.JsonPath

Example usage

using BlushingPenguin.JsonPath;

void Example()
{
var o = JsonDocument.Parse(@"{
""Stores"": [
    ""Lambton Quay"",
    ""Willis Street""
],
""Manufacturers"": [
    {
    ""Name"": ""Acme Co"",
    ""Products"": [
        {
        ""Name"": ""Anvil"",
        ""Price"": 50
        }
    ]
    },
    {
    ""Name"": ""Contoso"",
    ""Products"": [
        {
        ""Name"": ""Elbow Grease"",
        ""Price"": 99.95
        },
        {
        ""Name"": ""Headlight Fluid"",
        ""Price"": 4
        }
    ]
    }
]
}");

string? name = o.SelectToken("Manufacturers[0].Name")?.GetString();
// Acme Co

decimal? productPrice = o.SelectToken("Manufacturers[0].Products[0].Price")?.GetDecimal();
// 50

string? productName = o.SelectToken("Manufacturers[1].Products[0].Name")?.GetString();
// Elbow Grease

Assert.AreEqual("Acme Co", name);
Assert.AreEqual(50m, productPrice);
Assert.AreEqual("Elbow Grease", productName);

IList<string> storeNames = o.SelectToken("Stores")!.Value.EnumerateArray().Select(s => s.GetString()).ToList();
// Lambton Quay
// Willis Street

IList<string?> firstProductNames = o.RootElement.GetProperty("Manufacturers")!.EnumerateArray().Select(
    m => m.SelectToken("Products[1].Name")?.GetString()).ToList();
// null
// Headlight Fluid

decimal totalPrice = o.RootElement.GetProperty("Manufacturers")!.EnumerateArray().Aggregate(
    0M, (sum, m) => sum + m.SelectToken("Products[0].Price")!.Value.GetDecimal());
// 149.95

Local Development

Hacking on BlushingPenguin.JsonPath is easy! To quickly get started clone the repo:

git clone https://github.com/blushingpenguin/BlushingPenguin.JsonPath.git
cd BlushingPenguin.JsonPath

To compile the code and run the tests just open the solution in Visual Studio 2019 Community Edition. To generate a code coverage report run cover.ps1 from the solution directory.

blushingpenguin.jsonpath's People

Contributors

blushingpenguin avatar

Watchers

James Cloos 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.