我想使用 json-path-assert 为 Web 服务编写单元测试。鉴于此 JSON:
[
["Some short name","Some parent","Some name"],
["Some short name 2","Some parent 2","Some name 2"]
]
Run Code Online (Sandbox Code Playgroud)
我想检查“Some name 2”的父级是否为“Some parent 2”。我无法更改 JSON 的结构,因为它是由第三方库指定的。
这是我想出的 JSONPath 表达式:
$..[?(@[2] == 'Some name 2')][1]
Run Code Online (Sandbox Code Playgroud)
此表达式工作正常,并在此 JSON 工具中返回预期结果 ('Some parent 2') 。但是,当使用 JSONPath 库在 Java 中使用它时,我得到一个空结果而不是正确的值:
import com.jayway.jsonpath.JsonPath;
import org.junit.Test;
public class TestJSONPath
{
@Test
public void testJsonPath() {
String json = "[[\"Some short name\",\"Some parent\",\"Some name\"]," +
"[\"Some short name 2\",\"Some parent 2\",\"Some name 2\"]]";
System.out.println(JsonPath.read(json, …Run Code Online (Sandbox Code Playgroud) 我有这个json:
{
"treeview":[
{
"text":"blah",
"nodes":[
]
},
{
"text":"blah",
"nodes":[
]
},
{
"text":"blah",
"nodes":[
{
"text":"blah",
"nodes":[
{
"text":"foo",
"nodes":[
// I need to put data in here !!!
]
}
]
}
]
},
{
"text":"blah",
"nodes":[
]
},
{
"text":"foo",
"nodes":[
// Not here !
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
我需要将值放在我位于第2级的 “节点”元素上,并且“文本”等于“ foo”。
到目前为止,这是我尝试过的方法:
var json = myJson;
// First approach
var selector = (JArray)json.SelectTokens($"$..treeview[?(@.text == 'foo')]");
// Second approach
var selector2 = (JArray)json.SelectToken($"$.treeview[?(@...text == …Run Code Online (Sandbox Code Playgroud) 是否可以在 JSON 路径提取器表达式中使用输入变量?
我尝试过表达$.[${someInputVariable}].name,但没有成功。
数据格式:
[
{name=a},
{name=b},
{name=c}
]
Run Code Online (Sandbox Code Playgroud)
例如,如果${inputVariable}设置为 1,我希望表达式返回“b”。
到目前为止我发现的所有示例都有硬编码的搜索值,例如$.[1].name或$..[?(@.name == 'Smith')]
我有来自kubernetes的这个json输出从kubectl get pods -o jsonpath = {.items [*]}得到它
<json>
{
"apiVersion": "v1",
"items": [
{
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"annotations": {
"kubernetes.io/created-by": "{\"kind\":\"SerializedReference\",\"apiVersion\":\"v1\",\"reference\":{\"kind\":\"ReplicaSet\",\"namespace\":\"default\",\"name\":\"some-appdeployment-1780875823\",\"uid\":\"7180b966-7ec1-11e7-9981-305a3ae15081\",\"apiVersion\":\"extensions\",\"resourceVersion\":\"16711638\"}}\n"
},
"creationTimestamp": "2017-08-11T18:18:15Z",
"generateName": "some-appdeployment-1780875823-",
"labels": {
"app": "myapp-auth-some-app",
"pod-template-hash": "1780875823"
},
"name": "some-appdeployment-1780875823-59p06",
"namespace": "default",
"ownerReferences": [
{
"apiVersion": "extensions/v1beta1",
"controller": true,
"kind": "ReplicaSet",
"name": "some-appdeployment-1780875823",
"uid": "7180b966-7ec1-11e7-9981-305a3ae15081"
}
],
"resourceVersion": "16711688",
"selfLink": "/api/v1/namespaces/default/pods/some-appdeployment-1780875823-59p06",
"uid": "71829a96-7ec1-11e7-9981-305a3ae15081"
},
"spec": {
"containers": [
{
"env": [
{
"name": "PROFILE",
"value": "dev"
}
],
"image": "dockerrepo/myapp-auth-some-app:6",
"imagePullPolicy": …Run Code Online (Sandbox Code Playgroud) 我需要使用 kubectl 命令的集群 ID。
root@vagrant-xenial64:~# kubectl get cm cluster-info -n kube-system -o jsonpath='{.data.cluster-config\.json}'
{
"cluster_id": "xxx",
"cluster_name": "prod-yyy-mmm",
"cluster_type": "rrr",
"cluster_pay_tier": "vvv",
"datacenter": "cse",
"account_id": "456777",
"created": "2018-06-32323dffdf:35:48+0000"
}
Run Code Online (Sandbox Code Playgroud)
我需要cluster-id这个特殊的 json
root@vagrant-xenial64:~# kubectl get cm cluster-info -n kube-system -o jsonpath='{.data.cluster-config\.json.cluster_id}'
root@vagrant-xenial64:~#
Run Code Online (Sandbox Code Playgroud)
以上命令返回空字符串。我也尝试了许多其他组合
我正在读关于newtonsoft json.net的文章.
选择令牌并决定创建或不创建错误的示例如下:
result = (string)items.SelectToken(@"$.[0]['Name']", errorWhenNoMatch: true);
引用它的页面位于https://www.newtonsoft.com/json/help/html/ErrorWhenNoMatchQuery.htm
我不知道$.确实.有谁知道它的作用?
鉴于一些json:
{
"firstName": "John",
"lastName" : "doe",
"age" : 26,
"address" : {
"streetAddress": "naist street",
"city" : "Nara",
"postalCode" : "630-0192"
},
"phoneNumbers": [
{
"type" : "Bill's Automotive",
"number": "0123-4567-8888"
},
{
"type" : "home",
"number": "0123-4567-8910"
}
]
}
Run Code Online (Sandbox Code Playgroud)
I want to return the Bill's Automotive phone number object:
{
"type" : "Bill's Automotive",
"number": "0123-4567-8888"
}
Run Code Online (Sandbox Code Playgroud)
Using Json.NET, which uses the jsonpath syntax, i have the filter expression:
phoneNumbers.[?(@.type=="Bill's Automotive")]
Run Code Online (Sandbox Code Playgroud)
And this works fine when you …
我正在尝试根据 jsonpath 表达式修改 json 数据:
{
"SchemeId": 10,
"nominations": [
{
"nominationId": 1
}
]
}
Run Code Online (Sandbox Code Playgroud)
使用类似的东西
from jsonpath_ng import jsonpath, parse
jsonpath_expr = parse('$.SchemeId')
jsonpath_expr.find(data)
updated_json = jsonpath_expr.update(data, 'schemeId': 11)
Run Code Online (Sandbox Code Playgroud)
我想更新该SchemeId值,这应该可以使用https://github.com/h2non/jsonpath-ng 进行,但是没有示例。有没有办法实现这一目标?
我正在使用Json Path库来解析 JSON。我遵循的 json 具有带空格的键:
{
"attributes": {
"First Name": "Jim",
"Last Name": "Rohn"
}
}
Run Code Online (Sandbox Code Playgroud)
为了获取 的值First Name,我编写了如下代码(json其中包含 json 的对象在哪里)-
String firstName = JsonPath.from(json).getString("attributes.First Name");
Run Code Online (Sandbox Code Playgroud)
但它会导致以下错误 -
java.lang.IllegalArgumentException: Invalid JSON expression:
Script1.groovy: 1: expecting EOF, found 'Attributes' @ line 1, column 67.
.First Name
Run Code Online (Sandbox Code Playgroud)
您能否建议如何使用json-path库获取具有空格的键的值?
我正在尝试使用 jsonpath 和以下命令获取具有特定标签的 pod 的 IP 地址:
kubectl get pods -l app=validate -n {namespace_name} -o jsonpath={.status.podIP}
但这不会产生任何结果,即使命名空间和标签名称是正确的。另一方面,如果我尝试这样做:
kubectl get pod/pod_name -n {namespace_name} -o jsonpath={.status.podIP}
之后我就可以获得 pod IP 地址。但问题是,由于我试图查询为特定部署创建的所有 pod,我想获取该特定标签下所有 pod 的 Ip 地址。我不确定该命令有什么问题。