1

I need to add some more data into existing JSON

eg:

{
    "OrderId":"abc",
    "products":["a","b","c","etc"]
}

how to add more into products

8
  • There is no such thing as a JSON Array. JSON is a text format, like CSV or XML. Commented Jun 3, 2020 at 11:59
  • As such, how do you have this JSON right now? As a string? As an object? Commented Jun 3, 2020 at 12:01
  • 1
    @HereticMonkey Your linked answer is about javascript, not c#. Also, despite being text format json has concept of array elements. Commented Jun 3, 2020 at 12:07
  • 1
    The proper duplicate is json add new object to existing json file C# Commented Jun 3, 2020 at 12:11
  • 1
    @HereticMonkey 1) this is also not a proper duplicate 2) if you don't agree with wording you can dicuss it with w3schools or ietf rfc having next wording: "This is a JSON array containing two objects:" Commented Jun 3, 2020 at 12:14

2 Answers 2

0

The approach would be similar to used in answer to your previous question, but you will need to convert element to JArray:

var x = @"{
    'OrderId':'abc',
    'products':['a','b','c','etc']
}";

var jObj = JObject.Parse(x);
((JArray)jObj["products"]).Add("new");
Sign up to request clarification or add additional context in comments.

1 Comment

@RoadsideRomeozz was glad to help.
-1

Try this:

var jObject = JObject.Parse(json); 
var jArray = jObject["products"] as JArray; 
jArray?.Add("new_product");

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.