JSON- categorical data - json

I am writing a JSON file which holds data about electronics such as tvs, consoles etc. What is the best way to categorize, let's say 3D LED tvs, from non 3D LED tvs ? Would it make more sense to put a property '3d' within an 'leds' object ? Or is it better to create a '3d' object within 'leds' ?
{"products":[
{ "tvs":[
{"samsung":[
{"leds":[
{
"product_id": "034567",
"product_name": "Samsung UA22F5100 22'' LED TV (Black)",
"model_no": "UA22F5100",
"brand": "Samsung",
"price": 399,
"screen_size": 22,
"screen_res": "1920 x 1080",
"usb": 1,
"hdmi": 1,
"screen_format": "LED",
"dimensions": "513.1 x 366.5 x 169.6",
"manuf_guarantee": "1 year"
}
]}
]}
]}
]}

Related

Prevent JSON Object Being Sorted by Number Automatically

I use ajax to get a JSON object from an external API.
Here is the JSON object that I get :
"pricing_options": {
"7": {
"lodging_id": 7,
"lodging_url": "https://exampleapiurl.com/7",
"price": 520.0,
"name": "Private Room w/EnSuite Bath",
"max_occupancy": 2,
"image_url": "https://exampleapiurl.com/assets/282/2017/09/Karuna.jpg"
},
"552": {
"lodging_id": 552,
"lodging_url": "https://exampleapiurl.com/552",
"price": 400.0,
"name": "Private Room w/Queen Bed & Hall Bath",
"max_occupancy": 2,
"image_url": "https://exampleapiurl.com/assets/282/2019/03/Khandro-300x240.jpg"
},
"80": {
"lodging_id": 80,
"lodging_url": "https://exampleapiurl.com/80",
"price": 400.0,
"name": "Private Room w/Twin Bed & Hall Bath",
"max_occupancy": 1,
"image_url": "https://exampleapiurl.com/assets/282/2017/09/Basement-4-e1624825910642.jpg"
}
}
However, it keeps re-ordered automatically by its ID (7, 80, 552). I want to keep it that way (in the same exact order).
The JSON protocol does not define an ordering for dictionary entries. As such, in all implementations it will be impossible to set or preserve a specific order.
If you want to set an order, modify the JSON schema so that the structure containing these entries is a list instead. For example:
{
"pricing_options":[
{
"lodging_id":7,
"lodging_url":"https://exampleapiurl.com/7",
"price":520.0,
"name":"Private Room w/EnSuite Bath",
"max_occupancy":2,
"image_url":"https://exampleapiurl.com/assets/282/2017/09/Karuna.jpg"
},
{
"lodging_id":80,
"lodging_url":"https://exampleapiurl.com/80",
"price":400.0,
"name":"Private Room w/Twin Bed & Hall Bath",
"max_occupancy":1,
"image_url":"https://exampleapiurl.com/assets/282/2017/09/Basement-4-e1624825910642.jpg"
},
{
"lodging_id":552,
"lodging_url":"https://exampleapiurl.com/552",
"price":400.0,
"name":"Private Room w/Queen Bed & Hall Bath",
"max_occupancy":2,
"image_url":"https://exampleapiurl.com/assets/282/2019/03/Khandro-300x240.jpg"
},
]
}
Alternatively, parse the JSON and build an internal data structure that maintains the entries in a specific order.

json.load loads a string instead of json

I have a list of dictionaries written to a data.txt file. I was expecting to be able to read the list of dictionaries in a normal way when I load, but instead, I seem to load up a string.
For example - when I print(data[0]), I was expecting the first dictionary in the list, but instead, I got "[" instead.
Below attached is my codes and txt file:
read_json.py
import json
with open('./data.txt', 'r') as json_file:
data = json.load(json_file)
print(data[0])
data.txt
"[
{
"name": "Disney's Mulan (Mandarin) PG13 *",
"cast": [
"Jet Li",
"Donnie Yen",
"Yifei Liu"
],
"genre": [
"Action",
"Adventure",
"Drama"
],
"language": "Mandarin with no subtitles",
"rating": "PG13 - Some Violence",
"runtime": "115",
"open_date": "18 Sep 2020",
"description": "\u201cMulan\u201d is the epic adventure of a fearless young woman who masquerades as a man in order to fight Northern Invaders attacking China. The eldest daughter of an honored warrior, Hua Mulan is spirited, determined and quick on her feet. When the Emperor issues a decree that one man per family must serve in the Imperial Army, she steps in to take the place of her ailing father as Hua Jun, becoming one of China\u2019s greatest warriors ever."
},
{
"name": "The New Mutants M18",
"cast": [
"Maisie Williams",
"Henry Zaga",
"Anya Taylor-Joy",
"Charlie Heaton",
"Alice Braga",
"Blu Hunt"
],
"genre": [
"Action",
"Sci-Fi"
],
"language": "English",
"rating": "M18 - Some Mature Content",
"runtime": "94",
"open_date": "27 Aug 2020",
"description": "Five young mutants, just discovering their abilities while held in a secret facility against their will, fight to escape their past sins and save themselves."
}
]"
The above list is formatted properly for easy reading but the actual file is a single line and the different lines are denoted with "\n". Thanks for any help.
remove double quote in data.txt is useful for me。
eg. modify
"[{...},{...}]"
to
[{...},{...}]
Hope it helps!

