标签: jsonpath

JsonPath 与 Newtonsoft.JSON

我尝试了近一个小时的不同方法,但我不明白;(

我的 JSON 对象是这样的:

"typeOfHair": {
    "value": [
        {
            "code": "Dry Hair",
            "values": [
                {
                    "value": "DryHair",
                    "language": "en"
                },
                {
                    "value": "TrockenesHaar",
                    "language": "de"
                }
            ]
        },
        {
            "code": "Any Type of Hair",
            "values": [
                {
                    "value": "AnyTypeOfHair",
                    "language": "en"
                },
                {
                    "value": "JedenHaartyp",
                    "language": "de"
                }
            ]
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

我的任务是使用 Newtonsoft.JSON 获取语言为“de”的所有值。我目前的做法是:

JsonObject.SelectTokens("typeOfHair.value.values[?(@.language == 'de')].value").ToList()
Run Code Online (Sandbox Code Playgroud)

有人可以帮我弄这个吗?

亲切的问候

json json.net jsonpath

5
推荐指数
1
解决办法
6320
查看次数

如何在GSON中使用jsonpath

我正在掌握 JAVA 的 GSON,并且有一个关于如何从我正在使用的大型 json 文档中进行类似 jsonpath 的选择的问题。

例如,使用如下 json 文档:

{
  "environment": {
    "red": {
      "area": {
        "1": {
          "name": "foo"
        },
        "2": {
          "name": "bar"
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

jsonpath 表达式:

   $.environment.red.area
Run Code Online (Sandbox Code Playgroud)

返回:

 [
  {
    "1": {
      "name": "foo"
    },
    "2": {
      "name": "bar"
    }
  }
]
Run Code Online (Sandbox Code Playgroud)

在GSON中如何实现这种选择呢?

我不清楚该问题被标记为重复的问题的答案。似乎说它可以在 GSON 中完成,但没有说明或显示如何完成(据我所知)。

java json jsonpath gson

5
推荐指数
1
解决办法
1万
查看次数

如何使用 JsonPath (JayWay) 连接 json 元素

有一个简单的 json:

{ "Keys": [
  {"Format": "A", "Subtype": "A1"},
  {"Format": "A",  "Subtype": "A2"},
  {"Format": "B",  "Subtype": "A1"}]
}
Run Code Online (Sandbox Code Playgroud)

我想使用 JsonPath 表达式(没有 Java 特定实现)生成此结果(格式 + 子类型串联):

 AA1
 AA2
 BA1
Run Code Online (Sandbox Code Playgroud)

是否可以使用 jsonPath 连接字符串元素?

谢谢

java json jsonpath

5
推荐指数
1
解决办法
2万
查看次数

预期:null 但:是 <[null]> - Hamcrest 和 jsonPath

我想断言来自其余控制器的 json 输出,但我得到“预期:null 但:was <[null]>”。这是我的测试代码:

mockMvc.perform(post(TEST_ENDPOINT)
            .param("someParam", SOMEPARAM)
            .andDo(print())
            .andExpect(status().is2xxSuccessful())
            .andExpect(jsonPath("*.errorMessage").value(IsNull.nullValue())); 
Run Code Online (Sandbox Code Playgroud)

杰森:

{
    "some_string": {
        "errorMessage": null
    }
}
Run Code Online (Sandbox Code Playgroud)

我发现类似的问题How to assertThat Something is null with Hamcrest? ,但这两个答案都不起作用。也许这是由于 jsonPath,导致它在 [] 括号中返回空值?是断言框架的bug吗?

testing hamcrest jsonpath

5
推荐指数
1
解决办法
4489
查看次数

使用 JsonPath 映射数组

是否可以使用JSONPath将 JSON 数组映射到另一个具有较少字段和缩写键的数组?

我正在处理一个非常冗长的 RESTful API。它有太多的字段,并且每个对象都有很长的属性名称。我想通过仅从数组中的每个匹配项中选择几个字段并将这些字段重命名为三个字母键,将输出投影/映射到较小的有效负载中。

意思是,从这样的输入:

[
    {
        "fooField": "foo1",
        "barField": "bar1",
        "quxField": "qux1"
    },
    {
        "fooField": "foo2",
        "barField": "bar2",
        "quxField": "qux2"
    },
    {
        "fooField": "foo3",
        "barField": "bar3",
        "quxField": "qux3"
    }
]
Run Code Online (Sandbox Code Playgroud)

我希望能够制作这个:

[
    {
        "foo": "foo1",
        "bar": "bar1"
    },
    {
        "foo": "foo2",
        "bar": "bar2"
    },
    {
        "foo": "foo3",
        "bar": "bar3"
    }
]
Run Code Online (Sandbox Code Playgroud)

请注意,我已经重命名了fooFieldbarField字段并完全删除了quxField

也许没有简单的方法可以使用 JSONPath 来执行此操作,但我知道有一个脚本()运算符可以提供帮助,但到目前为止我无法使用它并且似乎找不到示例。

json jsonpath

5
推荐指数
0
解决办法
505
查看次数

如何在 jsonpath 中转义特殊字符冒号(':')

杰森:

{
    "im:rating": {
        "label": "1"
    }
}
Run Code Online (Sandbox Code Playgroud)

在尝试在 groovy 中使用 Jsonpath 时如果我使用这个'$.im: rating'

它显示以下错误

Caused by: com.nebhale.jsonpath.InvalidJsonPathExpressionException: Illegal
Run Code Online (Sandbox Code Playgroud)

字符 'PathCharacter [类型 = [],值 =:,位置 = 4]' $.im:评级 ----^ 非法字符 'PathCharacter [类型 = [SIMPLE_NAME_CHARACTER,LETTER,COMPLEX_NAME_CHARACTER],值 = r,位置 = 5 ]' $.im: rating -----^ 非法字符 'PathCharacter [types=[SIMPLE_NAME_CHARACTER, LETTER, COMPLEX_NAME_CHARACTER], value=a,position=6]' $.im: rating ------^ 非法字符 'PathCharacter [types=[SIMPLE_NAME_CHARACTER, LETTER, COMPLEX_NAME_CHARACTER], value=t,position=7]' $.im: rating -------^ 非法字符 'PathCharacter [types=[SIMPLE_NAME_CHARACTER, LETTER, COMPLEX_NAME_CHARACTER] ,值=i,位置=8]'$.im:评级--------^非法字符'PathCharacter [类型=[SIMPLE_NAME_CHARACTER,LETTER,COMPLEX_NAME_CHARACTER],值=n,位置=9]'$。 im:评级 ---------^ 非法字符 'PathCharacter [类型=[SIMPLE_NAME_CHARACTER,LETTER,COMPLEX_NAME_CHARACTER],值=g,位置=10]' $.im:评级 -------- --^

  at com.nebhale.jsonpath.JsonPath.compile(JsonPath.java:85)
  at com.nebhale.jsonpath.JsonPath.read(JsonPath.java:182)
Run Code Online (Sandbox Code Playgroud)

groovy parsing json jsonpath jsonparser

5
推荐指数
2
解决办法
7449
查看次数

从 JSON 模式模型派生 JSON 路径列表

我正在寻找一个 Javascript 库来列出基于 Json 架构的可能的 Json 路径。

对于如下所示的 json 模式,我想列出可能的 json 路径。

{
  "$id": "https://example.com/person.schema.json",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "Customer",
  "type": "object",
  "properties": {
    "firstName": {
      "type": "string",
      "description": "The person's first name."
    },
    "lastName": {
      "type": "string",
      "description": "The person's last name."
    },
    "age": {
      "description": "Age in years which must be equal to or greater than zero.",
      "type": "integer",
      "minimum": 0
    },
    "address": {
        "type": "object",
        "city": {
            "type": "string",
        },
        "country": {
            "type": "string",
        }
    }
  }
} …
Run Code Online (Sandbox Code Playgroud)

javascript json jsonschema jsonpath

5
推荐指数
2
解决办法
2673
查看次数

如何在加特林负载测试中为多个虚拟用户使用单个 OAuth2.0 令牌

我需要通过 Gattle 加载测试需要 OAuth2.0 令牌的 API(我是个新手!),但希望每个虚拟用户使用相同的令牌。我正在检索令牌(我认为)并将其放入名为“access”的变量中,但当测试本身开始时,我不断收到“没有定义名为“access”的属性”。

我的令牌检索如下所示(以及下面使用的 httpConf):

 class MySimulation extends Simulation {

 val httpConf = http        
    .baseUrl("https://MyBaseUrl.Com/")
    .acceptHeader("application/json") 
    .doNotTrackHeader("1")
    .acceptLanguageHeader("en-UK,en;q=0.5")
    .acceptEncodingHeader("gzip, deflate")
    .userAgentHeader("Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:16.0) Gecko/20100101 Firefox/16.0")
    .shareConnections

 val header = Map("Content-Type" -> """application/x-www-form-urlencoded""")

 al auth = scenario("Retrieve Token")
 .exec(http("POST OAuth Req")
 .post("https://SomeTokenUrl")
 .formParam("resource", "someresource")
 .formParam("grant_type", "somegranttype")
 .formParam("client_secret", "someclientsecret")
 .formParam("client_id", "someclientid")
 .headers(header).check(status.is(200)).check(jsonPath("$.access_token").find.saveAs("access"))) 
Run Code Online (Sandbox Code Playgroud)

然后我尝试将负载测试设置为(注意:我最初放置了“Map”,而不是可变变体,但在某处读到默认值是不可变的,并想知道这是否是标头无法更新的原因):

 val headers_10 = scala.collection.mutable.Map("Content-Type" -> "application/json; charset=ISO-8859-1", "Authorization" -> "Bearer ${access}")

 val scn = scenario("MyService Gatling test run")       
           .exec(http("")               
           .post("Myservice/api")
           .headers(headers_10.toMap) …
Run Code Online (Sandbox Code Playgroud)

scala load-testing jsonpath gatling

5
推荐指数
1
解决办法
5593
查看次数

使用JsonPath获取Json文件的所有叶子节点

我正在寻找一个 JsonPath 表达式来使用 python 检索 Json 文件的所有叶节点。

我曾经使用 xPath 表达式来处理 XML//*[not(child::*)]

我想知道是否存在任何等效的表达式。

谢谢

python json jsonpath

5
推荐指数
0
解决办法
1112
查看次数

在 jsonpath-ng python 中使用 OR 运算符 (|)

我有一个 json 结构 (client_json),如下所示:

client_json = { "data": [ { "attributes": { "creators": [ { "name": "This is a person", "nameType": "Personal", "givenName": "the", "familyName": "person" }, { "name": "This is an organization", "nameType": "Organizational", "givenName": "the", "familyName": "organization" } ] } } ] }
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用 jsonpath-ng 1.4.3 ( https://github.com/h2non/jsonpath-ng ) 来检索创建者名称。

from jsonpath_ng import jsonpath
from jsonpath_ng.ext import parse
Run Code Online (Sandbox Code Playgroud)

这两个表达式给出了预期的结果:

[match.value['name'] for match in parse("data[*].attributes.creators[?(@.nameType='Personal')]").find(client_json)]
[match.value['name'] for match in parse("data[*].attributes.creators[?(@.nameType='Organizational')]").find(client_json)]
Run Code Online (Sandbox Code Playgroud)

我试图将它们与 or 运算符结合起来,所有这些表达式都会产生解析错误:

parse("data[*].attributes.creators[?(@.nameType == 'Organizational' | @.nameType == …
Run Code Online (Sandbox Code Playgroud)

python json operators jsonpath

5
推荐指数
1
解决办法
2175
查看次数