0

In C sharp, may I ask how to decompose a string containing a json array into an array with each element as a json string? Thanks!

One example of the string is as following.

var string = "[{"id": 111, "value": 22, "timestamp": "2021-09-20T02:34:17.000Z"},{"id": 112, "value_1": 23, "value_2": 24, "timestamp": "2021-09-20T02:33:17.000Z"}]"

I want to get an array as below.

var messages = new[]
{
@"{'id': 111, 'value': 22, 'timestamp': '2021-09-20T02:34:17.000Z'}",
@"{'id': 112, 'value_1': 23, 'value_2' : 24, 'timestamp': '2021-09-20T02:33:17.000Z'}"
}.AsEnumerable();

I tried to use JsonConvert.DeserializeObject(string), but it does not work with error Unexpected character encountered while parsing value: [.

Thanks!

yyuankm
  • 295
  • 4
  • 22
  • https://www.newtonsoft.com/json/help/html/deserializeobject.htm **Edit**: you are trying to deserialize an array of objects and have to adjust accordingly: https://stackoverflow.com/questions/18192357/deserializing-json-object-array-with-json-net – J. S. Garcia Sep 22 '21 at 07:04
  • Thanks. I do not want to Deserialize into an Object. The json string also has different schema. I just want to have an array containing each string. – yyuankm Sep 22 '21 at 07:08
  • 1
    using Newtonsoft.Json.Linq you can deserialize to a JObject and select the values from there. **edit**: you can also deserialize to a JArray – J. S. Garcia Sep 22 '21 at 07:11
  • 1
    You can refer this question: https://stackoverflow.com/questions/69084053/cant-convert-json-to-c-sharp-class/69084847#69084847 – Le Vu Sep 22 '21 at 07:21
  • 1
    maybe this will help you further: [enumerate through a JObject](https://stackoverflow.com/a/10543553/15406539) – J. S. Garcia Sep 22 '21 at 07:22
  • This seems like an X/Y problem, why do you need to do this? – TheGeneral Sep 22 '21 at 08:10
  • We have a use case, which needs to decompose a string containing json array into multiple elements in an array. The array with json string is expected by another function. – yyuankm Sep 22 '21 at 08:49

2 Answers2

1

is this what you want?

using System;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

string input = "[{\"id\": 111, \"value\": 22, \"timestamp\": \"2021 - 09 - 20T02: 34:17.000Z\"},{\"id\": 112, \"value_1\": 23, \"value_2\": 24, \"timestamp\": \"2021 - 09 - 20T02: 33:17.000Z\"}]";

// Read Json into JArray
JArray array = JArray.Parse(input);

// Serialize each nested object into string Array
string[] output = array.Select((a) => JsonConvert.SerializeObject(a)).ToArray();


foreach (string line in output)
    Console.WriteLine(line);

output:

{"id":111,"value":22,"timestamp":"2021 - 09 - 20T02: 34:17.000Z"}
{"id":112,"value_1":23,"value_2":24,"timestamp":"2021 - 09 - 20T02: 33:17.000Z"}
J. S. Garcia
  • 366
  • 1
  • 9
0

One of the reasons for the error may be that you are using different names for the objects in the json text. For example: value and value_2

if the value is more than one and dynamic then it is more convenient to specify it in an array. like this.

{'id': 112, ['value_1': 23, 'value_2' : 24], 'timestamp': '2021-09-20T02:33:17.000Z'}

for this you need to edit the json text then adapt the object to it.

give this a try

JsonConvert.DeserializeObject<MyObject>("//Your Json string");

Converts the given Json text to the object you give. If Json and object are different it will throw an error . look here

thank you USEFUL for feedback if it worked for you

Emin Niftiyev
  • 197
  • 1
  • 13
  • Thank you! Instead of Deserializing into objects, I just want to get an array with json string with different schema. – yyuankm Sep 22 '21 at 08:32