how to do dynamic sized json response parsing? - json

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.

Related

Gpath for JSON with nested findAll

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?

Json object with nested objects, nested objects | mockable

I'm trying to create a test API in mockable.
What am I trying to create?
I'm trying to build an Json object with a Nested object which holds another nested object.
Example for use: store object => Store info => product list
What I expect to create
{
"Object": {
"id": 0,
"name": "Nova",
"nestedObject": {
{
"id": 1,
"name": "NestedNestedObject1",
},
{
"id": 2,
"name": "NestedNestedObject2",
},
}
Result I'm getting:
Error: Parse error on line 11:
...: { {
----------------------^
Expecting 'STRING', '}'
At NestedNestedObject2
How do I create a nested, nested object? If I'm correct mockable accepts pure Json
It depends on what you want to create and that depends on your API. The actual problem is that your JSON is not valid.
After your nestedObject there is just a { and that is wrong. In this case I assume you want to have an array of nestedObject (and perhaps also name should be nestedObjects) so fix would be (see the array []):
{
"Object": {
"id": 0,
"name": "Nova",
"nestedObject": [
{
"id": 1,
"name": "NestedNestedObject1"
},
{
"id": 2,
"name": "NestedNestedObject2"
}
]
}
}

Relation between two JSONs through keys

So I have 2 different JSONs.
Books
{
"books": [{
"name": "Lord of the rings",
"author": 1,
"year": 1937,
"genre": 3,
"imageUrl": "https://cdn.lifehack.org/wp-content/uploads/2015/03/9780618640157_custom-s6-c30.jpg",
"availability": true
}]
}
and, the other one, Authors
{
"authors": [{
"id": 1,
"name": "J.R.R. Tolkien"
}, {
"id": 2,
"name": "Harper Lee"
}, {
"id": 3,
"name": "J.K. Rowling"
}]
}
I want to have the author like an id, so when I'll display the author for the a book, I want the name(that's in the second json), not the id.
How can I do that?
Not sure if matters, but I'm working in VUE.
You could use loash's find method to do this e.g.
Documentation: https://lodash.com/docs/4.17.11#find
_.find(authors, { 'id': 1 });
I'd use the native Array.filter method, like so:
let tolkien = authors.filter(author => !!author.id === 1)
Using .filter means you don't need to include a library like lodash just for this one method.
You can create a new object with keys mapped to authors (i.e author[1] = {id: 1, ...}) which will let you quickly and easily access any author by his/her id. Example:
let authorByIds = {};
authors.forEach(author => {
authorByIds[author.id] = {...author};
});
let firstBook = books[0];
console.log(authorByIds[firstBook.author]);
// will output author with id = 1, i.e J.R.R. Tolkien

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'];

Json Slupper assert and extract

I'm using the next Json
{
"ID": 8,
"MenuItems": [
{
"ID": 38,
"Name": "Home",
"URL": "{\"PageLayout\":\"Home\",\"Icon\":\"home\"}",
"Culture": "en",
"Children": null
},
{
"ID": 534,
"Name": "GUIDE ",
"URL": "{''PageLayout'':''Page A'', ''Icon'':''A''}",
"MenuType": 1,
"PageID": 0,
"Culture": "en",
"Children": [
{
"ID": 6,
"Name": "Form A",
"URL": "[''Type'':''Form A'',''Icon'':''Form'',''ItemID'':\"358\"]",
"Culture": "he",
"RuleID": 0
},
{
"ID": 60,
"Name": "Drama",
"URL": "[''Type'':''Form B'',''Icon'':''Form'',''ItemID'':\"3759\"]",
"Culture": "en",
"RuleID": 0
}
]
}
]
}
i'm using Groovy script in soapUI and i need to:
Assert the exitance of node that has the name GUIDE
Extract a list of all Itemsid
You can parse the JSON content using JsonSlurper and then work with the results like so:
import groovy.json.JsonSlurper
// Assuming your JSON is stored in "jsonString"
def jsonContent = new JsonSlurper().parseText(jsonString)
// Assert node exists with name GUIDE
assert(jsonContent.MenuItems.Name.contains("GUIDE"))
// Get list of ItemIDs
def itemList = jsonContent.MenuItems.Children.URL.ItemID[0].toList()
// List the items
itemList.each {log.info it}
Note that the above will fail given your current example, because of a few issues. Firstly, Name contains "GUIDE " (trailing space) rather than "GUIDE" (so you will need to adapt accordingly). Secondly, it is invalid JSON; the URL nodes have various erroneous characters.
On a side note, if you first need to retrieve your JSON from the response associated with a previous TestStep (say one called "SendMessage") within the existing testCase, this can be done as follows:
def jsonString = context.testCase.getTestStepByName("SendMessage").testRequest.response.getContentAsString()