使用JSONpath从JSON文件中提取叶子

时间:2016-10-10 12:58:26

标签: json talend jsonpath

我有一个来自REST API的JSON输出,输出如下所示:

{
"sprints": [{
    "id": 10516,
    "sequence": 10516,
    "name": "SP121 - BRK relief",
    "state": "CLOSED",
    "linkedPagesCount": 0
}, {
    "id": 10447,
    "sequence": 10447,
    "name": "SP120 - Plannibal Smith",
    "state": "CLOSED",
    "linkedPagesCount": 0
}, {
    "id": 10391,
    "sequence": 10391,
    "name": "SP119 - Don't bug or bend over",
    "state": "CLOSED",
    "linkedPagesCount": 0
}, {
    "id": 10244,
    "sequence": 10244,
    "name": "SP118 - Be an all grounder!",
    "state": "CLOSED",
    "linkedPagesCount": 0
}, {
    "id": 10183,
    "sequence": 10183,
    "name": "SP117 - The R Factor",
    "state": "CLOSED",
    "linkedPagesCount": 0
}, {
    "id": 10182,
    "sequence": 10182,
    "name": "SP116 - Deliverfull",
    "state": "CLOSED",
    "linkedPagesCount": 0
}, {
    "id": 10123,
    "sequence": 10123,
    "name": "SP115 - Appartemenneke",
    "state": "CLOSED",
    "linkedPagesCount": 0
}],
"velocityStatEntries": {
    "10516": {
        "estimated": {
            "value": 10.0,
            "text": "10.0"
        },
        "completed": {
            "value": 7.5,
            "text": "7.5"
        }
    },
    "10244": {
        "estimated": {
            "value": 15.5,
            "text": "15.5"
        },
        "completed": {
            "value": 7.5,
            "text": "7.5"
        }
    },
    "10182": {
        "estimated": {
            "value": 12.0,
            "text": "12.0"
        },
        "completed": {
            "value": 10.0,
            "text": "10.0"
        }
    },
    "10391": {
        "estimated": {
            "value": 16.0,
            "text": "16.0"
        },
        "completed": {
            "value": 3.0,
            "text": "3.0"
        }
    },
    "10183": {
        "estimated": {
            "value": 12.0,
            "text": "12.0"
        },
        "completed": {
            "value": 7.0,
            "text": "7.0"
        }
    },
    "10123": {
        "estimated": {
            "value": 11.5,
            "text": "11.5"
        },
        "completed": {
            "value": 5.5,
            "text": "5.5"
        }
    },
    "10447": {
        "estimated": {
            "value": 7.0,
            "text": "7.0"
        },
        "completed": {
            "value": 3.0,
            "text": "3.0"
        }
    }
}}

我想从velocityStatEntries中提取叶子和叶子内的信息。

所以预期的输出是这样的:

sprint_id |估计|完成 10516 | 10.0 | 7.5 10244 | 15.5 | 7.5 等

奇怪的是,当我尝试通过这个在线JSONpath测试器(jsonpath.curiousconcept.com/)这样做时,我得到了一个像这样的“$ .velocityStatEntries”的查询的预期结果。我得到了这个:

[   {  
  "10516":{  
     "estimated":{  
        "value":10,
        "text":"10.0"
     },
     "completed":{  
        "value":7.5,
        "text":"7.5"
     }
  },
  "10244":{  
     "estimated":{  
        "value":15.5,
        "text":"15.5"
     },
     "completed":{  
        "value":7.5,
        "text":"7.5"
     }
  },
  "10182":{  
     "estimated":{  
        "value":12,
        "text":"12.0"
     },
     "completed":{  
        "value":10,
        "text":"10.0"
     }
  },
  "10391":{  
     "estimated":{  
        "value":16,
        "text":"16.0"
     },
     "completed":{  
        "value":3,
        "text":"3.0"
     }
  },
  "10183":{  
     "estimated":{  
        "value":12,
        "text":"12.0"
     },
     "completed":{  
        "value":7,
        "text":"7.0"
     }
  },
  "10123":{  
     "estimated":{  
        "value":11.5,
        "text":"11.5"
     },
     "completed":{  
        "value":5.5,
        "text":"5.5"
     }
  },
  "10447":{  
     "estimated":{  
        "value":7,
        "text":"7.0"
     },
     "completed":{  
        "value":3,
        "text":"3.0"
     }
  }}]

但是因为我正在使用Talend Open Studio,我必须输入一个Loop Jsonpath查询,然后指定映射。有没有人知道如何在Talend解决这个问题?即时通讯使用tExtractJSONFields组件

编辑:一些额外信息的附加屏幕截图

作业1:

Job1:

输出1:

Output1:

无法发布更多屏幕,因为我没有足够的声誉点...... :(

1 个答案:

答案 0 :(得分:0)

使用您在问题中显示的查询$.velocityStatEntries.jsonpath.com进行测试会产生可以映射的数据:

'0' ...
  '10123' ...
    'estimated' ...
      'value' => "11.5"
      'text' => "11.5"
    'completed' ...
      'value' => "5.5"
      'text' => "5.5"
  '10182' ...
    'estimated' ...
      'value' => "12"
      'text' => "12.0"
    'completed' ...
      'value' => "10"
      'text' => "10.0"
  '10183' ...
    'estimated' ...
      'value' => "12"
      'text' => "12.0"
    'completed' ...
      'value' => "7"
      'text' => "7.0"

使用您在评论$.velocityStatEntries[*]中显示的查询测试相同的源数据会导致无法映射数据:

'0' ...
  'estimated' ...
    'value' => "11.5"
    'text' => "11.5"
  'completed' ...
    'value' => "5.5"
    'text' => "5.5"
'1' ...
  'estimated' ...
    'value' => "12"
    'text' => "12.0"
  'completed' ...
    'value' => "10"
    'text' => "10.0"
'2' ...
  'estimated' ...
    'value' => "12"
    'text' => "12.0"
  'completed' ...
    'value' => "7"
    'text' => "7.0"

我建议您再次检查查询并使用第一个查询。

修改

你似乎很亲密。在获得更多信息后,我建议您检查查询$.velocityStatEntries并提取字段[0]estimated.valuecompleted.value

我对[0]不太确定(我建议使用little different JSON schema here)。这是因为JSONPath对于自身为ID的元素不能很好地工作。 XMLPath根本无法解释这一点,因此您可以查看我的其他答案,以便进一步参考如何从ID元素描述符中收集数据。