"Utilizing Golang's encoding/json package to include null values during marsh

Hello, currently I am using Golang for encoding/json and encountering an issue where the JSON error is returning as null:

{"user_message":"Can't find any Query with those parameters","application_context":"GroupsRepository.GetGroupsByQuery: ApplicationError: UserMessage - Error querying database for many Groups. ApplicationContext - Groups.GetMany: pq: column \"refill_too_soon_gpi_digits\" does not exist"} null

My marshalling method looks like this:

func EncodeErrorResponse(w http.ResponseWriter, err error, status int) {
    w.WriteHeader(http.StatusOK)
    w.Header().Set("Content-Type", "application/json")
    json.NewEncoder(w).Encode(err)
}

If anyone could provide assistance, I would greatly appreciate it.

Answer №1

After analyzing the JSON output provided, it appears that the error is encoded properly within the response. However, there seems to be an issue where a 'null' value is being appended after the JSON data, suggesting another write operation taking place separately. It's possible that the function responsible for calling EncodeErrorResponse is followed by additional output being written to the ResponseWriter.

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

Combining Python, Neo4j, and JSON to uniquely create a new NODE for each user

I am looking to create a Python script that can accomplish the following tasks: Load a JSON file containing user data Create a node for each user in the JSON file ## Sample User Data {'UserName': 'lolipop', 'UserId': '5 ...

What is the process for retrieving the API configuration for my admin website to incorporate into the Signin Page?

My admin website has a configuration set up that dynamically updates changes made, including the API. However, I want to avoid hardcoding the base URL for flexibility. How can I achieve this? Please see my admin page with the config settings: https://i.st ...

Exploring the Possibilities of Utilizing jqPlot with JSON Data

I've been working on retrieving a JSON string through an Ajax call in jQuery and trying to visualize that data in a bar chart using jqPlot. Although I found the JSON conversion code on a Stack Overflow post, I'm facing difficulties as it's ...

A guide on utilizing Linq to read a Json file in c#

I have a JSON file with role_to_secrets for "rec" and need to retrieve the corresponding secret values for the "prod" environment. For example, if roles under "rec" are "resdns", "dnsmoon", and "resmoon", I should fetch the matching secrets for "prod": " ...

Dividing a JSON array into two separate rows with Spark using Scala

Here is the structure of my dataframe: root |-- runKeyId: string (nullable = true) |-- entities: string (nullable = true) +--------+--------------------------------------------------------------------------------------------+ |runKeyId|entities ...

Python - Transforming a string containing escape sequences into JSON

My syslog file contains JSON objects that need to be extracted and converted into JSON format. The challenge arises when certain strings within the log have an escape character, causing issues with parsing using json.loads. Here is the specific problem: ...

Utilizing NodeJS to Extract Data from MongoDB and Export to JSON File

I'm struggling to figure out how to write a specific field from my mongodb database to a json file using node js. Can anyone provide guidance or resources? I haven't had much luck finding helpful information so far. ...

Looking to transform a list into JSON format using JavaScript?

I have a collection that looks like this: <ol class="BasketballPlayers"> <li id="1">Player: LeBron, TotalPoints: 28753, MVP: true</li> <li id="2">Player: Steph, TotalPoints: 17670, MVP: true< ...

Having trouble getting your Ajax script to function correctly when submitting a form?

I am currently facing an issue where I am loading a partial page, but I do not want the form on this page to redirect when the save button is clicked. I am unsure if I am using the script correctly. I would like to post to the controller when the submit b ...

What is the process for including a scope in an Angular.js HTTP controller?

I am looking to access a $scope variable within this controller in order to utilize Angular functions like ng-click and ng-change in my HTML. Currently, I am only able to use the $http function but unable to execute any Angular methods on it. I am struggli ...

Adding a JSON array to a ListView

I am new to Java programming language, so please forgive any obvious or silly mistakes I may have made. I am working on an activity in which I retrieve a JSON encoded string from a PHP file and display it in a simple list view. However, I am encountering ...

I am facing difficulties with PHP when using a while loop for array indexing and encoding JSON

Running this code on our localhost results in success: if($result){ if($tmp=$result->num_rows){ while($row = $result->fetch_assoc()) { $myArray[] = $row; } echo json_encode($myArray[0]); } } However, ...

Decoding JSON data retrieved from the Pokeapi

Looking for help on how to effectively call an API known as Pokeapi and then parse the received data into an object using an external library called PokeApi.NET in C#. I am able to get a response from the API in my C# code, but struggle with parsing it int ...

Using Gson library for JSON parsing in Android application and encountering an issue

After executing a Web Service, I received a JSON response. However, when trying to convert this JSONObject (org.json.JSONObject) into a specific object using the Gson library, my application crashes unexpectedly. The error message I receive is: { "atribut ...

The dot operator cannot be used to access Json objects

Utilizing a4j jsFunction to transmit data to the server and receive JSON in return <a4j:jsFunction name="submitData" action="#{imageRetriveBean.saveData}" data="#{responseNodesPathsBean}" oncomplete="processData(event.data)"> <a4j:param name= ...

Learning to parse a JSON file in Java

I need assistance with reading a Json file. The content of my Json File is as follows: [ { "arguments" : [ { "IsEnabled" : "false", "class" : "UITextField", "width" : 238, "parent" : { "class" : "UIVie ...

There seems to be an iPhone error involving a duplicate symbol _SBJSONErrorDomain located in the directory /Users/Shared/PhoneGap/Frameworks/PhoneGap.framework/Phone

I am currently working on a Phonegap project where I need to integrate ASIHTTPRequest and JSON references. However, I encountered the following error: ld: duplicate symbol _SBJSONErrorDomain in /Users/Shared/PhoneGap/Frameworks/PhoneGap.framework/Pho ...

How can I retrieve information from an HTML or JavaScript object?

Imagine a scenario where you have an HTML table consisting of 5,000 rows and 50 columns, all generated from a JavaScript object. Now, suppose you want to send 50 checked rows (checkbox) from the client to the server using HTTP in JSON format. The question ...

I am attempting to include the headers in my HTTP GET Json request. The API key documentation has provided me with a specific format for the API key that I need to use

This is the original format: response = requests.get(PAPERQUOTES_API_ENDPOINT, headers= {'Authorization': 'TOKEN {}'.format(TOKEN)}) Here is my attempt to replicate it (still unsure about the use of brackets and the function format ...

JavaScript: a single function and two calls to Document.getElementById() results in both returning "undefined"

Within my JavaScript file, I have a function that utilizes two variables: choice and courseChosen. The latter variable must be converted into an object first. In the HTML, the tags courseName and courseInfo are used for choice and courseChosen respectively ...