0

I have a JSON file. I want to parse it and print the response in Dialogflow.

import json

some JSON:

    x = '{
          "first": {"Id": "1","Zone": "South", "Product": "toy"}, 
          "second": {"Id": "2","Zone": "North", "Product": "softtoy"}, 
          "third": {"Id": "1", "Zone": "East","Product": "bat"}
         }'

parse x:

y = json.loads(x)
smilyface
  • 5,021
  • 8
  • 41
  • 57
  • Just to let you know one thing - the `i` (index of the for-loop) is not an integer. It is the key of the json. In your case it is "1" not the index 1. That's why I used "first" and "second" words in my answer. – smilyface Jun 04 '19 at 09:39
  • I am new to python & json, so it is difficult to understand at first. If there is any link which explains loop in json file well – Shreya Sinha Jun 04 '19 at 11:36
  • Trust me. I am doing Python for the first time. I am a java / j2ee developer. You can check my profile :) – smilyface Jun 04 '19 at 12:53

2 Answers2

0

I think it depends on how you parsed it. Your error message shows that the variable 'i' is a string.

you can use the 'json' library, and do something like this to parse and use your json file:

import json
with open(file_name) as json_file:
    file_data = json.load(json_file)
print(file_data['1']['Zone'])

Answer to your comment:

If you want to iterate over the json file with for loop, you can simply read it into an dictionary and iterate over the dictionary:

with open(file_name) as json_file:
    file_data = json.load(json_file)

for sub_dict_value in file_data.values():
    for key, value in sub_dict_value.items():
        print(key, value)
DijkWxyZ
  • 76
  • 5
0

Try this

    import json

    # some JSON:
    x = '{"first": {"Id": "1","Zone": "South", "Product": "toy"}, "second": {"Id": "2","Zone": "North", "Product": "softtoy"}, "third": {"Id": "1", "Zone": "East","Product": "bat"}}'

    # parse x:
    y = json.loads(x)

    # Get the key `Zone` under `1`
    z = y['1']['Zone']
    print(z)


    if z == 'South':
        print('Yes')

Output:
South
Yes


Update : 1
Your json is an object. It is not array. Check this https://stackoverflow.com/a/38561016/2086966

So, you does not have to iterate with a for-loop.
Just use as follows

if y['1']['Zone'] == 'North':
    print('No')
if y['1']['Id'] == 1:
    print('Yes')




Update : 2

I guess this is the one you are searching for

for i in y:
 print(i + " Id -> " +  y[i]['Id'])
 print(i + " Zone -> " +  y[i]['Zone'])

Output
second Id -> 2
second Zone -> North
first Id -> 1
first Zone -> South

Here is the working code : https://onlinegdb.com/rJ4Ui3m0E


Update : 3

for i in y: 
    if y[i]['Id'] == "1": 
        if y[i]['Zone'] == "East": 
            print (y[i]['Product'])

Output
bat

Here is the working code : https://onlinegdb.com/rJ41KyER4

smilyface
  • 5,021
  • 8
  • 41
  • 57
  • 1
    I am facing issue in using loop which will read each element and return response – Shreya Sinha Jun 04 '19 at 08:09
  • This code is working. But this is not what I am looking for. I have a data set which contains lots of similar json values. I want to search if Zone = North, then it will check Product and if product does not matches then it search for other North Zone and co on. Hope you understood my problem. – Shreya Sinha Jun 04 '19 at 11:20
  • Shreya, That's the logic you need to implement. As of my understanding it's just the if condition handling. Please try yourself (with my code) in the given url `https://onlinegdb.com/rJ4Ui3m0E` (click on `fork it`) and share it to me. Let's play with it. Let me know if you face any issue. – smilyface Jun 04 '19 at 11:23
  • import json # some JSON: x = '{"first": {"Id": "1","Zone": "South", "Product": "toy"}, "second": {"Id": "2","Zone": "North", "Product": "softtoy"}, "third": {"Id": "1", "Zone": "East","Product": "bat"}}' # parse x: y = json.loads(x) for i in y: if y.Id == "1": if y.Zone == "East": print (y.Product) – Shreya Sinha Jun 04 '19 at 11:53
  • Pls give me a new URL (you can fork my url) - read my above comment again. Click on `Fork this`. Edit your changes. Then click on `Share`. You will see a new URL in the share box. – smilyface Jun 04 '19 at 12:44
  • You should read well before you comment. You are again making the same mistake. It's not `y.key` it is `y[i]['key']`. Please make sure not to make simple mistakes (without reading the answers). https://onlinegdb.com/rJ41KyER4 – smilyface Jun 04 '19 at 12:51
  • Trust me. I am doing Python for the first time. I am a java / j2ee developer. You can check my profile :) – smilyface Jun 04 '19 at 12:53
  • Updated the answer with `Update 3`. Please go through. And select it as right answer (tick mark will become green color) so that it would help others to identify. – smilyface Jun 04 '19 at 14:59