Extract leaves from JSON file with JSONpath

2020-05-04 15:07发布

I have a JSON output from an REST API and the output looks like this:

{
"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"
        }
    }
}}

i would like to extract the leaves AND the info inside the leaves from velocityStatEntries.

so the expected output would be this:

sprint_id | estimated | completed 10516 | 10.0 | 7.5 10244 | 15.5 | 7.5 etc.

the strange thing is when i try to do this thru this online JSONpath tester (jsonpath.curiousconcept.com/) i get the expected result with a query like this "$.velocityStatEntries." there i get this:

[   {  
  "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"
     }
  }}]

but because i am using Talend Open Studio, i have to enter a Loop Jsonpath query and then specify the mapping. does anyone know how to fix this in Talend? im using the tExtractJSONFields component

EDIT: some additional screenshots for extra information

Job1:

Job1:

Output1:

Output1:

Cannot post more screens because i do not have enough reputation points...:(

1条回答
太酷不给撩
2楼-- · 2020-05-04 15:16

Testing on jsonpath.com with the query $.velocityStatEntries. you showed in the question results in data which could be mapped:

'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"

Testing the same source data with the query you showed in the comments $.velocityStatEntries[*] results in no data to map:

'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"

I suggest checking your query again and using the first one.

EDIT

You seem very close. After more information is available I suggest you check the query $.velocityStatEntries and extract the fields [0], estimated.value and completed.value.

I am not too sure about the [0] (I have suggested this with a little different JSON schema here). This is because JSONPath does not work well with elements which are IDs for themselves. XMLPath can't interpret this at all, so you might check my other answer for further reference in how to gather data from element descriptors which are IDs.

查看更多
登录 后发表回答