1

I am still new to Python and I am still trying a lot of new things in this aspect. I am working with an API that contains JSON data that looks as follow:

products: [
{
id: 66057248,
createdAt: "2018-02-28T14:55:22+01:00",
updatedAt: "2018-04-26T20:44:12+02:00",
isVisible: true,
visibility: "visible",
hasMatrix: false,
data01: "",
data02: "",
data03: "",
url: "product-name",
title: "product name",
fulltitle: "product name",
    etc...

What I am trying to do now is to let the user give input on the values in the JSON file that he wants to compare with each other. I tried a couple of things but so far without a result. I am probably doing something stupid, but I don't know what.

As you can see in my code below, I ask the user two times for the input, and these values are the values that I want to add to the for loop below. My question now is, what am I doing wrong and how can I insert the input data in the for loop?

import requests
import json

response = requests.get('http://inserturlhere.com')

data =response.text
parsed=json.loads(data)

first_value = input ('Name of first value: ')
second_value = input ('Name of second value: ')

first_test = ['first_value']
second_test = ['second_value']

for product in parsed['products']:
    if product[first_test] == product[second_test] :
        print(product['id'])
6
  • Can you provide a minimal reproducible example? The json snippet is incomplete, requests.get('APIURL') won't run, and we don't know what parse['orders'] refers to.
    – jpp
    Commented Apr 30, 2018 at 23:18
  • 2
    This is largely a guess as to your intentions due to lack of context, but when you write 'first_test' = ['first_value'], that's not doing anything with the var in first_value.
    – Ari K
    Commented Apr 30, 2018 at 23:21
  • @jpp Did some adjustments to my post, the code is working if I use the param anyway let's say in the first input 'first_value' I fill in 'title' and in the second one 'full_title' . I want that in the loop both of these values will be compared with each other. This is only working if my for loop looks like this: for product in parsed['orders']: if product['title'] == product['full_title'] : print(product['id']) But as you can see this is static, I want to make it dynamic to let the user decide which values he wants to compare.
    – Solaiman
    Commented Apr 30, 2018 at 23:25
  • 1
    @Solaiman, Still confused, What is parsed['orders']? [can you print an example] The first and only time orders comes up is in the 3rd last line.
    – jpp
    Commented Apr 30, 2018 at 23:27
  • 1
    @jpp My mistake, I have changed it now. This is how it should be
    – Solaiman
    Commented Apr 30, 2018 at 23:30

2 Answers 2

1

It's not clear what the purpose of first_test and second_test would be in your code. But could this be what you are trying to do?

import requests

response = requests.get('http://inserturlhere.com')
products = response.json()['products']

first_value = input('Name of first value: ')
second_value = input('Name of second value: ')

for product in products:
    if product[first_value] == product[second_value]:
        print(product['id'])

Note that any misspelling in the user inputs for first_value or second_value will result in a KeyError, so you should probably include som error handling with try-except.

3
  • I tried the code above but I get a key error anyway even when I fill in everything right. That's why I tried to work with first_test and second_test to convert both inputs to the same format as print(product['id']) In the hope that it would work then
    – Solaiman
    Commented Apr 30, 2018 at 23:51
  • If you get a key error, then clearly you have not filled out valid key names. What exactly is the keyerror you get?
    – Håken Lid
    Commented May 1, 2018 at 0:04
  • Ugh, I feel so stupid now. I was so close actually if I look at my first code. You are right your solutions works. Thanks!
    – Solaiman
    Commented May 1, 2018 at 0:18
1

Below are a couple of solutions using minimal data. The main reason your code does not work is you are introducing unnecessary variables first_test and second_test.

This assumes, as stated in your question, you wish to test equality of 2 values from your json given 2 parameter inputs.

Setup

products = [{'id': 545453435453,
             'title': "product name",
             'fulltitle': "product name"},
            {'id': 123454686786,
             'title': "product name2",
             'fulltitle': "product name3"}]

first_value = input('Name of first value: ')
second_value = input('Name of second value: ')

Solution 1: iteration

for product in products:
    if product[first_value] == product[second_value] :
        print(product['id'])

# Name of first value: title
# Name of second value: fulltitle
# 545453435453

Solution 2: pandas

This involves the 3rd party library pandas, which will be more efficient for repeated calls.

import pandas as pd

df = pd.DataFrame(products)

res = df[df[first_value] == df[second_value]]

# Name of first value: title
# Name of second value: fulltitle
#
#       fulltitle            id         title
# 0  product name  545453435453  product name
4
  • I didn't work with pandas before,but I will have a look at it. I looked at your solution, but that way I have to declare the values that I receive from the API in my code right looking at your setup?
    – Solaiman
    Commented Apr 30, 2018 at 23:53
  • @Solaiman, No, you don't - see update. The setup part remains the same for both solutions.
    – jpp
    Commented Apr 30, 2018 at 23:54
  • Thanks for the help! I can't choose both of your answers as the solution. But upvoted your post :).
    – Solaiman
    Commented May 1, 2018 at 0:19
  • 1
    @Solaiman, No problem, you accepted a correct answer.. that's all that matters!
    – jpp
    Commented May 1, 2018 at 0:19

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.