'Python: Make duplicate keys unique json response

I am extracting data through POST method and let's say we have different post data in a list and for each request there will be a different post data like this:

cookies = {
    "_sp_id.cf1a": "205e16a5-8970-4c92-97b8-969eebfcbb63.1647289721.7.1648545896.1648465133.73852927-e047-4c36-bae7-90c001509900",
    "_sp_ses.cf1a": "*",
}

headers = {
    "User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:98.0) Gecko/20100101 Firefox/98.0",
    "Accept": "text/plain, */*; q=0.01",
    "Accept-Language": "en-US,en;q=0.5",
    "Accept-Encoding": "gzip, deflate, br",
    "Referer": "https://www.tradingview.com/",
    "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
    "Origin": "https://www.tradingview.com",
    "Connection": "keep-alive",
    # Requests sorts cookies= alphabetically
    # 'Cookie': '_sp_id.cf1a=205e16a5-8970-4c92-97b8-969eebfcbb63.1647289721.7.1648545896.1648465133.73852927-e047-4c36-bae7-90c001509900; _sp_ses.cf1a=*',
    "Sec-Fetch-Dest": "empty",
    "Sec-Fetch-Mode": "cors",
    "Sec-Fetch-Site": "same-site",
    "Sec-GPC": "1",
    # Requests doesn't support trailers
    # 'TE': 'trailers',
}

    data = [
        '{"filter":[{"left":"name","operation":"nempty"},{"left":"exchange","operation":"in_range","right":["AMEX","NASDAQ","NYSE"]},{"left":"High.All","operation":"eless","right":"high"},{"left":"is_primary","operation":"equal","right":true},{"left":"subtype","operation":"nequal","right":"preferred"}],"options":{"lang":"en","active_symbols_only":true},"markets":["america"],"symbols":{"query":{"types":[]},"tickers":[]},"columns":["logoid","name","close","change","change_abs","Recommend.All","volume","Value.Traded","market_cap_basic","price_earnings_ttm","earnings_per_share_basic_ttm","number_of_employees","sector","description","type","subtype","update_mode","pricescale","minmov","fractional","minmove2","currency","fundamental_currency_code"],"sort":{"sortBy":"name","sortOrder":"asc"},"range":[0,150]}',
        '{"filter":[{"left":"name","operation":"nempty"},{"left":"exchange","operation":"in_range","right":["AMEX","NASDAQ","NYSE"]},{"left":"High.All","operation":"eless","right":"high"},{"left":"is_primary","operation":"equal","right":true},{"left":"subtype","operation":"nequal","right":"preferred"}],"options":{"lang":"en","active_symbols_only":true},"markets":["america"],"symbols":{"query":{"types":[]},"tickers":[]},"columns":["logoid","name","change|1","change|5","change|15","change|60","change|240","change","change|1W","change|1M","Perf.3M","Perf.6M","Perf.YTD","Perf.Y","beta_1_year","Volatility.D","description","type","subtype","update_mode","currency","fundamental_currency_code"],"sort":{"sortBy":"name","sortOrder":"asc"},"range":[0,150]}',
    ]
    
    
        with httpx.Client() as client:
            for d in data:
                r = client.post(
                    "https://scanner.tradingview.com/america/scan",
                    headers=headers,
                    cookies=cookies,
                    data=d,
                )

I store post data in a list and iterating over it and passing it in request object, so far so good. Post requests returns json object and problem begins here, every request returns different json object except keys, they are the same for e.g

