0

I am creating an ASP.NET Core Web API that stores and retrieves data from a SQL database. One particular endpoint will take a JSON object, however, this particular JSON object will represent the contents of a JSON configuration file. So, the JSON object being sent across the wire will look like this:

{ 
    "userID": 123,
    "jsonFileName" : "aUserConfigFile.json",
    "jsonFileData" : " ...some dynamic json string... "
}

Where the jsonFileData will be dynamic in length depending on user choices. Just imagine a typical JSON file where there is lots of different bits of data in it.

The caller that will be calling this ASP.NET Core Web API will be a C++ app. The JSON object gets stored in a SQL database.

Also, there is the flip side, where I will need to be able to return this same data back to the calling C++ app.

I've tried to send the jsonFileData to the API using Swagger, but I get bad request errors all pointing to the jsonFileData string.

I guess my question would be: what kind of text treatment can I use on this string to make it usable to send into the API?

4
  • Can you please share a minimal reproducible example? What is the model you are accepting? Commented Aug 15, 2024 at 19:08
  • I mean, what do you do now to serialize an object to a string using JSON? SerializeObject or something like that? So use that to set the jsonFileData property. When you serialize the containing object, SerializeObject (or whatever) will presumably take care of anything that needs to be done to the string value in JSON so that it survives the serialization and deserialization. Commented Aug 15, 2024 at 19:39
  • Examples of this include answers to Serialize object to JSON that already contains one JSON property (uses Newtonsoft Json.NET) and How to serialize an object to a JSON string property instead of an object using Json.Net Commented Aug 15, 2024 at 19:45
  • @user3328969 , have you solved the problem? If not, can you share the JSON object definition? According to your description, I create a model with the related property and use the string data type, then when using it to receive json string, everything works well. You can check this screenshot. Commented Aug 26, 2024 at 5:49

1 Answer 1

0

If I understand your question correctly - then the easiest option would be to send the data as "double encoded JSON", using JSON escapes, hence something like the following:

{ 
    "userID": 123,
    "jsonFileName" : "aUserConfigFile.json",
    "jsonFileData" : "{\"test\":\"value\", \"test1\":1} "
}

Though to put it as JSON object into database you will need to unescape it (i.e. double parse it).

Or you can use some dynamic options from the System.Text.Json in the model (like JsonElement/JsonNode, etc. see the How to use a JSON document object model in System.Text.Json) to represent the jsonFileData property and just send "ordinary" JSON data in the property (hence without double encoding).

Another options might include - using encoding like Base64 to turn the "inner" JSON into string.

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

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.