0

is there a way to store my JSON key data(id,name,price...) on each of my variable(id1-id3, id1-id3, price1-price3...)

here is my sample code

import json

from kivy.properties import StringProperty

f = open('sample.json')
data = json.load(f)

id1 = StringProperty(None)
id2 = StringProperty(None)
id3 = StringProperty(None)

name1 = StringProperty(None)
name2 = StringProperty(None)
name3 = StringProperty(None)

price1 = StringProperty(None)
price2 = StringProperty(None)
price3 = StringProperty(None)

quantity1  = StringProperty(None)
quantity2  = StringProperty(None)
quantity3  = StringProperty(None)

for i in data:
    print(i)

and here is the JSON file

[
  {
    "id": "p01",
    "name": "Name 1",
    "price": 1,
    "quantity": 1
  },
  {
    "id": "p02",
    "name": "Name 2",
    "price": 2,
    "quantity": 2
  },
  {
    "id": "p03",
    "name": "Name 3",
    "price": 3,
    "quantity": 3
  }
]

i'm planning to use bigger data. but for simplicity's sake, i just use 3 items as an example

2
  • 1
    Why do you want to use variables? You can simply convert JSON to Python dictionary with json.loads(). Commented Feb 3, 2021 at 4:24
  • @MuhammadYousufNoor, I will be using this variables in the future, so i need to extract the individual data like name, price, and quantity from the JSON file. I'm relatively new to JSON module so please bare with me. Commented Feb 3, 2021 at 4:33

2 Answers 2

1

You don't need to store each value of JSON in a new variable, you can use a python dictionary.

See JSON and Python Dictionary

import json

#opening json file
with open("data.json") as f:
    #converting json to python dict and stroing it in data variable
    data = json.loads(f.read())

#iterating dict items and printing values
for items in data:
    print(items['id'],items['name'],items['price'])

#you can also access dict values like this
#0 is the index number.
print(data[0]['id'])
print(data[0]['name'])
print(data[0]['price'])

#similarly
print(data[1]['id'])
print(data[2]['id'])
Sign up to request clarification or add additional context in comments.

1 Comment

Thank You This Works, Just a follow up question. What if I have another table, how should I access the dict values for table 2? I'll attach the modified JSON code
0

You can simply use json module with python dictionary as follow:

# Import the module
import json

# String with JSON format
data_JSON =  """
[
  {
    "id": "p01",
    "name": "Name 1",
    "price": 1,
    "quantity": 1
  },
  {
    "id": "p02",
    "name": "Name 2",
    "price": 2,
    "quantity": 2
  },
  {
    "id": "p03",
    "name": "Name 3",
    "price": 3,
    "quantity": 3
  }
]
"""

# Convert JSON string to dictionary
data_dict = json.loads(data_JSON)
print(data_dict)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.