How to parse dynamic json? - flutter

I'm new to flutter development, I can't figure out how to parse json correctly, since lists are changeable.
Maybe there are some libraries that allow you to turn json into an object right away?
{
"name": "Nuan",
"key": "wqewewrwerer",
"places": {
"1": {
"place": 1,
"numberPlaces": 4,
"type": "TABLE"
},
"2": {
"place": 2,
"numberPlaces": 4,
"type": "TABLE"
},
"3": {
"place": 3,
"numberPlaces": 4,
"type": "TABLE"
}
},
"menu": {
"categories": [{
"name": "Salads",
"elements": [{
"id": 1,
"name": "Caesar salad",
"img": "cesar.jpg",
"price": 4,
"currency": "USD",
"weight": "222",
"typeWeight": "g",
"time": 3,
"calories": 500,
"description": "The salad\u0027s creation is generally attributed to the restaurateur Caesar Cardini, an Italian immigrant who operated restaurants in Mexico and the United States. His daughter Rosa recounted that her father invented the salad at his Prohibition-era restaurant in Tijuana, Mexico when a Fourth of July rush in 1924 depleted the kitchen\u0027s supplies. (Cardini lived in San Diego but ran the family restaurant in Tijuana to attract American customers seeking to circumvent the restrictions of the Prohibition). Cardini made do with what he had, adding the dramatic flair of the table-side tossing \"by the chef.\" A number of Cardini\u0027s staff have said that they invented the dish.",
"structure": "croutons, romaine, anchovies, parmeasan cheese, olive oil, vinegar and plenty of black pepper."
}, {
"id": 2,
"name": "Greek salad",
"img": "grek.jpg",
"price": 5,
"currency": "USD",
"weight": "220",
"typeWeight": "g",
"time": 3,
"calories": 300,
"description": "Various other salads have also been called \"Greek\" in the English language in the last century, including some with no apparent connection to Greek cuisine. A 1925 Australian newspaper described a Greek salad of boiled squash dressed with sour milk; a 1934 American newspaper described a mayonnaise-dressed lettuce salad with shredded cabbage and carrots",
"structure": "Lettuce, tomatoes, feta, olives, cucumber"
}, {
"id": 3,
"name": "Salmon salad",
"img": "semga.jpg",
"price": 8,
"currency": "USD",
"weight": "250",
"typeWeight": "g",
"time": 3,
"calories": 400
}, {
"id": 4,
"name": "Hunting salad",
"img": "hunting.jpg",
"price": 7,
"currency": "USD",
"weight": "220",
"typeWeight": "g",
"time": 4,
"calories": 390,
"description": "Hunting salad has such a name, since it includes meat of animals caught by hunters. History is silent at what point the game meat was replaced with beef, hunting sausages or even pork, but in our reality it is these meat products that are used for such a salad. The salad turns out to be very satisfying, nutritious and quite dense � ideal for men\u0027s snacks.",
"structure": "sausages, potatoes, greens"
}]
}, {
"name": "soups",
"elements": [{
"id": 5,
"name": "Solyanka soup",
"img": "solyanka.jpg",
"price": 10,
"currency": "USD",
"weight": "300",
"typeWeight": "g",
"time": 15,
"calories": 300,
"description": "There is no consensus on the correctness of the name selyanka in relation to soup. Russian linguist and writer L. I. Skvortsov writes about the traditionality of the name selyanka and the ambiguity of the etymology of the word \"solyanka\", at the same time, Russian historian, researcher and popularizer of cooking V. V. Pokhlebkin writes about the incorrectness and distortion of the name selyanka and claims that the name solyanka is fixed in the \"House-building\" of 1547, while the term selyanka took root only in the XIX century and at the beginning of the XX century was again replaced by the term solyanka. The Dictionary of the Russian Academy (1794) indicated the name solyanka as the main variant, and marked the selyanka variant as \"simple\"",
"structure": "sausage, potato, onion, tomato, pickle"
}, {
"id": 6,
"name": "Borsch soup",
"img": "borsch.jpg",
"price": 8,
"currency": "USD",
"weight": "300",
"typeWeight": "g",
"time": 15,
"calories": 300,
"description": "In the old days, borscht was called soup made from borscht. Later borscht was cooked on beet kvass: it was diluted with water, the mixture was poured into a clay pot or cast iron and brought to a boil. Chopped beets, cabbage, carrots and other vegetables were put in boiling water and put the pot in the oven. The cooked borscht was salted and refilled",
"structure": "meat, carrots, onions, potatoes, beets, white cabbage, beans, tomato paste, vegetable oil, bay leaf, sugar, vinegar"
}, {
"id": 7,
"name": "Mushroom soup",
"img": "mushroom.jpg",
"price": 10,
"currency": "USD",
"weight": "230",
"typeWeight": "g",
"time": 16,
"calories": 320,
"description": "Cream soup with mushrooms has been prepared since about the 17th century, when entrepreneurs already learned how to grow champignons in artificial conditions. It was in France, so this country is considered the birthplace of mushroom soups-puree. But the soup with dried porcini mushrooms is cooked on water and already only one aroma, spreading around the house, can drive even the most refined gourmets crazy.",
"structure": "potatoes, onions, herbs, salt, pepper, vegetable oil, mushrooms, soft processed cheese"
}]
}, {
"elements": [{
"id": 8,
"name": "Crackers",
"img": "crackers.png",
"price": 4,
"currency": "USD",
"weight": "200",
"typeWeight": "g",
"time": 12,
"calories": 600,
"description": "Crackers are second-baked bread, \"dried for the purpose of either storage or further culinary use in various dishes,\" is the definition of the product given by William Pohlebkin (1923-2000), a Russian historian and writer. The main distinguishing feature of crackers from all other bakery products is their reduced humidity, ideally no more than forty-nine percent, more often - up to eight.",
"structure": "bread, garlic, salt, oil"
}, {
"id": 9,
"name": "Croutons",
"img": "croutons.png",
"price": 4,
"currency": "USD",
"weight": "200",
"typeWeight": "g",
"time": 12,
"calories": 580,
"description": "Croutons are made from any bread and are used as a light snack, for example, croutons with garlic for beer, or as an ingredient in soups, broths, salads (\"Caesar\"), cutlets and other dishes. To add to soups (French onion soup), bread is simply fried with salt and/or black pepper.",
"structure": "white bread, garlic, salt, pepper, oil"
}, {
"id": 11,
"name": "Chips",
"img": "chips.png",
"price": 3,
"currency": "USD",
"weight": "100",
"typeWeight": "g",
"time": 3,
"calories": 330,
"structure": "potatoes, salt, pepper, oil"
}]
}]
},
"description": "There are many things in the world, friend Horatio, that our wise men never dreamed of"
}
I've seen standard ways where data is passed to the constructor, but it's not entirely clear how to embed large json there

