简体   繁体   中英

JsonPath fields extraction, in Talend 5.5

I have the below JSON String, and I need to extract only the list of the id field and name field, The expected output for the ids would be:

{15124,10287,13766,13568...etc}

And the expected output for the names would be:

{" yoav (yoavshaki@yahoo.com) - 301519506662355","Inactive (ilan.dayan)","Inactive (joelpeleh@yahoo.com)","Inactive (jonatan.silvester)"..etc}

I tried this path: $.response.data.*.id but I got the list of the ids inside the key of "timezone" which I don't need.

This is the JSON String:

{ "response": {
  "code": 200,
  "msg": "Success",
  "data": [
    {
      "id": 15124,
      "name": " yoav (yoavshaki@yahoo.com) - 301519506662355",
      "network_id": 301519506662355,
      "network_type": "Facebook",
      "currency": "USD",
      "currency_info": {
        "prefix": "$",
        "postfix": "",
        "name": "US Dollars"
      },
      "timezone": {
        "id": 139,
        "code": "IST",
        "region": "Asia",
        "locality": "Jerusalem",
        "offset": 2,
        "facebook_code": 70
      },
      "owner": {
        "name": "Ido Levi",
        "email": "yoavshaki@yahoo.com",
        "network_id": 100004129233318,
        "network_type": "Facebook"
      }
    },
    {
      "id": 10287,
      "name": "Inactive (ilan.dayan)",
      "network_id": 111979298917521,
      "network_type": "Facebook",
      "currency": "USD",
      "currency_info": {
        "prefix": "$",
        "postfix": "",
        "name": "US Dollars"
      },
      "timezone": {
        "id": 139,
        "code": "IST",
        "region": "Asia",
        "locality": "Jerusalem",
        "offset": 2,
        "facebook_code": 70
      },
      "owner": {
        "name": "Ilan Sloto",
        "email": "ilan.dayan@walla.com",
        "network_id": 100003163553895,
        "network_type": "Facebook"
      }
    },
    {
      "id": 13766,
      "name": "Inactive (joelpeleh@yahoo.com)",
      "network_id": 1405567046335275,
      "network_type": "Facebook",
      "currency": "USD",
      "currency_info": {
        "prefix": "$",
        "postfix": "",
        "name": "US Dollars"
      },
      "timezone": {
        "id": 139,
        "code": "IST",
        "region": "Asia",
        "locality": "Jerusalem",
        "offset": 2,
        "facebook_code": 70
      },
      "owner": {
        "name": "Joel Peleh",
        "email": "joelpeleh@yahoo.com",
        "network_id": 100006459791435,
        "network_type": "Facebook"
      }
    },
    {
      "id": 13568,
      "name": "Inactive (jonatan.silvester)",
      "network_id": 125737960970262,
      "network_type": "Facebook",
      "currency": "USD",
      "currency_info": {
        "prefix": "$",
        "postfix": "",
        "name": "US Dollars"
      },
      "timezone": {
        "id": 92,
        "code": "PST",
        "region": "America",
        "locality": "Los_Angeles",
        "offset": -7,
        "facebook_code": 1
      },
      "owner": {
        "name": "Jonatan Silvester",
        "email": "jonatan.silvester@gmail.com",
        "network_id": 100006021764102,
        "network_type": "Facebook"
      }
    },
    {
      "id": 13472,
      "name": "Inactive (markus.sogova)",
      "network_id": 127771634098217,
      "network_type": "Facebook",
      "currency": "USD",
      "currency_info": {
        "prefix": "$",
        "postfix": "",
        "name": "US Dollars"
      },
      "timezone": {
        "id": 92,
        "code": "PST",
        "region": "America",
        "locality": "Los_Angeles",
        "offset": -7,
        "facebook_code": 1
      },
      "owner": {
        "name": "Marcus Sogova",
        "email": "markus.sogova@gmail.com",
        "network_id": 100005961429701,
        "network_type": "Facebook"
      }
    },
    {
      "id": 13052,
      "name": "Inactive (matan.hamesh)",
      "network_id": 110032855847424,
      "network_type": "Facebook",
      "currency": "USD",
      "currency_info": {
        "prefix": "$",
        "postfix": "",
        "name": "US Dollars"
      },
      "timezone": {
        "id": 139,
        "code": "IST",
        "region": "Asia",
        "locality": "Jerusalem",
        "offset": 2,
        "facebook_code": 70
      },
      "owner": {
        "name": "",
        "email": "matan.hamesh@walla.com",
        "network_id": 100005221303485,
        "network_type": "Facebook"
      }
    },
    {
      "id": 13010,
      "name": "Inactive (matan.shmone)",
      "network_id": 110145245840590,
      "network_type": "Facebook",
      "currency": "USD",
      "currency_info": {
        "prefix": "$",
        "postfix": "",
        "name": "US Dollars"
      },
      "timezone": {
        "id": 139,
        "code": "IST",
        "region": "Asia",
        "locality": "Jerusalem",
        "offset": 2,
        "facebook_code": 70
      },
      "owner": {
        "name": "",
        "email": "matan.shmone@walla.com",
        "network_id": 100005353451967,
        "network_type": "Facebook"
      }
    },
    {
      "id": 13726,
      "name": "Inactive (matinsonk@rediffmail.com)",
      "network_id": 1406755166207011,
      "network_type": "Facebook",
      "currency": "USD",
      "currency_info": {
        "prefix": "$",
        "postfix": "",
        "name": "US Dollars"
      },
      "timezone": {
        "id": 92,
        "code": "PST",
        "region": "America",
        "locality": "Los_Angeles",
        "offset": -7,
        "facebook_code": 1
      },
      "owner": {
        "name": "Venita Vagt",
        "email": "matinsonk@rediffmail.com",
        "network_id": 100006176232384,
        "network_type": "Facebook"
      }
    },
    {
      "id": 15134,
      "name": "yoav (zoltantimov@gmx.com) - 1388742651379471",
      "network_id": 1388742651379471,
      "network_type": "Facebook",
      "currency": "USD",
      "currency_info": {
        "prefix": "$",
        "postfix": "",
        "name": "US Dollars"
      },
      "timezone": {
        "id": 10,
        "code": "CET",
        "region": "Europe",
        "locality": "Sofia",
        "offset": 3,
        "facebook_code": 19
      },
      "owner": {
        "name": "Zoltan Timov",
        "email": "zoltantimov@gmx.com",
        "network_id": 100007312476197,
        "network_type": "Facebook"
      }
    }
  ]
} }

