JQ - 使用Startswith过滤JSON文件并使搜索大小写不敏感

时间:2017-08-08 08:17:27

标签: json jq

以下是我尝试运行的JSON文件的示例:

{
    "$itemsPerPage": 1208,
    "$resources": [
        {
            "$uuid": "7b44f5b6-a5bd-4c7a-8d4a-581ff36a1072",
            "$etag": "2016-08-12T12:29:33Z",
            "BPSNAM": "InfoCenter",
            "BPSNUM": "AO011",
            "BPSNUM_REF": {
                "$title": "InfoCenter",
                "$description": "InfoCenter"
            },
            "CUR": "AOA",
            "CUR_REF": {
                "$title": "Kwanza",
                "$description": "Angolan kwanza",
                "$symbol": "",
                "$scale": 2,
                "$precision": 11
            }
        },
        {
            "$uuid": "25ee60e6-dd55-4c0a-842d-8e66234342b8",
            "$etag": "2015-01-23T09:18:55Z",
            "BPSNAM": "NeoPneus - Componentes e Pneus",
            "BPSNUM": "AO051",
            "BPSNUM_REF": {
                "$title": "NeoPneus",
                "$description": "NeoPneus - Componentes e Pneus"
            },
            "CUR": "AOA",
            "CUR_REF": {
                "$title": "Kwanza",
                "$description": "Angolan kwanza",
                "$symbol": "",
                "$scale": 2,
                "$precision": 11
            }
        },
        {
            "$uuid": "00682755-d9c7-4755-928a-439f09555e77",
            "$etag": "2014-02-04T18:49:06Z",
            "BPSNAM": "SuperBikes - Acessorios",
            "BPSNUM": "AO052",
            "BPSNUM_REF": {
                "$title": "SuperBikes",
                "$description": "SuperBikes - Acessorios"
            },
            "CUR": "AOA",
            "CUR_REF": {
                "$title": "Kwanza",
                "$description": "Angolan kwanza",
                "$symbol": "",
                "$scale": 2,
                "$precision": 11
            }
        }

我使用下面输出我想要的3个结果。但是,这是一个不敏感的包含但我想通过startswith搜索:

.["$resources"]|.[]|(select(.BPSNAM | test("A";"i")))|.BPSNAM,.BPSNUM,.CUR

如何通过startswith进行搜索?提前感谢任何指导。

1 个答案:

答案 0 :(得分:0)

有一个过滤器,'以/ 0'开头,但是因为你已经遇到了使用' test / 2'用" i"选项,让我们坚持:

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(IocHelper.GetContainer()));

        JwtSecurityTokenHandler.InboundClaimTypeMap = new Dictionary<string, string>();

        app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = "Cookies"
        });

        app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
        {
            ClientId = ConfigurationHelper.GetClientId(),
            Authority = ConfigurationHelper.GetSecurityTokenServiceUrl(),
            RedirectUri = ConfigurationHelper.GetPortalHomePageUrl(),
            PostLogoutRedirectUri = ConfigurationHelper.GetPortalHomePageUrl(),
            ResponseType = "code id_token",
            Scope = "openid profile public_api",
            TokenValidationParameters = new TokenValidationParameters
            {
                NameClaimType = "name",
                RoleClaimType = "role"
            },

            SignInAsAuthenticationType = "Cookies",


            Notifications = new OpenIdConnectAuthenticationNotifications
            {
                AuthorizationCodeReceived = async n =>
                {
                    // use the code to get the access and refresh token
                    var tokenClient = new TokenClient(ConfigurationHelper.GetTokenEndpointUrl(), ConfigurationHelper.GetClientId(), ConfigurationHelper.GetClientSecret());
                    var tokenResponse = await tokenClient.RequestAuthorizationCodeAsync(n.Code, n.RedirectUri);
                    if (tokenResponse.IsError)
                    {
                        throw new Exception(tokenResponse.Error);
                    }

                    // use the access token to retrieve claims from userinfo
                    var userInfoClient = new UserInfoClient(ConfigurationHelper.GetUserInfoUrl());
                    var userInfoResponse = await userInfoClient.GetAsync(tokenResponse.AccessToken);
                    if (userInfoResponse.IsError)
                    {
                        throw new Exception(userInfoResponse.Error);
                    }

                    // create new identity
                    var id = new ClaimsIdentity(n.AuthenticationTicket.Identity.AuthenticationType);
                    foreach (var c in userInfoResponse.Claims)
                    {
                        id.AddClaim(new Claim(c.Type, c.Value));
                    }

                    id.AddClaim(new Claim("access_token", tokenResponse.AccessToken));
                    id.AddClaim(new Claim("expires_at", DateTime.Now.AddSeconds(tokenResponse.ExpiresIn * 2).ToLocalTime().ToString(CultureInfo.InvariantCulture)));
                    id.AddClaim(new Claim("id_token", n.ProtocolMessage.IdToken));
                    id.AddClaim(new Claim("sid", n.AuthenticationTicket.Identity.FindFirst("sid").Value));


                    var claimsIdentity = new ClaimsIdentity(id.Claims, n.AuthenticationTicket.Identity.AuthenticationType, "name", "role");
                    //claimsIdentity.IsAuthenticated = true;
                    n.AuthenticationTicket = new AuthenticationTicket(claimsIdentity, n.AuthenticationTicket.Properties);
                },
                RedirectToIdentityProvider = n =>
                {
                    // if signing out, add the id_token_hint
                    if (n.ProtocolMessage.RequestType != OpenIdConnectRequestType.LogoutRequest)
                    {
                        return Task.FromResult(0);
                    }

                    var idTokenHint = n.OwinContext.Authentication.User.FindFirst("id_token");

                    if (idTokenHint != null)
                    {
                        n.ProtocolMessage.IdTokenHint = idTokenHint.Value;
                    }

                    // DOESN'T WORK
                    n.OwinContext.Request.Headers.Append("Ocp-Apim-Subscription-Key", "MY KEY GOES HERE");

                    // ALSO DOESN'T WORK
                    n.Request.Headers.Append("Ocp-Apim-Subscription-Key", "MY KEY GOES HERE");

                    return Task.FromResult(0);
                }
            }
        });
    }
}