You can use the json2Dart utility to generate your dart objects and call the fromJson() method on your root object :)

Related

MongoDB - MongoImport of JSON (jsonl) - Rename, change types and add fields

i'm new to the topic MongoDB and have 4 different problems importing a big (16GB) file (jsonl) into my MongoDB (simple PSA-Cluster).
Below attached you will find a sample entry from the mentiond JSON-Dump.
With this file which i get from an external provider I actually have 4 problems.
"hotel_id" is the key and should normally be (re-)named as "_id"
"hotel_id" should not be treated as string rather than as Number
"location" is not properly formatted (if i understood correctly the MongoDB Manual) as GeoJSON as it should be like
"location": {
"type": "Point",
"coordinates": [-93.26838,37.15845]
}
instead of
"location": {
"coordinates": {
"latitude": 37.15845,
"longitude": -93.26838
}
}
"dates" can this be used to efficiently update just the records which needs to be updated?
So my challenge is now to transform the data according to my needs before importing the data or at time of import, but in both cases of course as quickly as possible.
Therefore i searched a lot for hints and best practices, but i was not able to find a solution yet, maybe due to the fact that i'm a beginner with MongoDB.
I played around with "jq" to adjust the data and for example add the type which seems to be necessary for the location (point 3), but wasn't really successful.
cat dump.jsonl | ./bin/jq --arg typeOfField Point '.location + {type: $typeOfField}'
Beside that i was injecting a sample dump of round-about 500MB which took 1,5 mins when importing it the first time (empty database). If i run it in "upsert" mode it will take round-about 12 hours. So i was also wondering what is the best practice to import such a big JSON-dump?
Any help is appreciated!! :-)
Kind regards,
Lumpy
{
"hotel_id": "12345",
"name": "Test Hotel",
"address": {
"line_1": "123 Test St",
"line_2": "Apt A",
"city": "Test City",
},
"ratings": {
"property": {
"rating": "3.5",
"type": "Star"
},
"guest": {
"count": 48382,
"average": "3.1"
}
},
"location": {
"coordinates": {
"latitude": 22.54845,
"longitude": -90.11838
}
},
"phone": "555-0153",
"fax": "555-7249",
"category": {
"id": 1,
"name": "Hotel"
},
"rank": 42,
"dates": {
"added": "1998-07-19T05:00:00.000Z",
"updated": "2018-03-22T07:23:14.000Z"
},
"statistics": {
"11": {
"id": 11,
"name": "Total number of rooms - 220",
"value": "220"
},
"12": {
"id": 12,
"name": "Number of floors - 7",
"value": "7"
}
},
"chain": {
"id": -2,
"name": "Test Hotels"
},
"brand": {
"id": 2,
"name": "Test Brand"
}
}