What jsonpath pattern should I send?

With the $.response.data.*.id pattern, you were requesting all the *id*s under the data node which would include the timezone id.

The correct pattern to use would be $.response.data.[*].id and $.response.data.[*].name , ie select all id ( name ) value which are direct siblings of the data node.

The following snippet would illustrate the case:

public class ResponseService
{
  public static void main(String[] args)
  {
    final String OPENING_CURLY = "{";
    final String CLOSING_CURLY = "}";
    final String COLON = ",";
    final String QUOTE = "\"";

    String jsonData; // Load you response json feed.
    List<Object> ids = JsonPath.read(jsonData, "$.response.data[*].id");
    StringBuilder formattedIds = new StringBuilder(OPENING_CURLY);
    for (Iterator it = ids.iterator(); it.hasNext();)
    {
      formattedIds.append(it.next());
      if (it.hasNext())
      {
        formattedIds.append(COLON);
      }
    }
    formattedIds.append(CLOSING_CURLY);
    System.out.println(formattedIds);

    List<Object> names = JsonPath.read(jsonData, "$.response.data[*].name");
    StringBuilder formattedNames = new StringBuilder(OPENING_CURLY);
    for (Iterator it = names.iterator(); it.hasNext();)
    {
      formattedNames.append(QUOTE)
        .append(it.next())
        .append(QUOTE);
      if (it.hasNext())
      {
        formattedNames.append(COLON);
      }
    }
    formattedNames.append(CLOSING_CURLY);
    System.out.println(formattedNames);
  }
}

The above code will result in below output:

{15124,10287,13766,13568,13472,13052,13010,13726,15134}
{" yoav (yoavshaki@yahoo.com) - 301519506662355","Inactive (ilan.dayan)","Inactive (joelpeleh@yahoo.com)","Inactive (jonatan.silvester)","Inactive (markus.sogova)","Inactive (matan.hamesh)","Inactive (matan.shmone)","Inactive (matinsonk@rediffmail.com)","yoav (zoltantimov@gmx.com) - 1388742651379471"}

Simple way using json.org

String str ="yourJsonString";
List<String> listOfIds = new ArrayList<String>();
JSONObject jsonObject =new JSONObject(str);
String jsonStr = jsonObject.get("response").toString();
JSONObject jsonObj =new JSONObject(jsonStr);
String jsonS = jsonObj.get("data").toString();
JSONArray jsonArray = new JSONArray(jsonS);
for(int i =0;i<jsonArray.length();i++){
    JSONObject object = new JSONObject(jsonArray.get(i).toString());
    listOfIds.add(object.get("id").toString());
}
System.out.println(listOfIds);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM