JSONAARAY Parsing when dynamic key of object - json

I am trying to parse json that has dynamic key values. Here is my json format.
Works with flutter
{
"status": "success",
"data": [
{
"accepted": [
{
"id": 15,
"emp_id": 3,
}
]
},
{
"negotiate": [
{
"id": 15,
"emp_id": 3,
}
]
},
]
}
here accepted and negotiate is dynamic key.

Related

parsing json file store in Map

I have a json file like this and I want to save both "id" and "tenantId" in some map as key/value pair to process further. Is there a way I can parse my JSON to do that?
{
"type": "nodesRepresentation",
"paging": {
"cursorAfter": "NjA3YmUyMjA4ZjdkMTMwMDAxMDc1MDlk",
"limit": 50,
"pageSize": 2,
"links": [
{
"href": "/api/node/nodes%3Fafter=NjA3YmUyMjA4ZjdkMTMwMDAxMDc1MDlk&limit=50",
"rel": "next"
}
]
},
"pageSize": 2,
"values": [
{
"id": "a423a565-8ae3-4d99-9c8a-92b8af843228",
"tenantId": "4d010b5a-a9ab-4e7d-b989-6a05245105ed"
},
{
"id": "e350ffaa-d62e-4cfc-9a3b-ae4624d35dc0",
"tenantId": "8725905d-90c4-4728-a94a-47c0d57f8251"
}
]
}
You could use jq. Since you haven’t specified precisely the result you want, I’d suggest starting with
jq ‘.values[]' myfile.json

I need assistance in building my own json schema

How do I build my own json schema to validate the json coming back from an api is the same structure? I have this sample JSON
{
"httpStatus": 200,
"httpStatusMessage": "success",
"timestamp": "2020-11-11T19:32:45",
"response": {
"header": {
"SchoolId": 10006,
"SchoolName": "Naples"
},
"body": {
"dataProviders": [
{
"dataProviderId": 14,
"students": [
{
"studentId": 1000611000,
"driverGrade": "Junior",
"firstName": "Authur",
"lastName": "Boccuto"
},
{
"studentId": 1000611001,
"studentGrade": "Senior",
"firstName": "Antwan",
"lastName": "Carter"
}
]
}
]
}
}
}
At times it can come in with a different structure and I need to build my own json schema to validate that it's the same before manipulating the json data. How do I build my own schema to make sure that it has a valid structure?

How we can add Name of a restful response result List

I have a service which is producing List of results I want to give the name of each list,find the Example.
Current Result:
[
[
{
"marketname": "East",
"totalmarketcount": 22
},
{
"marketname": "Midwest",
"totalmarketcount": 9275
},
{
"marketname": "West",
"totalmarketcount": 25
}
],
[
{
"discarded": 0,
"received": 1268,
"coded": 6031,
"completed": 5951
}
],
But I want :
{
"EncoderCodedlist": [
{
"totalcount": 6001,
"encountercoder": "name1"
},
{
"totalcount": 41,
"encountercoder": "name8"
},
{
"totalcount": 6,
"encountercoder": "name6"
},
{
"totalcount": 3,
"encountercoder": "name4"
},
{
"totalcount": 3,
"encountercoder": "name2"
}
]
}
I am using Spring Boot and Spring rest, which we produce the json object.

Return JSON data with predefined key Yii2

I use Yii2 and I created RESTful API service. But in my app I want to return JSON data with predefined key for all responses. For example:
Default respons:
[
{
"id": 1,
"title": "Brooklyn"
},
{
"id": 2,
"title": "Financial District"
},
{
"id": 4,
"title": "Social District"
}
]
But I want to get smth like that:
"data": [
{
"id": 1,
"title": "Brooklyn"
},
{
"id": 2,
"title": "Financial District"
},
{
"id": 4,
"title": "Social District"
}
]
You should simply customize rest serializer, in your controller :
public $serializer = [
'class' => 'yii\rest\Serializer',
'collectionEnvelope' => 'data',
];
Read more.

Linq to Json using Like Clause

I've got an MVC 3 web app and am returning a JSON object which I would like to use Linq against to limit the result returned to the client jquery.
The Json response takes the following structure:
{
"Data": {
"Items": [
{
"Result": {
"Id": "e2ba4912-c387-4f54-b55e-06742a6858db",
"Name": "SomeOtherSetting",
"Value": "500",
"Archived": false
},
"Result": {
"Id": "17c27584-cea8-42c2-b6c4-0b30625ac3ce",
"Name": "Setting2",
"Value": "600",
"Archived": false
},
"Result": {
"Id": "17c27584-cea8-42c2-b6c4-0b30625ac3ce",
"Name": "Setting3",
"Value": "700",
"Archived": false
}
}]
}
}
....
I need to return or grab just the json items that have a Name like 'Setting' for example. In the example, this would return just the 2 Json nodes.
My Linq is very limited and what I have is: Settings is where the Json response is stored.
NewtonSoft.Json.Linq.JObject data = NewtonSoft.Json.Linq.JObject.Parse(Settings);
var result = from p in data["Data"]["Items"].Children()
where (p["Result"]["Name"].Contains("Expenses.Help.Tip"))
select new { Name = (string)p["Result"]["Name"], Value = (string)p["Result"]["Value"] };
When I run this I get nothing in my result. Can anyone help and tell me what I'm doing wrong?
Thanks.
Well, I'm not a Json specialist, but I think your Json structure has some problems.
I tried an online parser, and parsing took only the third result... (try to copy past your code in the left window, and look at JS eval.
Instead of
"Items": [
{
"Result": {
},
"Result": {
},
"Result": {
}
}]
you should have each element of Items array (each 'Result') into {}.
"Items": [
{
{"Result": {
}
},
{"Result": {
}
},
{"Result": {
}
}]
I got it to work by changing your Json file to
{
"Data": {
"Items": [
{
"Result": {
"Id": "e2ba4912-c387-4f54-b55e-06742a6858db",
"Name": "SomeOtherSetting",
"Value": "500",
"Archived": false
}
},
{
"Result": {
"Id": "17c27584-cea8-42c2-b6c4-0b30625ac3ce",
"Name": "Setting2",
"Value": "600",
"Archived": false
}
},
{
"Result": {
"Id": "17c27584-cea8-42c2-b6c4-0b30625ac3ce",
"Name": "Setting3",
"Value": "700",
"Archived": false
}
}]
}
}
using
var data = JObject.Parse(test)["Data"]["Items"].Children()
.Where(m => m["Result"]["Name"].Value<string>().Contains("Setting"))
.Select(m => new
{
Name = m["Result"]["Name"].Value<string>(),
Value = m["Result"]["Value"].Value<int>()
})
.ToList();