0

[OrderedDict([("{'asin': '0000037214'", "{'asin': '0000031887'"), (" 'related': {'also_viewed': ['B00JO8II76'", " 'related': {'also_bought': ['0000031852'"), (" 'B00DGN4R1Q'", " '0000031895'"), (" 'B00E1YRI4C']}", " '0000031909'"), (" 'title': 'Purple Sequin Tiny Dancer Tutu Ballet Dance Fairy Princess Costume Accessory'", " 'B00D2K1M3O'"), (" 'price': 6.99", " 'B00D10CLVW'"), (" 'salesRank': {'Clothing': 1233557}", " 'B003AVKOP2'"), (" 'imUrl': 'http://ecx.images-amazon.com/images/I/31mCncNuAZL.jpg'", " 'B00D103F8U'"), (" 'brand': 'Big Dreams'", " 'B00613WDTQ'"), (" 'categories': [['Clothing", " 'B008F0SU0Y'"), (" Shoes & Jewelry'", " 'B008UBQZKU'"), (" 'Girls']", " 'B00D0GCI8S'"), (" ['Clothing", " 'B00E1YRI4C'"), (" 'Novelty", " 'B003AVEU6G'"), (" Costumes & More'", " 'B008F0SMUC'"), (" 'Costumes & Accessories'", " 'B002C3Y6WG'"),

So in a python file, I would want to parse over this json file^^^ line by line and when it gets the ID part I want it to add the value of it to a list. I just don't want to print out all the individual values I want to append all the ids values into a list. So by at the end, I should have a list containing 1 and 2

clothing_ids = [1,2]

Like so ^^, Below is what I have tried so far but can't seem to get to work.

clothing_ids = {'List of Clothing IDs': []}

json_file = open('jsonfile.json', 'r') 
file = json_file.read()
for line in file:
    if 'id' in line:
        clothing_ids['List of Clothing IDs'].append(this)
 print(data)
 json_file.close()

***IDs in this file are called 'asin'

Jake123
  • 171
  • 1
  • 10
  • Possible duplicate of [Parsing values from a JSON file?](https://stackoverflow.com/questions/2835559/parsing-values-from-a-json-file) – Elis Byberi Nov 21 '17 at 22:02
  • No, it's different, in that question is asking how to print ALL individual values, were I want just the id values and append them to there own list. hope that makes sense. – Jake123 Nov 21 '17 at 22:09
  • why do you want to loop over the data line by line instead of properly parsing it? – Bryan Oakley Nov 21 '17 at 22:10
  • @BryanOakley sorry, Im kinda new to this data analysis stuff Im not aware parsing it would be best :) – Jake123 Nov 21 '17 at 22:16
  • Also, the data you've shown is invalid json. Are you _certain_ it's json data, and not just data that looks like JSON but isn't? – Bryan Oakley Nov 21 '17 at 22:17
  • @BryanOakley That is not the actual data, that's just an example data I used to explain my question and ill edit my question now with a little snippet of my actual json file I'm using and let me know what you think because I just assumed if it has .json on the end its a json file?? – Jake123 Nov 21 '17 at 22:20
  • @BryanOakley Edited – Jake123 Nov 21 '17 at 22:27

2 Answers2

0
clothing_ids = []    
for key, value in file.items():
    if key == 'id':
        clothing_ids.append(value)

Go through every key value pair, and where the key is ID, add the value to your ID list.

JRodge01
  • 280
  • 1
  • 7
0

If your JSON file was syntactically correct and contained an array of those objects, you could just use the JSON parsing library:

import json
data = json.load(open('jsonfile.json'))
clothing_ids = {'List of Clothing IDs': map(lambda x: x['ID'], data)}

That worked on this JSON file:

[
  {
    "ID": "1",
    "FirstName": "John",
    "LastName": "Smith",
    "Age": 27
  },
  {
    "ID": "2",
    "FirstName": "Frank",
    "LastName": "Jones",
    "Age": 29
  }
]
Allen Luce
  • 7,859
  • 3
  • 40
  • 53