I have a nested json object:
{
"51": {
"wheels": 10,
"id": 1,
"name": "truck"
},
"55": {
"wheels": 4,
"id": 33,
"name": "Car"
},
"88": {
"wheels": 2,
"id": 90,
"name": "Bike"
}
}
I would like to filter by ID but only return the wheels so ie.
Filter ID = 33 which would return 4.
I have tried using the .filter function but I get an error: filter is not a function which I assume is because this is not an array. I have tried to replicate using answer here:
How to filter deeply nested json by multiple attributes with vue/javascript
Without success because the json has a key (51, 55, 88) so I am stumped.
Thanks for the help in advance.
You can use Object.values to convert the object into an array and then use find method on it to retrieve the specific object. Something like:
Object.values(obj).find(val => val.id === 33)?.wheels
let obj = {
"51": {
"wheels": 10,
"id": 1,
"name": "truck"
},
"55": {
"wheels": 4,
"id": 33,
"name": "Car"
},
"88": {
"wheels": 2,
"id": 90,
"name": "Bike"
}
}
console.log(Object.values(obj).find(val => val.id === 33)?.wheels)
I have the fallowing JSON object and I want to take the value of Microsoft.VSTS.Scheduling.RemainingWork
[
{
"id": 13,
"rev": 12,
"fields": {
"System.Id": 13,
"Microsoft.VSTS.Scheduling.RemainingWork": 32,
"Microsoft.VSTS.Scheduling.CompletedWork": 20
},
"url": "https://dev.azure.com/.../_apis/wit/workItems/13"
}
]
I am able retrieve data until some point:
console.log("object of json : ",result);
console.log("result[0] : ", result[0])
console.log("result[0].fields : ", result[0].fields)
The console output is,
But I this is not working result[0].fields.Microsoft.VSTS.Scheduling.RemainingWork
You can access data like an associative array :
result[0].fields['Microsoft.VSTS.Scheduling.RemainingWork']
You need to use
result[0].fields["Microsoft.VSTS.Scheduling.RemainingWork"]
Basically when you use
result[0].fields.Microsoft.VSTS.Scheduling.RemainingWork
each time you use a ".", you are trying to get the value from a nested object, like this -
[
{
"id": 13,
"rev": 12,
"fields": {
"System.Id": 13,
"Microsoft": {
"VSTS": {
"Scheduling": {
"RemainingWork": 32
}
}
},
"Microsoft.VSTS.Scheduling.CompletedWork": 20
},
"url": "https://dev.azure.com/.../_apis/wit/workItems/13"
}
]
which is not correct since that is not the way your data is structured.
I am trying to get (via JSON Path) only key values from the JSON below but with no success.
I cannot use the tilde sign (~) because JMeter's JSON Path Extractor works under JSON PATH 4.0 and ~ is not recognized.
{
"facetCount": {
"designer": {
"4856430": 2,
"7313551": 14,
"7018102": 8,
"306838": 1,
"85146": 146,
"2654979": 11,
"221111": 4,
"180510": 40,
"3344622": 59,
"472718": 73,
"107993": 19,
"166170": 58,
"6908": 2,
"426629": 1,
"1358858": 9,
"9879178": 6,
"55006": 43,
"285396": 2,
"3355": 9,
"215501": 8,
"4968477": 4,
"11349629": 7,
"11229643": 27,
"11355128": 9,
"7093068": 3,
"11098281": 2,
"5833751": 1,
"4741301": 1,
"9198104": 21,
"991324": 4
},
"attributes": {
"135979:77": 290,
"135979:83": 27,
"136227:20": 141,
"136227:78": 670,
"135985:44": 123,
"135985:43": 669,
"135979:62": 700,
"135979:61": 1188,
"136644:176": 2,
"136331:7": 1,
"136331:8": 3,
"136641:190": 13,
"136641:191": 12,
"136061:144": 3
},
"category": {
"136103": 208,
"136105": 147,
"137322": 2,
"136389": 120,
"136215": 236,
"136214": 954,
"136216": 217,
"136217": 352,
"136218": 452,
"136219": 40,
"136480": 4,
"136220": 111,
"136221": 288,
"136222": 58,
"136223": 369,
"136224": 163,
"136986": 3,
"136307": 1125,
"136059": 10,
"136308": 956,
"136315": 984,
"136003": 574,
"136045": 267,
"136035": 1501,
"135985": 1380,
"137134": 27,
"136309": 60,
"137323": 9,
"136390": 1,
"136021": 16,
"136322": 1951,
"137166": 16,
"137317": 7,
"136005": 4,
"135983": 4019,
"136033": 1513,
"136310": 1224,
"136392": 18,
"135981": 2430,
"136031": 16,
"136326": 1312,
"136061": 79
},
"colour": {
"1": 41686,
"7": 14593,
"5": 9596,
"18": 1,
"13": 5185,
"6": 5259,
"3": 6391,
"11": 5715,
"12": 1537,
"4": 8767,
"16": 1466,
"9": 8590,
"15": 1730,
"8": 8333,
"14": 3208,
"2": 13269,
"10": 2730
},
"ninetyminutes": {
"3": 309
},
"sameday": {
"3": 1714,
"42": 254
},
"size": {
"135972:1620": 523,
"136657:2650": 1,
"136657:2850": 1
},
"location": {
"3": 2674,
"4": 7671,
"5": 35808,
"6": 2761,
"7": 11948
},
"labels": {
"1300": 2969
}
}
}
I would like to get the keys that are under facetCount element (designers, attributes, colour, etc.) and also (another JSON Path expression) get the keys that are inside these keys, such as 4856430 from designers, 135979:77 from attributes, and so on.
Could you help me, please?
Thanks in advance!
You can use JSR223 PostProcessor to get key in object JSON
new groovy.json.JsonSlurper().parse(prev.getResponseData()).facetCount.eachWithIndex{ def node, int idx ->
log.info('Key ' + idx + '=' + node.getKey())
vars.put('key_' + idx, node.getKey())
}
And you can get key with a variable like that:
${key_0}, ${key_1}, ...
More detail check here How to extract values from json in jmeter when the keys are unkown?
JMeter's JSON test elements rely on Jayway Jsonpath which doesn't has this tilde operator for querying the keys, you will have to go for:
JSR223 PostProcessor
Groovy language
JsonSlurper
Add JSR223 PostProcessor as a child of the request which returns the above JSON and use the following code:
For direct keys (designer, attributes, etc)
def counter = 1
new groovy.json.JsonSlurper().parse(prev.getResponseData()).facetCount.each { facet ->
vars.put('key_' + counter, facet.key)
counter++
}
vars.put('key_matchNr', counter - 1 as String)
For child keys (4856430, 135979:77, etc)
def counter = 1
new groovy.json.JsonSlurper().parse(prev.getResponseData()).facetCount.each { child ->
child.value.keySet().each { key ->
vars.put('childKey_' + counter, key)
counter++
}
}
vars.put('childKey_matchNr', counter -1 as String)
More information: Groovy - Parsing and producing JSON
I'm trying to make a JSONPath RegEx Match on SoapUI for the following Json Response:
{
"quantidadeItens": 5,
"registros": [
{
"identificador": 1,
"descricao": "Viagem à Disney"
},
{
"identificador": 2,
"descricao": "Carro"
},
{
"identificador": 3,
"descricao": "Smartphone novo"
},
{
"identificador": 4,
"descricao": "Casa nova"
},
{
"identificador": 5,
"descricao": "Apartamento Novo"
}
]
}
On the attached Image we can see that the JsonPath is correct, but the SoapUI is not finding the match.
I guess that the [*] is not supported on SoapUI, but I didn't find anything about it on documentation.
The expected output of your JSONPath expression would be something like:
[
1,
2,
3,
4,
5
]
This doesn't match your regex, but in any case soapUI produces [1, 2, 3, 4, 5], which the soapUI documentation says is not a JSON array but just a list of values enclosed in square brackets.
So, a regex like \[(\s?[0-9]+,?)*\s?\] will match this output:
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'];