Once more: "The JSON object should be a string, bytes, or bytearray, not a list" encountered in the requests library

I'm struggling with a simple request and can't figure out what's wrong

data1 = df.loc[N, 'online_raw_json']
print(type(data1))
print(data1)
data1 = json.dumps(data1)
print(type(data1))
print(data1)

response = requests.post("http://127.0.0.1:5000/predict", json=data1)
print(response.text)

OUTPUT:

<class 'str'>
{"batch_id": 230866, "heat_no": "2019333", "tasks":7, "oxy": 41.6, ... "data": []}]}
<class 'str'>
"{\"batch_id\": 230866, \"heat_no\": \"2019333\", \"tasks\": 7, ... \"data\": []}]}"

This results in an error Exception happened in API function: the JSON object must be str, bytes or bytearray, not list

Even though json.dumps should provide valid json format, it is being treated as a string.

Answer №1

json.dumps is designed to convert a Python Dictionary into a string. You can find more information here.

In this scenario, you need to convert the string back to a dictionary using json.loads(data1), for instance.

data1 = df.loc[N, 'online_raw_json']
data1 = json.loads(data1)

response = requests.post("http://127.0.0.1:5000/predict", json=data1)
print(response.text)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Looking for assistance with printing a vertical list?

Being a complete novice to Python, I have exhausted all other options and turned to this platform for help. Despite spending hours on my study notes and searching the internet, I am unable to grasp what I need to do. The list that I am dealing with is str ...

Encountering difficulties extracting audio from the webpage

Attempting to extract audio(under experience the sound) from 'https://www.akrapovic.com/en/car/product/16722/Ferrari/488-GTB-488-Spider/Slip-On-Line-Titanium?brandId=20&modelId=785&yearId=5447'. The code I have written is resulting in an ...

The Ajax call failed to connect with the matching JSON file

<!DOCTYPE html> <html> <body> <p id="demo"></p> <script <script> function launch_program(){ var xml=new XMLHttpRequest(); var url="student.json"; xml.open("GET", url, true); xml.send(); xml.onreadystatechange=fun ...

Mapping an array of values in Retrofit for Android: A step-by-step guide

The API endpoint will provide a JSON response structured as follows: { "students":[ [ 1, "Tom", 18, "USA" ], [ 2, "Linda", 21, "Mexico" ] ], "other":[ ...

a guide on transforming a string array into JSON

I initialized an array like this String[] finalcodes = new String[50] ; and populated it with some values. However, when I print finalcodes, the output is: ["aaa","bbb","ccc"] My goal is to convert this string array into a JSON Object. Can someone pl ...

JavaScript JSON YouTube API viewCount function is not functioning correctly

I'm struggling to retrieve the views of a video using JavaScript (Chrome Extension). Here is the code I have so far: $(function() { $.getJSON('http://gdata.youtube.com/feeds/api/videos/H542nLTTbu0?alt=json',function(data) { ...

Formatting blocks of data using xlsxwriter in Python with loop iterations

My goal is to impress my colleagues at work by creating a spreadsheet (.xlsx) using Python, even though I am still relatively new to the language. Can someone help me figure out how to format specific cells in my code using xlsxwriter? In the snippet bel ...

Managing Related Data with JSONPlaceholder and MVC 5

Attempting to present a paginated table of information about "Albums" within an MVC 5 project, collecting data from various records sourced through the JSONPlaceholder REST API: View Source Users: (10 entries) { "id": 1, "name" ...

"Transforming JSON data into a format compatible with Highcharts in PHP: A step-by-step

Currently facing an issue with converting the given array format into a Highcharts compatible JSON to create a line chart. Although everything else is functioning correctly, I am struggling with this specific conversion task. { name: [ 1000, ...

Populate the dropdown menu with data from a JSON file

Recently, I created a custom JSON file and wanted to populate a select>option using this data. However, I encountered an error message saying: Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at file:///C:/.../p ...

Python Enum using an Array

I am interested in implementing an enum-based solution to retrieve an array associated with each enum item. For example, if I want to define a specific range for different types of targets, it might look like this: from enum import Enum class TargetRange ...

How to calculate the sum of elements from multiple lists containing [] elements in Python?

_Greetings, fellow beings! I am currently facing an issue with removing elements from multiple lists. The scenario involves handling a set of lists. print("Initiating") print(dateTableMonthValues) Upon execution, the output is: [['4:10&apos ...

dividing binary numbers by 2 and 8

My task is to determine if certain binary numbers are divisible by 2 or 8 and then report the total count. If the last digit of a binary number is 0, it's divisible by 2, and if the last three digits are all 0, it's divisible by 8. twos = 0 eigh ...

Mapping Coordinate Points on a Three-Dimensional Sphere

I am having trouble generating uniformly distributed points on a sphere. The current code seems to be creating points that form a disk shape instead of being spread evenly across the surface of the sphere. I suspect that the issue lies in the calculation f ...

The TextView is not displaying the Android Json data retrieved from mysqli

Currently, I am conducting a basic test to display JSON data fetched from PHP MySQLi in a TextView. TextView mTxtDisplay; String url = "http://192.168.1.102/web_service/test.php/"; @Override protected void onCreate(Bundle savedInstanceState) { super ...

How to remove a specified number of characters from a pandas column based on the values in another column in Python

Looking at my DataFrame: df = pd.DataFrame({'A':['elisabeth','adamms','jorhgen'], 'B':[2,2,3]}) I am trying to remove the last characters from the strings in column A based on the values in column B w ...

I am having trouble with the find_elements_by_class_name function in Selenium using Python

I am searching for web elements that share the same class name in order to capture screenshots of elements for my application. I have opted to utilize Selenium with Python for this task. url = "https://www.pexels.com/search/happy/" driver = webdr ...

How to dynamically generate data with exceljs

Currently, I am attempting to download JSON data into an Excel spreadsheet. One of my requirements is to insert an image in cell a1 and then bold the headers. The code snippet below was sourced from Google; however, I need to populate the data dynamically ...

Transforming a string list into a list of numeric values

I am dealing with a list that has the following structure: mylist = ['1,2,3'] As it stands, this is a list containing one string. My goal is to transform it into a list of integers like so: mylist = [1,2,3] My attempt using [int(x) for x in m ...

Managing JSON object with irregular data in Angular 7: Best Practices

When the service returns data in a specific format, I am able to view the data in the developer tools. {"results":{"BindGridDatatable":[{"ID":"0005","Name":"Rohit"}, {"ID":"0006","Name":"Rahul"}], "Totalvalue":119}} ...