1

First, I am new to Python and working with JSON. I am trying to extract just one value from an API request response, and I am having a difficult time parsing out the data I need. I have done a lot of searching on how to do this, but most all the examples use a string or file that is formatted is much more basic than what I am getting. I understand the key - value pair concept but I am unsure how to reference the key-value I want. I think it has something to do with the response having multiple objects having the same kay names. Or maybe the first line "Bookmark" is making things goofy. The value I want is for the model name in the response example below. That's all I need from this. Any help would be greatly appreciated.

{
  "Bookmark": "<B><P><p>SerNum</p><p>Item</p></P><D><f>false</f><f>false</f></D><F><v>1101666</v><v>ADDMASTER IJ7102-23E</v></F><L><v>123456</v><v>Model Name</v></L></B>",
  "Items": [
    [
      {
        "Name": "SerNum",
        "Value": "123456"
      },
      {
        "Name": "Item",
        "Value": "Model Name"
      },
      {
        "Name": "_ItemId",
        "Value": "PBT=[unit] unt.DT=[2021-07-28 08:20:33.513] unt.ID=[eae2621d-3e9f-4515-9763-55e67f65fae6]"
      }
    ]
  ],
  "Message": "Success",
  "MessageCode": 0
}
Greg
  • 31
  • 5
  • You want the string `"Model Name"`? Because it's associated to `"Name": "Item"`? Also, is the `Items` element deliberately an array that itself contains a single array? – MatBailie Jan 04 '23 at 22:37
  • I would pass your `obj.Bookmark` into a string and pass it through [Beautiful Soup](https://www.crummy.com/software/BeautifulSoup/) to get the "Model Name" -- Assuming you're trying to extract "ADDMASTER IJ7102-23E" – Zak Jan 04 '23 at 22:41
  • 1
    There are at least two "Model Name" strings in that sample data. One in the Bookmark string, and one in the Item list. Which one are you talking about? – John Gordon Jan 04 '23 at 22:51

2 Answers2

1

If you want to find value of dictionary with key 'Name' and value 'Item' you can do:

import json

with open('your_data.json', 'r') as f_in:
    data = json.load(f_in)

model_name = next((i['Value'] for lst in data['Items'] for i in lst if i['Name'] == 'Item'), 'Model name not found.')
print(model_name)

Prints:

Model Name

Note: if the dictionary is not found string 'Model name not found.' is returned

Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
0

First, load the JSON into a python dict:

import json

x = '''{
  "Bookmark": "<B><P><p>SerNum</p><p>Item</p></P><D><f>false</f><f>false</f></D><F><v>1101666</v><v>ADDMASTER IJ7102-23E</v></F><L><v>123456</v><v>Model Name</v></L></B>",
  "Items": [
    [
      {
        "Name": "SerNum",
        "Value": "123456"
      },
      {
        "Name": "Item",
        "Value": "Model Name"
      },
      {
        "Name": "_ItemId",
        "Value": "PBT=[unit] unt.DT=[2021-07-28 08:20:33.513] unt.ID=[eae2621d-3e9f-4515-9763-55e67f65fae6]"
      }
    ]
  ],
  "Message": "Success",
  "MessageCode": 0
}'''

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

# The result is a Python dictionary. 

Now if you want the value 'Model Name', you would do:

print(y['Items'][0][1]['Value'])
radhadman
  • 115
  • 8
  • Hard coding [1] is not definitively the answer. How do you know the op always wants the second element? You may as well just do `return `Model Name'` – MatBailie Jan 04 '23 at 22:52
  • Then you either ask questions to confirm, or you state your assumptions in your answer. But you certainly don't answer with hidden assumptions. – MatBailie Jan 04 '23 at 23:03
  • 1
    @radhadman Thank you for your response! It worked perfectly. I did do some more research and leaned I was dealing with nested arrays within the object. I also found a neat little function that helped also but it adds a module that really wouldn't be needed as long as the accepted answer is used. https://hackersandslackers[dot]com/extract-data-from-complex-json-python/ I will also try to make my questions clearer in the future! Thanks again for your help! – Greg Jan 04 '23 at 23:14
  • great. As MatBailie said, a structural change in the JSON file could cause my solution to break, in which case you will want to get the value in a way that prevents future breaks. I figured that if I gave you a start on how to retrieve the value you could figure out the rest on your own. Glad I could help. Cheers :) – radhadman Jan 04 '23 at 23:25