Mongodb multilingual query

Material collection represents user submissions.
Default language is defined by the user. Other users can extend first submission with some other languages.
When I request a material, maybe I need more than one language, so I think it fits ok with just one collection where I should support multilanguage.
My current desing is like this (document for testing):
{
"activities": [ "Ficha de actividades", "Juego Colectivo" ],
"files": [ "Asociacion_Profesiones.zip"],
"areas": ["Literatura", "Ciencias Naturales", "Ciencias Sociales" ],
"authors": [
{ "author": "César", "email": "xxx#gmail.com"},
{ "author": "José", "email": "xxxx#gmail.com"}
],
"desc": "La leche, el agua, el vino y la bebida",
"state": 1,
"date": ISODate("2017-01-10 19:40:39"),
"updated": null,
"id": 1,
"language": "spanish",
"images": [],
"license": "Creative Commons BY-NC-SA",
"downloads": 0,
"popular": false,
"title": "La bebida",
"file": "1.zip",
"translations": [
{"language": "english", "title": "Beverages", "desc": "Milk, water, wine and all related with drinking", "authors": [], "files": [], "file": "1.zip", "images": [], "downloads": 0, "state": 1, "created": null, "updated": null},
{"language": "french", "title": "le boisson", "desc": "du lait, de l'eau, du vin, boire", "authors": [], "files": [], "file": "1.zip", "images": [], "downloads": 0, "state": 1, "created": null, "updated": null}
]
}
I create a text index focused on title and descripcion (desc) fields.
db.materials.createIndex({ "title": "text", "desc": "text", "translations.title": "text", "translations.desc": "text"})
I know the user default language, but I don't know how he should make the search.
Imagine an spanish user want to search for drinks:
db.materials.find( { $text: { $search: "drink", $language: "es"} } ).explain(true)
It returns our document. It searches drink, and as "Milk, water, wine and all related with drinking" has drink as stemming for drinking it works ok.
However... if I look for water:
db.materials.find( { $text: { $search: "water", $language: "es"} } ).explain(true)
It returns nothing. water gets converted to wat (as spanish verbs end with er suffix) and "wat" is not found.
Any improvement to my searches?
The only workaround I can imagine is to add language dropdown selector to user searches.

Trying to figure out how tax lines work when creating Shopify order via API

I am posting the following to the Shopify API create order endpoint and receiving a response without tax lines that are filled. I haven't seen anything on the internet on this, other than that Shopify doesn't offer another endpoint for just taxes. So, I am trying to figure out if tax lines are supposed to be generated automatically in the order response when creating an order via the API or if I have to include the tax lines in the request in order for tax lines to be included, because I know when I create an order in the admin the tax lines are automatically calculated and included for me?:
{
"order": {
"email": "some#email.com",
"financial_status": "paid",
"fulfillment_status": null,
"send_receipt": true,
"send_fulfillment_receipt": true,
"note": "Created by somename",
"line_items": [
{
"variant_id": 21718275463,
"quantity": 1,
"price": 99,
"requires_shipping": true,
"product_id": 6820646151
},
{
"variant_id": 21717700871,
"quantity": 1,
"price": 1000,
"requires_shipping": true,
"product_id": 6820646151
},
{
"variant_id": 21717690055,
"quantity": 1,
"price": 555,
"requires_shipping": true,
"product_id": 6821668807
}
],
"processing_method": "offsite",
"shipping_address": {
"first_name": "Chris",
"address1": "10101 Musick Road",
"phone": "9999999999",
"city": "St. Louis",
"zip": "63123",
"province": "MO",
"country": "United States",
"last_name": "Becker",
"name": "Chris Becker",
"country_code": "US",
"province_code": "MO"
},
"source_name": "somename",
"taxes_included": false,
"shipping_lines": [
{
"title": "standard",
"price": 0.00,
"code": null,
"source": "brand owner on shopify",
"carrier_identifier": null,
"tax_lines": null
}
],
"tags": "some Order"
}
}
Here is a useful documentation about tax: https://help.shopify.com/api/reference/location
Tax are not automatically calculated from cart.js ajax api. They just give a clue whether tax is already included in the price or not. If not, then it can be added with the bill.

