0

I'm wrapping a third-party API in a controller (due to security hoops we have to jump through) and the API returns a JSON string.

I do some minor changes to the JSON string and want to return that string.

I don't see a way to do that with a JSONResult, as it requires an object, and returning JSON string is sent back as a string.

Am I stuck with using something like a ContentResult?

JSONLint.com says the modified JSON is valid. It starts with...

[{"Acknowledgment Start Date":null,"Additional Location Details":null,"Area Code":null,"Assign To Vendor":"No",...

If I use the Newtonsoft.Json.JsonConvert(), it does this to my JSON string...

[[[[]],[[]],[[]],[[]],[[]],[[]],[[]],[[]],...

If I use JavaScriptSerializer, I get this again...

[[[[]],[[]],[[]],[[]],[[]],[[]],[[]],[[]],...

I suspect part of the problem is the null values in the JSON string.

Is there another solution? Are there issues with using a ContentResult that I'm not aware of?

1
  • Can you show us your code? How are you making changes to the JSON string? Are you using JObjects or a model? Commented Sep 12, 2019 at 22:29

2 Answers 2

1

Newtonsoft.Json.JsonConvert() serializes an object, it doesn't parse (AKA deserialize) it. What you're doing when you call Newtonsoft.Json.JsonConvert(jsonString) is you're saying "serialize this JSON string to JSON". So, you get a funky result.

You could instead parse the JSON, then make your modifications, then serialize it again. For example:

var myObject = Newtonsoft.Json.DeserializeObject<POCOClass>(jsonString);
myObject.Whatever = "123";
//... etc.

This of course after defining your POCO class like such:

public class POCOClass {
    [JsonProperty(PropertyName = "Acknowledgment Start Date")]
    public string AcknowledgmentStartDate { get; set; }
    // etc.
}

Then when you're done, serialize it back:

jsonString = Newtonsoft.Json.JsonConvert(myObject) // or Newtonsoft.Json.SerializeObject(myObject)
Sign up to request clarification or add additional context in comments.

2 Comments

Alternatively, if they want to use a JsonResult return type, they can also serialize the object with Json(myObject) (though it might require the JsonRequestBehavior parameter as well).
Ultimately, it strikes me as redundant to get a JSON string and deserialize it so it can be represented as a string. That seems like a lot of unnecessary work.
0

Upon closer inspection, I had a couple of realizations.

The first being that the outputted JSON isn't valid because the quoted identifiers include spaces and colons. For example, this one from above.

... ,"Additional Location Details":null, ...

While I suspect this is the root of problems, I don't have the time to write a parser to make it proper JSON.

I will come back and write a tool to properly clean up the mess at some point, but right now I have another approach I'm doing.

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.