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 ([email protected]) - 301519506662355","Inactive (ilan.dayan)","Inactive ([email protected])","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 ([email protected]) - 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": "[email protected]",
"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": "[email protected]",
"network_id": 100003163553895,
"network_type": "Facebook"
}
},
{
"id": 13766,
"name": "Inactive ([email protected])",
"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": "[email protected]",
"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": "[email protected]",
"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": "[email protected]",
"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": "[email protected]",
"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": "[email protected]",
"network_id": 100005353451967,
"network_type": "Facebook"
}
},
{
"id": 13726,
"name": "Inactive ([email protected])",
"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": "[email protected]",
"network_id": 100006176232384,
"network_type": "Facebook"
}
},
{
"id": 15134,
"name": "yoav ([email protected]) - 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": "[email protected]",
"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
, i.e. 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 ([email protected]) - 301519506662355","Inactive (ilan.dayan)","Inactive ([email protected])","Inactive (jonatan.silvester)","Inactive (markus.sogova)","Inactive (matan.hamesh)","Inactive (matan.shmone)","Inactive ([email protected])","yoav ([email protected]) - 1388742651379471"}