0

i need to extract key value pair data from this json array. I need to extract data like the 'producId' as well as availability status inside array with in a parent array.

data=[
 {
  "productId": 1001080012,
  "storeNumber": 289,
  "isSosVendorDirect": true,
  "price": {
   "selling": "249.00",
   "retail": "249.00",
   "typeCode": 1,
   "typeIndicator": "Regular Price"
  },
  "availability": 
  [
   {
    "availabilityStatus": "Available",
    "productStockType": "STK",
    "availabileQuantity": 822,
    "deliveryMethodId": 1,
    "deliveryMethodName": "Parcel Shipping",
    "storeNumber": 907
   },
   {
    "availabilityStatus": "Available",
    "productStockType": "STK",
    "availabileQuantity": 1,
    "leadTime": 1570607222335,
    "deliveryMethodId": 2,
    "deliveryMethodName": "Store Pickup",
    "storeNumber": 289
   },
   {
    "availabilityStatus": "Available",
    "productStockType": "STK",
    "availabileQuantity": 1,
    "leadTime": 1570607222335,
    "deliveryMethodId": 3,
    "deliveryMethodName": "Truck Delivery",
    "storeNumber": 289
   }
  ],
  "@type": "item"
 }
]

I have tired using.

price=data[selling]
store=data[storeNumber]
2
  • did you try the json module function json.loads? Commented Oct 9, 2019 at 8:34
  • You cannot use a json directly. First you need to load it. The json data need to a string, or a plain text file to be loaded. Check my answer. Commented Oct 9, 2019 at 9:30

2 Answers 2

2

You have a mix of nested lists and dictionaries. Looking at it you can figure out the structure.

Assuming your data to be:

txt = '''
[
 {
  "productId": 1001080012,
  "storeNumber": 289,
  "isSosVendorDirect": true,
  "price": {
   "selling": "249.00",
   "retail": "249.00",
   "typeCode": 1,
   "typeIndicator": "Regular Price"
  },
  "availability": 
  [
   {
    "availabilityStatus": "Available",
    "productStockType": "STK",
    "availabileQuantity": 822,
    "deliveryMethodId": 1,
    "deliveryMethodName": "Parcel Shipping",
    "storeNumber": 907
   },
   {
    "availabilityStatus": "Available",
    "productStockType": "STK",
    "availabileQuantity": 1,
    "leadTime": 1570607222335,
    "deliveryMethodId": 2,
    "deliveryMethodName": "Store Pickup",
    "storeNumber": 289
   },
   {
    "availabilityStatus": "Available",
    "productStockType": "STK",
    "availabileQuantity": 1,
    "leadTime": 1570607222335,
    "deliveryMethodId": 3,
    "deliveryMethodName": "Truck Delivery",
    "storeNumber": 289
   }
  ],
  "@type": "item"
 }
]
'''

Then:

import json
data = json.loads(txt)

price = data[0]['price']['selling']
print(price)

store = data[0]['availability'][0]['storeNumber']
print(store)

Output:

249.00
907
Sign up to request clarification or add additional context in comments.

Comments

1

Since your data is a length 1 array, you have to call the 0th index first.

price=data[0]['price']['selling']
store=data[0]['storeNumber']

print(price, store)

The result is as follows:

249.00 289

You can also have the storeNumbers in the availability key which has different values. In this case, you have to specify which value you want to pick but I don't know what you want.

3 Comments

its throwing out the error, "true is not defined "'isSosVendorDirect': true"
That is right. I was modified into True but you can load your data as a string and parse as json, e.g. @alec_djinn's example.
How can we achieve if it is a multi dimensional array?

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.