1
r = '{"available_sizes":[{"id":104682,"name":"40","preorder_only":false},{"id":104683,"name":"41","preorder_only":false},{"id":104684,"name":"42","preorder_only":false},{"id":104685,"name":"43","preorder_only":false},{"id":104687,"name":"45","preorder_only":false}]}'

data = json.loads(r)

for element in data: 
    for value in data['available_sizes']:
        print(value['name'])

At the moment this prints out the following:

40
41
42
43
45

How would I then use this data as a string? Below is the desired output.

Available sizes are 40, 41, 41, 43, 45
3
  • ', '.join([str(v['name']) for v in data['available_sizes']]) Commented Jun 9, 2018 at 21:14
  • Works a dream thank you - would you like to post as an asnwer so I can award it to you? @coldspeed Commented Jun 9, 2018 at 21:16
  • Sure, a second. Commented Jun 9, 2018 at 21:17

3 Answers 3

1

Your outermost loop is superfluous, since you only have a single key to iterate over.

Iterate over data, append your numbers to a list, and then call str.join at the end to efficiently join your strings together.

nums = []
for v in data['available_sizes']:
    nums.append(str(v['name']))  # v['name'] if it is already string

print(f'Available sizes are {', '.join(nums)}')

You can rewrite the loop using a list comprehension -

num_str = ', '.join([v['name'] for v in data['available_sizes']])
print(f'Available sizes are {num_str}')  

For a primer on JSON data traversals, I recommend looking at this answer.

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

Comments

0

Use a for-comprehension to extract the size names, and then str.join() to add the comma separator:

import json

r = '{"available_sizes":[{"id":104682,"name":"40","preorder_only":false},{"id":104683,"name":"41","preorder_only":false},{"id":104684,"name":"42","preorder_only":false},{"id":104685,"name":"43","preorder_only":false},{"id":104687,"name":"45","preorder_only":false}]}'
data = json.loads(r)

# Extract the size names from the list of available sizes
size_names = [size_entry["name"] for size_entry in data["available_sizes"]]

# Join the sizes names as strings using a comma separator and print
sizes_string = ", ".join(size_names)
print("Available sizes are: " + sizes_string)

Comments

0

Do something like that this is the out put that you want

import json

 r = '{"available_sizes":[{"id":104682,"name":"40","preorder_only":false},{"id":104683,"name":"41","preorder_only":false},{"id":104684,"name":"42","preorder_only":false},{"id":104685,"name":"43","preorder_only":false},{"id":104687,"name":"45","preorder_only":false}]}'

data = json.loads(r)

var = []

for element in data: 
    for value in data['available_sizes']:
        var.append(value['name'])
print( 'Availble size are %s' %(', '.join(var)))

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.