JSON structure for serializing into Dictionary - json

In an MVC controller i have below piece of code
[HttpPost]
public ActionResult One(Dictionary<Guid, int[]> groups)
{
return Json(groups);
}
What is the json structure i need to provide for it to be properly serialized into the Dictionary ?
Tried below:
{
"Key": "4e89af43-59c8-492b-b646-1185e3f8776c",
"Value": [1, 2, 3, 4, 5]
}
and
{
"groups": {
"Key": "4e89af43-59c8-492b-b646-1185e3f8776c",
"Value": [1, 2, 3, 4, 5]
}
}

If you want it to be serialized into "Dictionary", you need the following structure:
{
"Key": "Value",
"Key": "Value",
"Key": "Value"
}
In your case it will be the following object:
{
"4e89af43-59c8-492b-b646-1185e3f8776c": [1, 2, 3, 4, 5],
"4e89af43-1234-492b-b646-1185e3f8776c": [0, -1, -2],
"4e89af43-59c8-5678-b646-1185e3f8776c": [7]
}
In terms of JSON request in ASP.NET you probably will need to wrap it in a object corresponding to a variable name:
{
groups: {
"4e89af43-59c8-492b-b646-1185e3f8776c": [1, 2, 3, 4, 5],
"4e89af43-1234-492b-b646-1185e3f8776c": [0, -1, -2],
"4e89af43-59c8-5678-b646-1185e3f8776c": [7]
}
}
At least, it definitely should be so in case of Dictionary<string, int[]>.
You have a Guid as a key and I hope that ASP.NET will properly serialize Guid as a string.

Seems GUID are not meant to be serialized when inside the dictionary. Here is the error i received
So i had to change my Dictionary to below and the parameters to be sent like the next piece of code
[HttpPost]
public ActionResult One(Dictionary<string, int[]> groups)
{
return Json(groups);
}
JSON Input
{"groups":[
{
"Key": "4e89af43-59c8-492b-b646-1185e3f8776c",
"Value": [1, 2, 3, 4, 5]
}
]}
Thanks to anyone and everyone who helped

Related

How to remove brakets on Json respone

im working on Laravel Rest Api with passeport ,
in return response()->json() i want to trim the brackets
I've tried trim($json,'[]') function but it's not what i want
public function getOffers()
{
$offers = Offer::where('type', 'primary')->where('active', 1)->get();
$paks = Offer::where('type', 'pack')->where('active', 1)->get();
return response()->json([
'offersList' => $offers,
'packsList' => $paks,
], 200);
}
i expect the output will be
{
"offersList": {
{
"id": 3,
"name": "Gold",
"description": null
}
},
"packsList":[]
}
but the actual result is
{
"offersList": [
{
"id": 3,
"name": "Gold",
"description": null
}
],
"packsList":[]
}
$offers is a collection, and thus an array in JSON.
If $offers should be a single item, use first() instead of get() and it will be rendered as a single object in your JSON instead of an array of objects.
$offers = Offer::where('type', 'primary')->where('active', 1)->first();
If $offers should, at times, contain multiple offers, leave it as-is; it's correct!
Braces {} nested in another object is not valid JSON.
Objects can be used in property values and as array elements.
Not valid JSON
{
"offersList": {
{
"id": 3,
"name": "Gold",
"description": null
}
}
}
Valid option 1
{
"offersList": [
{
"id": 3,
"name": "Gold",
"description": null
}
]
}
Valid option 2
{
"offersList": {
"id": 3,
"name": "Gold",
"description": null
}
}
You can use online linters to quickly validate your JSON structure.
https://jsonformatter.curiousconcept.com/

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"
}
]
}
}

How to add custom properties to Laravel paginate json response

I have the following simple index method:
public function index()
{
// dd(Poll::paginate(2));
return response()->json(Poll::paginate(2),200);
}
The output of that method is looks like the following json object:
{
"current_page": 1,
"data": [
{
"id": 1,
"title": "I suppose?' said Alice. 'Why, you don't know.",
"created_at": "2018-09-14 16:42:11",
"updated_at": "2018-09-14 16:42:11"
},
{
"id": 2,
"title": "Alice; but she knew that it seemed quite.",
"created_at": "2018-09-14 16:42:11",
"updated_at": "2018-09-14 16:42:11"
}
],
"first_page_url": "http://127.0.0.1:8000/polls?page=1",
"from": 1,
"last_page": 6,
"last_page_url": "http://127.0.0.1:8000/polls?page=6",
"next_page_url": "http://127.0.0.1:8000/polls?page=2",
"path": "http://127.0.0.1:8000/polls",
"per_page": 2,
"prev_page_url": null,
"to": 2,
"total": 11
}
I want to add another array property after "total: 11" attribute such as:
,
"anotherData" : [
"userId": 1,
"userName": "john10",
"message": "This is a message"
]
I have tried to understand how response()->json() works, so it can extract some data from LengthAwarePaginator object which it is the output of Poll::paginate(2) from this resource, but I could not able to understand how does it able to get an array from LengthAwarePaginator that holds the resultant keys in the json object?!
From the regarded resource above, the json() method takes an array and, I guess that, if the parameter is not an array, it tries to convert it to array, specially if it is an object like LengthAwarePaginator, so it may use toArray() method.
I have tried replacing return response()->json(Poll::paginate(2),200) with return response()->json(Poll::paginate(2)->toArray,200), then I have got the same output. Hence, I have decided to replace the code of my index method to be like the following:
public function index()
{
//dd(Poll::paginate(2)->toArray());
$output = Poll::paginate(2)->toArray();
$output['userData'] = ['userId' => \Auth::user()->id, 'userEmail' => \Auth::user()->email];
return response()->json($output,200);
}
The resultant output is:
...
"path": "http://127.0.0.1:8000/polls",
"per_page": 2,
"prev_page_url": null,
"to": 2,
"total": 11,
"userData": {
"userId": 1,
"userEmail": "lion#example.com"
}
}
If you're directly responding with the paginated data, a better option would be to wrap it in a Resource Collection and adding meta data to it.
This is how I implemented it.
CategoryController.php
public function index()
{
return new CategoryCollection(Category::paginate(10));
}
CategoryCollection.php
public function toArray($request)
{
return [
'status' => 1,
'message' => 'Success',
'data' => $this->collection,
];
}
Since Collection extends JsonResource, your response would automatically be converted to JSON when returned from the Controller.

Matching arrays in soapUI JSONPath RegEx Match assertions

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:

How to compare request body in JSON and response body in JSON?

This is the example body we will be requesting and also received in response.
Please advise on how to validate the response body against request body
"Details": {
"PaymentRequested": {
"Transaction_Reference": "string",
"Final_Reference": "string",
"Payment Details": {
"Amount": "0.10",
"Currency": "INR" }
}
}
If you use Karate, you can compare any JSON with any other JSON in one step. So this will be very easy.
* def json = { foo: 'world', hey: 'ho', zee: [1, 2, 3] }
* remove json.hey
* match json == { foo: 'world', zee: [1, 2, 3] }