-1

i have a json structure like that, where i would like to take from the json path the "id", but when i run the JSON input method in pentaho, putting "$..id" as the path, it returns me two lines about the same person, one containing the first id and the second one containing the second id (from the "photo"). What type of path should i use to take just the first id? ("id":"63b81795-83f0-4ffc-8ec3-6acc0236fcb5")

"profile":  {"email":"joao.luiz@clinicasim.com",                 
             "id":"63b81795-83f0-4ffc-8ec3-6acc0236fcb5",                                      
             "mobile":"85985119032",                                      
             "name":"João Luiz Sampaio da Silva"                                     
             "photo":{"filename":"20210610_152453.jpg",
                      "id":"cdee6e2a-5bf7-4957-b5ee-3a82e1fc9f55",                             
                      "mimetype":"image/jpeg",
                      "path":"individual/ebf08be7-dfc5-4e24-97c6-abe3e5b56342/document/0dc3f588-ef55-41d8-bc33-1c88d1542df8/cdee6e2a-5bf7-4957-b5ee-3a82e1fc9f55.jpg"}} 

1 Answers1

0

json path always returns data as an array,even if it is just one item, so you have to take the first element

var id = jsonPath(profile , "$.profile.id")[0]; //63b81795-83f0-4ffc-8ec3-6acc0236fcb5

to get a nested id

var id = jsonPath(profile , "$['profile']['photo'].id")[0]; //cdee6e2a-5bf7-4957-b5ee-3a82e1fc9f55
//or 
var id = jsonPath(profile , "$..id")[1];
Serge
  • 40,935
  • 4
  • 18
  • 45
  • I am not sure of _"json path always returns data as an array"_ .The index will be not be required for the first two expression. JsonPath will automatically try to cast the result to the type expected by the invoker. Only [indefinite path](https://github.com/json-path/JsonPath#what-is-returned-when) always returns an array. – Akshay G Apr 01 '22 at 12:37
  • 1
    @AkshayG There are tonns of jsonpath libraries. You have to show in your post that you are using jsonpath for java, not for javascript, or c#. – Serge Apr 01 '22 at 12:42
  • Yeah , I just verified with original spec it does return array! BTW I am not the OP – Akshay G Apr 01 '22 at 12:43
  • 1
    @AkshayG And BTW Nobody uses a jsonPath without a seacrh conditions, this is it was created for. it is much easier to get a value by key if you need just a value of a key. – Serge Apr 01 '22 at 12:46