Gpath for JSON with nested findAll - json

I have the following response payload
[{
"id": 1,
"catname": "Cat01",
"items": [{
"Name": "Item01",
"id": 2
}, {
"Name": "Item02",
"id": 3
}]
},
{
"id": 4,
"catname": "Cat02",
"items": [{
"Name": "Item03",
"id": 5
}, {
"Name": "Item04",
"id": 6
}]
},
{
"id": 7,
"catname": "Cat03",
"items": [{
"Name": "Item05",
"id": 8
}]
}
]
I want to retrieve a list of all the items.ids (but not the ids of their parents), So this: [2, 3, 5, 6, 8].
I've tried this findAll{it}.items.findAll{it}.id but it doesn't work. Any help would be welcomed. Thanks!

You don't need to use findAll here to iterate, and you can make use of collectMany to automatically flatten the list
Assuming your parsed json is in a variable json, you can simply do:
json.items.collectMany { it.id }

Never mind, I found the answer :).
I needed to use flatten here
items.flatten().id
did the trick.
I got the answer from here: How to search in anonymous and nested array using find or findAll in groovy's closures using REST-Assured library?

Related

Azure ADF - Array elements can only be selected using an integer index

Hi I am trying to select Status from Json Array in azure data factory
{
"dataRead": 2997,
"dataWritten": 2714,
"filesWritten": 1,
"sourcePeakConnections": 1,
"sinkPeakConnections": 1,
"rowsRead": 11,
"rowsCopied": 11,
"copyDuration": 3,
"throughput": 0.976,
"errors": [],
"effectiveIntegrationRuntime": "DefaultIntegrationRuntime (East US)",
"usedDataIntegrationUnits": 4,
"billingReference": {
"activityType": "DataMovement",
"billableDuration": [
{
"meterType": "AzureIR",
"duration": 0.06666666666666667,
"unit": "DIUHours"
}
]
},
"usedParallelCopies": 1,
"executionDetails": [
{
"source": {
"type": "AzureSqlDatabase",
"region": "East US"
},
"sink": {
"type": "AzureBlobStorage",
"region": "East US"
},
"status": "Succeeded",
"start": "2020-03-19T06:24:39.0666585Z",
"duration": 3,
"usedDataIntegrationUnits": 4,
"usedParallelCopies": 1,
I have tried selecting #activity('Copy data From CCP TO Blob').output.executionDetails.status.It throws an error:
'Array elements can only be selected using an integer index'.
Any way to resolve it?
executionDetails is an array, you have to set index to refer elements in it.
Please try:
#activity('Copy data From CCP TO Blob').output.executionDetails[0].status
Thank you for the reply
Yes, we have to use slicing and indexing the lists and Dictionaries
I have tried Dispensing_Unit_Master_Dim
#activity('Copy data From CCP TO Blob').output.executionDetails[0]['status'] and it works
0 and status there is no Dot

How to Append JSON Object in already created object in mysql json document

My object is
{
"name":"Testing",
"id": "hcig_3fe7cb00-e936-11e6-af69-a748c8cc89ad",
"belongsTo": {
"id": "69616d26-c3bb-405c-8c84-c51c091524b2",
"name": "test"
},
"locatedAt": {
"id": "49616d26-c3bb-405c-8c84-c51c091524b2",
"name":"Test"
} }
I want to merge one more object like
"obj":[{
"a": 123
}}
With the help of JSON_MERGE in mysql document store i am able to add object.
But it looks likes
{
"name":"Tester",
"id": "hcig_3fe7cb00-e936-11e6-af69-a748c8cc89ad",
"belongsTo": {
"id": "69616d26-c3bb-405c-8c84-c51c091524b2",
"name": "test"
},
"locatedAt": {
"id": "49616d26-c3bb-405c-8c84-c51c091524b2",
"name":"Test"
},{
"obj":[{
"a": 123
}]
}}
I want my object to be as
{
"name": "Tester",
"id": "hcig_3fe7cb00-e936-11e6-af69-a748c8cc89ad",
"belongsTo": {
"id": "69616d26-c3bb-405c-8c84-c51c091524b2",
"name": "test"
},
"locatedAt": {
"id": "49616d26-c3bb-405c-8c84-c51c091524b2",
"name": "Test"
},
"obj": [{
"a": 123
}]}
Any idea on how to add object as above manner using JSON Functions in mysql ??
Use lodash for a recursive deep copy - https://lodash.com/
lodash.merge(targetObj, sourceObj);
Or if you have programmatic access:
targetObj.obj = sourceObj;

Remove pivote attribute from JSON response in Laravel 5.2

I'm using many to many relationships using pivot table. But the problem is when I'm returning json response, the json also contains the pivot attribute as shown below:
{
"id": 2,
"job_title": "et",
"job_description": "Iusto provident.",
"job_industry": "Braun, Jast and Quigley",
"job_location": "Christiansenland",
"job_experience": 7,
"employment_type": "full",
"recruiter_id": 9,
"status": 1,
"posted_date": "2016-02-02 07:55:28",
"skills": [
{
"id": 1,
"value": "molestiae",
"pivot": {
"job_id": 2,
"skill_id": 1
}
}
]
}
What I want is something like this:
{
"id": 2,
"job_title": "et",
"job_description": "Iusto provident.",
"job_industry": "Braun, Jast and Quigley",
"job_location": "Christiansenland",
"job_experience": 7,
"employment_type": "full",
"recruiter_id": 9,
"status": 1,
"posted_date": "2016-02-02 07:55:28",
"skills": [
{
"id": 1,
"value": "molestiae",
}
]
}
I have tried various solutions from the stackoverflow questions, but none of them seem to work. I'm new to Laravel. If you guys need more info on the model,I can post it. Please help.
Go to your Skill model and set:
protected $visible = ['id', 'value'];

how to do dynamic sized json response parsing?

I have a http get call which will populate the entire student list with id, name and class. Can anyone help me on how to find the id of the kid for a name?
The response will be in the below mentioned format.
[
{
"id": 1,
"SchoolName": "Cp",
"StudentList": [
{
"id": 1,
"name": "jerin",
"class": 1,
},
{
"id": 2,
"name": "john",
"class": 2,
}
]
}
]
This will be as big as the number of students. If i need to find the id of Jerin, how to proceed ?
This can be solved easily.
This is a json array. so we can use jsonArray and we can parse through it.

How to input nested json to jquery tokeninput?

I use jquery tokeninput.
A flatted json is no problem to be searched. but howto input nested object.
my code:
var flat_obj = [{id: 7, name: "Ruby"}, {id: 11, name: "Python"}];
var nested_obj = [{ "name": "main", "id": "2", "Parent": "0", "children": [{ "name": "submain", "id": "3", "Parent": "2"}] }];
$("#search-input-local").tokenInput(nested_obj, { });
TokenInput doesn't handle nested JSON, you will need to format the JSON correctly before you pass it to the plugin.
There are a number of libraries to flatten JSON, or take a look at this question here.