Related
I want to make a schema of json file.It's for an array of products.
The json schema is similar as below:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Product set",
"type": "array",
"items": {
"title": "Product",
"type": "object",
"properties": {
"id": {
"description": "The unique identifier for a product",
"type": "number"
},
"name": {
"type": "string"
},
"price": {
"type": "number",
"minimum": 0,
"exclusiveMinimum": true
},
"tags": {
"type": "array",
"items": {
"type": "string"
},
"minItems": 1,
"uniqueItems": true
},
"dimensions": {
"type": "object",
"properties": {
"length": {"type": "number"},
"width": {"type": "number"},
"height": {"type": "number"}
},
"required": ["length", "width", "height"]
},
"warehouseLocation": {
"description": "Coordinates of the warehouse with the product",
"$ref": "http://json-schema.org/geo"
}
},
"required": ["id", "name", "price"]
}
}
The array should at least one item in it. How can I define the minimum of the array?
Do I need to add the minimun defination?
To set the minimum # of item in an array, use the "minItems".
See:
https://datatracker.ietf.org/doc/html/draft-fge-json-schema-validation-00#section-5.3.3
and
http://jsonary.com/documentation/json-schema/?section=keywords/Array%20validation
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Product",
"description": "A product from Acme's catalog",
"type": "object",
"properties": {
...
"tags": {
"type": "array",
"items": {
"type": "string"
},
"minItems": 1,
"maxItems": 4,
"uniqueItems": true
}
},
"required": ["id", "name", "price"]
}
It looks like draft v4 permits what you are looking for. From http://json-schema.org/example1.html:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Product",
"description": "A product from Acme's catalog",
"type": "object",
"properties": {
...
"tags": {
"type": "array",
"items": {
"type": "string"
},
"minItems": 1,
"uniqueItems": true
}
},
"required": ["id", "name", "price"]
}
Notice that the "tags" property is defined as an array, with a minimum number of items (1).
I suppose no, at least looking to working draft the minimum is applied only for numeric values, not arrays.
5.1. Validation keywords for numeric instances (number and integer)
...
5.1.3. minimum and exclusiveMinimum
So you should be good with min/maxItems for arrays.
This is my first post.
I am trying to integrate Shippo to our application and wish to obtain a specific value out of a Json response.
I am getting the following Json response:
{
"count": 3,
"next": null,
"previous": null,
"results": [
{
"object_created": "2019-08-28T12:58:57.064Z",
"object_id": "16b602e0ajdsk87c4313920bc5e3174XYZ",
"object_owner": "some#email.com",
"shipment": "bd62234e151244dab2b2152fdcd15e76",
"attributes": [
"FASTEST"
],
"amount": "31.30",
"currency": "USD",
"amount_local": "31.30",
"currency_local": "USD",
"provider": "USPS",
"provider_image_75": "https://shippo-static.s3.amazonaws.com/providers/75/USPS.png",
"provider_image_200": "https://shippo-static.s3.amazonaws.com/providers/200/USPS.png",
"servicelevel": {
"name": "Priority Mail Express",
"token": "usps_priority_express",
"terms": ""
},
"estimated_days": 1,
"arrives_by": null,
"duration_terms": "Overnight delivery to most U.S. locations.",
"messages": [],
"carrier_account": "4e1506b8b7f7449e90620967e45aa1e9",
"test": false,
"zone": "4"
},
{
"object_created": "2019-08-28T12:58:57.063Z",
"object_id": "ebdee42047aa49a3b7e08b1903ea02ea",
"object_owner": "some#email.com",
"shipment": "bd62234e151244dab2b2152fdcd15e76",
"attributes": [
"BESTVALUE",
"CHEAPEST"
],
"amount": "7.49",
"currency": "USD",
"amount_local": "7.49",
"currency_local": "USD",
"provider": "USPS",
"provider_image_75": "https://shippo-static.s3.amazonaws.com/providers/75/USPS.png",
"provider_image_200": "https://shippo-static.s3.amazonaws.com/providers/200/USPS.png",
"servicelevel": {
"name": "Priority Mail",
"token": "usps_priority",
"terms": ""
},
"estimated_days": 2,
"arrives_by": null,
"duration_terms": "Delivery within 1, 2, or 3 days based on where your package started and where it’s being sent.",
"messages": [],
"carrier_account": "4e1506b8b7f7449e90620967e45aa1e9",
"test": false,
"zone": "4"
},
{
"object_created": "2019-08-28T12:58:57.062Z",
"object_id": "ad410a41c84940ee80eb30c41c507613",
"object_owner": "some#email.com",
"shipment": "bd62234e151244dab2b2152fdcd15e76",
"attributes": [],
"amount": "7.78",
"currency": "USD",
"amount_local": "7.78",
"currency_local": "USD",
"provider": "USPS",
"provider_image_75": "https://shippo-static.s3.amazonaws.com/providers/75/USPS.png",
"provider_image_200": "https://shippo-static.s3.amazonaws.com/providers/200/USPS.png",
"servicelevel": {
"name": "Parcel Select",
"token": "usps_parcel_select",
"terms": ""
},
"estimated_days": 7,
"arrives_by": null,
"duration_terms": "Delivery in 2 to 8 days.",
"messages": [],
"carrier_account": "4e1506b8b7f7449e90620967e45aa1e9",
"test": false,
"zone": "4"
}
]
}
I am using this call:
WebClient webClient = new WebClient();
webClient.Headers.Add("Authorization: ShippoToken " + authToken);
var result = JsonValue.Parse(webClient.DownloadString("https://api.goshippo.com/shipments/"+ theObject.ObjectId + "/rates/USD"));
My question is how could I get just the "amount" value from the response?
Thank you for any help!
Instead of recreating the json layout as a class model you can work with Newtonsofts JToken. Which will leave you with less boilerplate code to create:
var json = "your json data";
// Parse the whole json string
var obj = JObject.Parse(json);
// Extract the results data as JArray
var results = (JArray)obj["results"];
// Iterate over all array entries and get the amount property
foreach(var result in results)
{
var amount = result["amount"].ToString();
// Do something with the amount
}
You could create some classes based on json then deserialize using JsonConvert
e.g https://www.newtonsoft.com/json/help/html/DeserializeObject.htm
tip:
Copy the json and put in json viewer like https://jsonformatter.curiousconcept.com/
Use https://www.newtonsoft.com/json to deserialize the json response. If you didn't want that you could use a series of string.Split()'s to get the amount. But using newtonsoft (via the nuget package manager) would be the easiest.
I have a Cosmos DB with a collection with 134k documents and every document contains 2 string properties (named ChemicalName and EchNo) and 3 string collections (named CasNos, IndexNos and ReachNos).
Document JSON:
{
"id": "9cb924ad-a100-4fce-9dc9-30ec968de585",
"ChemicalName": "MyChemical",
"EchaNumber": 1,
"EcNo": "111-222-3",
"CasNos": [
"4444-55-6"
],
"IndexNos": [
"777-888-99-0"
],
"ReachNos": [
"01-0123456789-38-0000",
"01-0123456789-38-0001",
"01-0123456789-38-0002",
"01-0123456789-38-0003",
"01-0123456789-38-0004",
"01-0123456789-38-0006",
"01-0123456789-38-0007",
"01-0123456789-38-0008",
"01-0123456789-38-XXXX"
],
"IndexScrapeDate": "2018-01-19T12:30:20.0404174Z",
"ChemicalScrapeDate": null,
"_rid": "96VvAOgIVwABAAAAAAAAAA==",
"_self": "dbs/96VvAA==/colls/96VvAOgIVwA=/docs/96VvAOgIVwABAAAAAAAAAA==/",
"_etag": "\"00007e4d-0000-0000-0000-5a69b49b0000\"",
"_attachments": "attachments/",
"_ts": 1516876955
}
Collection have index definition:
{
"indexingMode": "consistent",
"automatic": true,
"includedPaths": [
{
"path": "/*",
"indexes": [
{
"kind": "Range",
"dataType": "Number",
"precision": -1
},
{
"kind": "Hash",
"dataType": "String",
"precision": 3
}
]
},
{
"path": "/EchaNumber/?",
"indexes": [
{
"kind": "Range",
"dataType": "Number",
"precision": -1
},
{
"kind": "Hash",
"dataType": "String",
"precision": 3
}
]
},
{
"path": "/ChemicalName/?",
"indexes": [
{
"kind": "Range",
"dataType": "String",
"precision": -1
},
{
"kind": "Range",
"dataType": "Number",
"precision": -1
}
]
},
{
"path": "/CasNos/[]/?",
"indexes": [
{
"kind": "Range",
"dataType": "String",
"precision": -1
},
{
"kind": "Range",
"dataType": "Number",
"precision": -1
}
]
},
{
"path": "/EcNo/?",
"indexes": [
{
"kind": "Range",
"dataType": "String",
"precision": -1
},
{
"kind": "Range",
"dataType": "Number",
"precision": -1
}
]
},
{
"path": "/IndexNos/[]/?",
"indexes": [
{
"kind": "Range",
"dataType": "String",
"precision": -1
},
{
"kind": "Range",
"dataType": "Number",
"precision": -1
}
]
},
{
"path": "/ReachNos/[]/?",
"indexes": [
{
"kind": "Range",
"dataType": "String",
"precision": -1
},
{
"kind": "Range",
"dataType": "Number",
"precision": -1
}
]
}
],
"excludedPaths": []
}
I implemented the code in Dotnet Core 2.0 and are using the SQL API.
When I search the collection I use the following SQL to find the ids of the docs I need:
SELECT VALUE c.id FROM IndexCollection c JOIN cas IN c.CasNos JOIN indx IN c.IndexNos WHERE CONTAINS(c.ChemicalName,"searchVal") OR STARTSWITH(cas,"searchVal") OR STARTSWITH(c.EcNo,"searchVal") OR STARTSWITH(indx,"searchVal")
I verified it by searching for a known partial match in CasNos string collection property.
I then needed to add search for ReachNos as well and added it to the search:
SELECT VALUE c.id FROM IndexCollection c JOIN cas IN c.CasNos JOIN indx IN c.IndexNos JOIN rch IN c.ReachNos WHERE CONTAINS(c.ChemicalName,"searchVal") OR STARTSWITH(cas,"searchVal") OR STARTSWITH(c.EcNo,"searchVal") OR STARTSWITH(indx,"searchVal") OR STARTSWITH(rch,"searchVal")
...But as soon as I add ReachNos to search it stops finding any result tho I use same searchval as above.
If I modify the SQL to only search the ReachNos property it will find results from that given I enter a known value from that property.
Can anyone pls. tell me why this is not working?
I tried all combinations of the first SQL and no matter what properties I searched for it worked fine until I added ReachNos property.
EDIT: I should add I have tested on local Cosmos DB Emulator and azure.
EDIT2: Added document JSON
/Thanx
In Windows Phone App.. I am parsing JSON Data By (get,set) method
I got another way of processing JSON data from this URL
In the part of this site (http://www.dfg-team.com/json-feeds-on-windows-phone/?lang=en)
Is used two techniques to parse json data..
in that I want to use this. Key word for processing JSON data ..
But My app have data Have a Lot of Json data which contains lot of JSON Arrays and JSON Objects. But its not a Problem in that I have main problem is My json contains Json array inside json object or json object inside the json array so at here we got problem.
This is My JSon data I want to parse these fields.. using this. method
{
"returnCode": "success",
"SData": {
"results": [
{
"wdetails": [
{
"noffers": [],
"offers_count": 0,
"name": "yahoo.com",
"recentoffers_count": 0,
"sku": "30072826",
"url": "http://www.yahoo.com"
},
{
"noffers": [
{
"id": "3aggEYoyaso",
"price": "179.99",
"firstrecorded_at": 13700,
"lastrecorded_at": 13700,
"seller": "google",
"availability": "In stock, ships same Business Day.",
"currency": "USD"
}
],
"offers_count": 1,
"name": "frys.com",
"recentoffers_count": 1,
"sku": "789",
"url": "http://www.google.com"
},
{
"listprice_currency": "USD",
"noffers": [
{
"id": "2SlCKQKm",
"price": "192.37",
"firstrecorded_at": 111,
"lastrecorded_at": 111,
"seller": "Amazon",
"availability": "13 In Stock",
"currency": "USD"
}
],
"offers_count": 1,
"name": "amazon.com",
"listprice": "288.56",
"recentoffers_count": 1,
"sku": "1084089",
"url": "http://www.amazon.com"
}
],
"model": "GGGG",
"weight": "771107.03",
"price_currency": "USD",
"gtins": [
"00839294"
],
"mpn": "GGGG",
"cat_id": "25552",
"height": "98.81",
"features": {
"Product Type - General": "Tablet",
"Height (in.)": "1.70"
},
"length": "416.56",
"geo": [
"usa"
],
"width": "267.97",
"category": "Tablets",
"price": "179.99",
"updated_at": 138,
"color": "Black",
"manufacturer": "google",
"images_total": 1,
"images": [
"https://evbdn.eventbrite.com/s3-s3/eventlogos/1832816/google.png"
],
"brand": "Dell",
"offers_total": 180
}
],
"total_results_count": 1,
"results_count": 1,
"code": "OK",
"offset": 0
}
}
I want to parse this Using this. in C#.
In Android I done this By hash map method, in Windows Phone this. is similar to is hash map so I am using this..
I am editing my previous answer :-
JObject json = JObject.Parse(yourJson);
if you need results to be parsed
this.ClassOfResult= json ["SData"]["results"];
if you want the 'wdetails' in the Json to get parsed you can try this
this.parentclass.wdetailsClass= json ["SData"]["results"]["wdetails"];
Hope that this will give you some idea.
This question already has answers here:
Parsing JSON using Json.net
(5 answers)
Closed 3 years ago.
I am using Newtonsoft Json library to parse json but i don't know how to use it. I parsed the string using JObject. When i output the value of JObject instance in immediate window i get this :-
json
{
"data": [
{
"id": "id",
"from": {
"name": "name",
"id": "someotherid"
},
"name": "pic",
"description": "desc",
"link": "linktosite",
"privacy": "everyone",
"count": 1,
"type": "normal",
"created_time": "2010-10-22T14:54:32+0000",
"updated_time": "2010-10-22T14:55:41+0000"
},
{
"id": "id2",
"from": {
"name": "name",
"id": "someotherid"
},
"name": "Profile Pictures",
"link": "link",
"privacy": "everyone",
"count": 6,
"type": "profile",
"created_time": "2010-10-12T14:27:58+0000",
"updated_time": "2011-01-01T18:38:14+0000"
},
{
"id": "id3",
"from": {
"name": "name",
"id": "829741958"
},
"name": "T",
"link": "link",
"privacy": "everyone",
"count": 5,
"type": "normal",
"created_time": "2010-05-01T03:03:39+0000",
"updated_time": "2010-05-01T03:19:13+0000",
"comments": {
"data": [
{
"id": "id",
"from": null,
"message": "message",
"created_time": "2010-08-28T18:27:10+0000",
"likes": 1
}
]
}
}
],
"paging": {
"previous": "paginglink",
"next": "otherpaginglink"
}
}
Count: 2
Type: Object
What should i do further to have the values from this jobject?
I personally prefer the JavaScriptSerializer for use with JSON in the .NET environment. By default it will return a Dictionary result, but can be used to parse in to a custom object (or you could make use of the dynamic datatype).
Some other posts on SO with JSON & JavaScript Serializer as topic