'{"filter":[{"left":"name","operation":"nempty"},{"left":"exchange","operation":"in_range","right":["AMEX","NASDAQ","NYSE"]},{"left":"High.All","operation":"eless","right":"high"},{"left":"is_primary","operation":"equal","right":true},{"left":"subtype","operation":"nequal","right":"preferred"}],"options":{"lang":"en","active_symbols_only":true},"markets":["america"],"symbols":{"query":{"types":[]},"tickers":[]},"columns":["logoid","name","change|1","change|5","change|15","change|60","change|240","change","change|1W","change|1M","Perf.3M","Perf.6M","Perf.YTD","Perf.Y","beta_1_year","Volatility.D","description","type","subtype","update_mode","currency","fundamental_currency_code"],"sort":{"sortBy":"name","sortOrder":"asc"},"range":[0,150]}'

this post data returns this json:

{
  "totalCount": 141,
  "data": [
    {
      "s": "NYSE:ABBV",
      "d": [
        "abbvie",
        "ABBV",
        0,
        0.13602918,
        0.2662209,
        0.37497288,
        0.6400696,
        0.39670241,
        0.39670241,
        9.60952832,
        20.52236029,
        48.81477398,
        19.62333826,
        51.75676942,
        0.5128781,
        1.56710337,
        "AbbVie Inc.",
        "stock",
        "common",
        "delayed_streaming_900",
        "USD",
        "USD"
      ]
    }
  ]
}

and post data

{"filter":[{"left":"name","operation":"nempty"},{"left":"exchange","operation":"in_range","right":["AMEX","NASDAQ","NYSE"]},{"left":"High.All","operation":"eless","right":"high"},{"left":"is_primary","operation":"equal","right":true},{"left":"subtype","operation":"nequal","right":"preferred"}],"options":{"lang":"en","active_symbols_only":true},"markets":["america"],"symbols":{"query":{"types":[]},"tickers":[]},"columns":["logoid","name","close","change","change_abs","Recommend.All","volume","Value.Traded","market_cap_basic","price_earnings_ttm","earnings_per_share_basic_ttm","number_of_employees","sector","description","type","subtype","update_mode","pricescale","minmov","fractional","minmove2","currency","fundamental_currency_code"],"sort":{"sortBy":"name","sortOrder":"asc"},"range":[0,150]}'

returns this which is similar to the last one i.e. keys but values are different

{
  "totalCount": 141,
  "data": [
    {
      "s": "NYSE:ABBV",
      "d": [
        "abbvie",
        "ABBV",
        161.97,
        0.39670241,
        0.64,
        0.42121212,
        4516453,
        731529892.41,
        286085169370,
        25.01356652,
        6.4775,
        50000,
        "Health Technology",
        "AbbVie Inc.",
        "stock",
        "common",
        "delayed_streaming_900",
        100,
        1,
        "false",
        0,
        "USD",
        "USD"
      ]
    }
  ]
}

Hence I am getting just one json object which is the last one in the loop instead of both.

Desired Output:

    {
  "overview": {
    "totalCount": 141,
    "data": [
      {
        "s": "NYSE:ABBV",
        "d": [
          "abbvie",
          "ABBV",
          161.97,
          0.39670241,
          0.64,
          0.42121212,
          4516453,
          731529892.41,
          286085169370,
          25.01356652,
          6.4775,
          50000,
          "Health Technology",
          "AbbVie Inc.",
          "stock",
          "common",
          "delayed_streaming_900",
          100,
          1,
          "false",
          0,
          "USD",
          "USD"
        ]
      }
    ]
  },
  "performance": {
    "totalCount": 141,
    "data": [
      {
        "s": "NYSE:ABBV",
        "d": [
          "abbvie",
          "ABBV",
          0,
          0.13602918,
          0.2662209,
          0.37497288,
          0.6400696,
          0.39670241,
          0.39670241,
          9.60952832,
          20.52236029,
          48.81477398,
          19.62333826,
          51.75676942,
          0.5128781,
          1.56710337,
          "AbbVie Inc.",
          "stock",
          "common",
          "delayed_streaming_900",
          "USD",
          "USD"
        ]
      }
    ]
  }
}

I am looking for some solution to handle this kind of duplication in json response and get data from different post data on each request by making the duplicate keys to unique.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source