English 中文(简体)
我怎么能够排除阵列和物体,而不排除jq过滤器中的无效值?
原标题:How can I exclude arrays and objects without excluding null values in a jq filter?
  • 时间:2023-09-23 20:52:15
  •  标签:
  • json
  • jq

我不理解,为什么在过滤所有物体和阵列的物品时,无效价值也受到影响。

我迄今没有找到任何工作。

投入:

$ cat test.json
{
  "object": {
    "key1": 1,
    "key2": null,
    "key3": 3
  },
  "array": [ "a", null, "c" ]
}

实际行为,意想不到的无效价值产出:

$ jq  [ paths(select(type | IN("object", "array") | not)) as $path | { "($path)": getpath($path) } ] | add  test.json
{
  "["object","key1"]": 1,
  "["object","key3"]": 3,
  "["array",0]": "a",
  "["array",2]": "c"
}

预期(和预期)产出:

{
  "["object","key1"]": 1,
  "["object","key2"]": null,
  "["object","key3"]": 3,
  "["array",0]": "a",
  "["array",1]": null,
  "["array",2]": "c"
}

EDIT:

我们可以在这里看到,如果不采用过滤器,那么具有无效价值的道路就是产出:

$  jq  [ paths as $path | { "($path)": getpath($path) } ] | add  test.json
{
  "["object"]": {
    "key1": 1,
    "key2": null,
    "key3": 3
  },
  "["object","key1"]": 1,
  "["object","key2"]": null,
  "["object","key3"]": 3,
  "["array"]": [
    "a",
    null,
    "c"
  ],
  "["array",0]": "a",
  "["array",1]": null,
  "["array",2]": "c"
}
最佳回答

已经在您的海关过滤器中使用select/1。 您的做法起到了一定的作用,因为你总结了另一个<条码>。 由此可见falsy <>/em>值(<>codenull和false)。 删除<条码>。

[ paths(type | IN("object", "array") | not) as $path
  | { "($path)": getpath($path) }
] | add
{
  "["object","key1"]": 1,
  "["object","key2"]": null,
  "["object","key3"]": 3,
  "["array",0]": "a",
  "["array",1]": null,
  "["array",2]": "c"
}

Demo


根据您的具体使用情况,您可能还有兴趣将自己的投入转化为“上游代表”,该代表已经通过<代码>[第8条,价值]结构,使用<代码>tostream滤或--stream。 仅通过指数1选择回溯跟踪项目(无价值)

jq  [tostream | select(has(1)) | {("(first)"): last}] | add 
# or
jq --stream -s  map(select(has(1)) | {("(first)"): last}) | add 
# or
jq --stream -n  reduce (inputs | select(has(1))) as [$p,$v] ({}; .["($p)"] = $v) 
问题回答

暂无回答




相关问题
JQuery/MVC Search Issue

I have inherited a piece of work where the entry screen shows a summary of 20 calculated variables. E.g. Var A (250), Var B (79). Clicking on any of these links takes the user to a view with a ...

jQuery quicksearch plug-in tinkering with JSON

I ve implemented the quicksearch plugin by Rik Lomas and I love it for an application in a custom CMS I m building. I was wondering though, since I m going to have a bizillion items in the table if ...

JSON with classes?

Is there a standardized way to store classes in JSON, and then converting them back into classes again from a string? For example, I might have an array of objects of type Questions. I d like to ...

PHP json_decode question

i m trying to use json_decode to combine a few json objects and then re-encode it. my json looks like: { "core": { "segment": [ { "id": 7, "...

Converting JSON data to Java object

I want to be able to access properties from a JSON string within my Java action method. The string is available by simply saying myJsonString = object.getJson(). Below is an example of what the string ...

热门标签