Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8878351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:41:30+00:00 2026-06-14T19:41:30+00:00

I have a JSON string that I want to be able to amend in

  • 0

I have a JSON string that I want to be able to amend in C#. I want to be able to update a value in the results when I meet a certain condition.

Take the following

 {
  "responseHeader":{
    "status":0,
    "QTime":0,
    "params":{
      "explainOther":"",
      "fl":"*,score",
      "indent":"on",
      "start":"0",
      "q":"*:*",
      "hl.fl":"",
      "qt":"",
      "wt":"json",
      "fq":"",
      "version":"2.2",
      "rows":"2"}
  },
  "response":{"numFound":2,"start":0,"maxScore":1.0,"docs":
  [{
        "id":"438500feb7714fbd9504a028883d2860",
        "name":"John",
        "email":"john@email.com"
        "dateTimeCreated":"2012-02-07T15:00:42Z",
        "dateTimeUploaded":"2012-08-09T15:30:57Z",
        "score":1.0
   },
   {
        "id":"2f7661ae3c7a42dd9f2eb1946262cd24",
        "name":"David",
        "email":"david@email.com"
        "dateTimeCreated":"2012-02-07T15:02:37Z",
        "dateTimeUploaded":"2012-08-09T15:45:06Z",
        "score":1.0
    }]
 }}

I want to be able to update the name and email element values when I find a result with a certain Id.

For example I would want to update the name and email element where that docs Id equals “438500feb7714fbd9504a028883d2860” and update the name value to Richard and the email value to richard@email.com. The result of this is shown below.

 {
  "responseHeader":{
    "status":0,
    "QTime":0,
    "params":{
      "explainOther":"",
      "fl":"*,score",
      "indent":"on",
      "start":"0",
      "q":"*:*",
      "hl.fl":"",
      "qt":"",
      "wt":"json",
      "fq":"",
      "version":"2.2",
      "rows":"2"}
  },
  "response":{"numFound":2,"start":0,"maxScore":1.0,"docs":
  [{
        "id":"438500feb7714fbd9504a028883d2860",
        "name":"Richard",
        "email":"richard@email.com"
        "dateTimeCreated":"2012-02-07T15:00:42Z",
        "dateTimeUploaded":"2012-08-09T15:30:57Z",
        "score":1.0
   },
   {
        "id":"2f7661ae3c7a42dd9f2eb1946262cd24",
        "name":"David",
        "email":"david@email.com"
        "dateTimeCreated":"2012-02-07T15:02:37Z",
        "dateTimeUploaded":"2012-08-09T15:45:06Z",
        "score":1.0
    }]
 }}

Performance is a consideration as I will need to process many JSON strings, so please bear that in mind.

Thanks in advance
Andrew

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T19:41:31+00:00Added an answer on June 14, 2026 at 7:41 pm

    You can use “Newtonsoft.Json” for this http://json.codeplex.com/

    It’s very easy to use! I wrote a little example which exactly does what you wanted:

    Test Method:
    [C#]

        private void Test(){
            byte[] Bytes = File.ReadAllBytes("json.txt");
            string Json = Encoding.ASCII.GetString(Bytes);
    
            Response JsonResponse = JsonConvert.DeserializeObject<Response>(Json);
    
            if (JsonResponse != null){
                if (JsonResponse.response != null){
                    foreach(Document Doc in JsonResponse.response.docs){
                        if (Doc.id == "2f7661ae3c7a42dd9f2eb1946262cd24"){
                            Doc.name = "David";
                            Doc.email = "david@email.com";
                        }
                    }
    
                    string JsonMod = JsonConvert.SerializeObject(JsonResponse, Formatting.Indented);
    
                    BinaryWriter Writer = new BinaryWriter(File.Create("JsonMod.txt"));
                    Writer.Write(Encoding.ASCII.GetBytes(JsonMod));
                    Writer.Close();
                    Writer.Dispose();
                }
            }
        }
    

    Json classes:
    [C#]

    [JsonObject(MemberSerialization.OptIn)]
    public class Response{
    
        [JsonProperty]
        public ResponseHeader responseHeader{
            get; set;
        }
    
        [JsonProperty]
        public ResponseContent response{
            get; set;
        }
    
    }
    
    [JsonObject(MemberSerialization.OptIn)]
    public class ResponseHeader{
        [JsonProperty]
        public int Status{
            get; set;
        }
    
        [JsonProperty]
        public int QTime{
            get; set;
        }
    
        [JsonProperty]
        public object Params{
            get; set;
        }
    }
    
    [JsonObject(MemberSerialization.OptIn)]
    public class ResponseContent{
    
        [JsonProperty]
        public int numFound{
            get; set;
        }
    
        [JsonProperty]
        public int start{
            get; set;
        }
    
        [JsonProperty]
        public double maxScore{
            get; set;
        }
    
        [JsonProperty]
        public Document[] docs{
            get; set;
        }
    }
    
    [JsonObject(MemberSerialization.OptIn)]
    public class Document{
    
        [JsonProperty]
        public string id{
            get; set;
        }
    
        [JsonProperty]
        public string name{
            get; set;
        }
    
        [JsonProperty]
        public string email{
            get; set;
        }
    
        [JsonProperty]
        public DateTime dateTimeCreated{
            get; set;
        }
    
        [JsonProperty]
        public DateTime dateTimeUploaded{
            get; set;
        }
    
        [JsonProperty]
        public double score{
            get; set;
        }
    }
    

    Oh, and please have look on the json you’ve posted – after “email”:”x@y.de” you missed a “,” – this will lead to a parsing exception 😉

    Cheers!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JSON string that I would like to include as a value
I have a variable that contains the following JSON string: { 0 : Jun
I have a string: items[0].name that I want to apply to a JSON object:
I have JSON string that has nested objects with dynamic names that vary each
I have a JSON string that looks like this: { package1: { type: envelope,
I have a VB6 application that is creating a JSON string and posting it
How do you make JS think that a string is JSON ? I have
I have a json string like this: { d: { results: [ { __metadata:
Hi I have a JSON string that looks like this (Usingt Rails and a
I have an entity framework entity that i want to serialize as a json

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.