Many-to-Many data modelling between fields for each record in mongoDB

Lets say I have to save records of cloths in mongoDB. Attribute of the cloth is
name
description
style
size
color
condition
brand
brandName
someAttrubute
price
For every cloth price changes for each combination of style and brand. So How do I model this in mongoDB.
So far what I have been thinking is:
{
"name": "A name",
"description": "A typical description",
"style":[
{"size": "XL","color": "red", "condition": "good"},//--style 0
{"size": "XXL","color": "white", "condition": "bad"},//--style 1
//...
{"size": "L","color": "black", "condition": "best"}//--style N
],
"brand":[
{"brandName":"brand0","someAttribute":"Attribute 0"},
{"brandName":"brand1","someAttribute":"Attribute 1"},
{"brandName":"brand2","someAttribute":"Attribute 2"}
],
"price":[
//Every price need to be added for every combination of brand and style
{"style":0,"brand":0,"price": 10},
{"style":0,"brand":1,"price": 20},
{"style":0,"brand":2,"price": 30},
{"style":1,"brand":0,"price": 10},
{"style":1,"brand":1,"price": 20},
//...
{"style":"N","brand":2,"price": 10}
]
}
I don't think this is the right way to do it in mongoDB. How to model this?
I would go like this,
{
"name": "A name",
"description": "A typical description",
"priceGroup" : [
{
"style": {"size": "XL","color": "red", "condition": "good"},
"brand": {"brandName":"brand0","someAttribute":"Attribute 0"}
"price": 10
},
{
"style": {"size": "XXL","color": "white", "condition": "bad"},
"brand": {"brandName":"brand0","someAttribute":"Attribute 0"}
"price": 20
},
{
"style": {"size": "XL","color": "red", "condition": "good"},
"brand": {"brandName":"brand1","someAttribute":"Attribute 1"}
"price": 30
},
{
"style": {"size": "XXL","color": "white", "condition": "bad"},
"brand": {"brandName":"brand1","someAttribute":"Attribute 1"}
"price": 40
},
.....
]
}
But as #Neil Lunn pointed out, while designing nosql schemas, there are no rules as in relational database design concept - no normalization. Hence it is more up to your application and requirements. Put the things that you will be querying all together in a collection, and the others in a different collection.

Why is image_hash empty for some ad_creatives? Is it because it is expired?

I ran _">https://graph.facebook.com/act_47121778/adcreatives?access_token=_
and got the following results. Why are some creatives returning empty image hash?
{
"view_tag": "",
"alt_view_tags": [
],
"creative_id": "6002716206572",
"type": 1,
"title": "Grow your fans!",
"body": "Fan Page Owners! Grow your fanbase. Create quizzes for your page. Win $100 Weekly. Make money with our revenue share program.",
"image_hash": "6ac30e43f21c8580361de92d3288ed68",
"link_url": "http://www.facebook.com/quizwriter?sk=app_81434956828",
"name": "Grow your fans!",
"run_status": 1,
"preview_url": "http://www.facebook.com/ads/api/creative_preview.php?cid=6002716206572",
"count_current_adgroups": 1,
"id": "6002716206572",
"image_url": "http://creative.ak.fbcdn.net/v41818/flyers/109/44/1296329249810543669_1_31eb15ed.jpg"
},
{
"view_tag": "",
"alt_view_tags": [
],
"creative_id": "6002444043572",
"type": 1,
"title": "Tennis Champs Social Game",
"body": "Tennis Champ! Start with 5,000 dollars , beat out other players with your mad tennis skills and become the tennis champ! Socialize!",
"image_hash": "",
"link_url": "334886511760",
"name": "Tennis Champs Social Game",
"run_status": 1,
"preview_url": "http://www.facebook.com/ads/api/creative_preview.php?cid=6002444043572",
"count_current_adgroups": 2,
"id": "6002444043572"
},