How can I retrieve the perimeter and specific geometric properties using the Model Derivative API?

I have followed the Postman tutorial for the model derivative API, specifically for extracting metadata. I used a .dxf file, since I want to know if it is possible to retrieve perimeter, length/width properties based off the file.
I received a 200 response and it gave me a massive list of objects w/ their respective objectid's. Basically I got back a ton of these:
{
"objectid": 253,
"name": "Line [108]",
"externalId": "108",
"properties": {
"3D Visualization ": {
"Material": "ByLayer"
},
"General": {
"Color": "ByLayer",
"Handle": "108",
"Layer": "color#000000ff",
"Linetype": "BYLAYER",
"Linetype scale": "1.000",
"Lineweight": "ByLayer",
"Name ": "Line",
"Plot style": "ByColor",
"Thickness": "0.000 mm",
"Transparency": "ByLayer"
},
"Geometry": {
"Angle": "192.931 deg",
"Length": "0.088 mm"
}
}
}
The .dxf file I tested was as simple as possible and it looks like this image:
How can I retrieve the perimeter of this image? Is it possible to retrieve other specific geometric properties that I specify?
How can I know what part of the .dxf file each objectid is referring to?
Although it looks simple, the polyline (?) is probably being tessellated, resulting in a large number of small lines. Have you tried the original DWG file? Can you try that with viewer.autodesk.com?

For and If/else in python to check and add information to json

Hello I'm having trouble making a loop of if else in python, I need my if to check that there is a description "quantity" in my product and if there is to leave it as it is else to add in "quantity" : 0,
I want to make my for check that the "quantity" is present and if it's not to add it in.
But I have no idea how to make this for if else combo
data = json.load(json_data)
for product in data:
if product ["quantity"] in data
else 'w' product ["quantity":0]
It's going to show the result hopefully with this
with open('br2.json', 'w', encoding='utf8') as json_data:
json_data.write(json.dumps(data, ensure_ascii=False))
json_data.close()
I want it to go over a json like this
[{"id": 2162952, "name": "Kit Gamer acer - Notebook + Headset + Mouse",
"price": 25599.0, "category": "Eletrônicos"},
{"id": 3500957, "name": "Monitor 29 LG FHD Ultrawide com 1000:1 de
contraste", "quantity": 18, "price": 1559.4, "category":
"Eletrônicos"},
{"id": 1911864, "name": "Mouse Gamer Predator cestus 510 Fox Preto",
"price": 699.0, "category": "Acessórios"}]
And return it like this
[{"id": 2162952, "name": "Kit Gamer acer - Notebook + Headset +
Mouse","quantity": 0, "price": 25599.0, "category": "Eletrônicos"},
{"id": 3500957, "name": "Monitor 29 LG FHD Ultrawide com 1000:1 de
contraste", "quantity": 18, "price": 1559.4, "category": "Eletrônicos"},
{"id": 1911864, "name": "Mouse Gamer Predator cestus 510 Fox Preto",
"price": 699.0, "category": "Acessórios"}]
if product is a dictionary you can check if the key "quantity" is not in product and in that case add that key with value 0 with:
if "quantity" not in product:
product["quantity"] = 0

What does "JSON standard only allows one top level value"?

In my IntelliJ editor, I have a test.json file as below. The second json record displays an error message "Json standard only allows one top-level value". But this is a valid JSON file, right? How to get rid of this annoying message?
{"reviewerID": "A2XVJBSRI3SWDI", "asin": "0000031887", "reviewerName": "abigail", "helpful": [0, 0], "title": "Ballet Dress-Up Fairy Tutu", "url": "https://www.amazon.com/dp/0000031887", "brand": "Boutique Cutie", "reviewText": "Perfect red tutu for the price. I baught it as part of my daughters Halloween costume and it looked great on her.", "overall": 5.0, "summary": "Nice tutu", "unixReviewTime": 1383523200, "reviewTime": "11 4, 2013"}
{"reviewerID": "A2G0LNLN79Q6HR", "asin": "0000031887", "reviewerName": "aj_18 \"Aj_18\"", "helpful": [1, 1], "title": "Ballet Dress-Up Fairy Tutu", "url": "https://www.amazon.com/dp/0000031887", "brand": "Boutique Cutie", "reviewText": "This was a really cute tutu the only problem is that it was super short on my 5 yr old daughter. Other than that it was really adorable.", "overall": 4.0, "summary": "Really Cute but rather short.", "unixReviewTime": 1337990400, "reviewTime": "05 26, 2012"}
You have to combine them as [ {Obj 1}, {Obj 2} ] if you want to use them in the same json file. Otherwise the json file will be containing two dictionary objects and it can only have one top level object.