0

I need some help to map a json array without keys. For examples:

[
    "value1",
    234,
    3034,
    "data",
 [
    "some value",
    null,
    2020
 ],
]

I created a class based on these values received, but i'm not able to map to a class object. I just could access this data using a dynamic variable, like:

dynamic object = DataFromJson();
var firstvalues = object[0]; // "value1"

My json is too big with many arrays inside, so accessing with indexes is a big big process.

Does someones know how to access this kind of data and map to a class?

Samuel Finatto
  • 147
  • 1
  • 1
  • 7
  • Have you seen this post? https://stackoverflow.com/questions/9988395/how-to-map-json-to-c-sharp-objects – Jay Mason May 28 '21 at 14:15
  • This isn't JSON format. JSON must contains keys and values. – Dmitry Grebennikov May 28 '21 at 14:17
  • I know it's weird, but I'm receiving this data like that, without keys. That's why I'm having trouble to map that. – Samuel Finatto May 28 '21 at 14:19
  • @JayMason Is not the same, I don't have keys (weird) on this json. – Samuel Finatto May 28 '21 at 14:20
  • 1
    @DmitriyGrebennikov This is perfectly valid JSON. It's a single JSON value (an array) that contains other JSON values (arrays) of other json values (strings/numbers/nulls/booleans). – gnud May 28 '21 at 14:20
  • @DmitriyGrebennikov as you see, is not using "{ }", but "[ ]" as an array. – Samuel Finatto May 28 '21 at 14:21
  • That parses as JSON. It's an array. The first four values in the array are strings and numbers, the last one is an an array. You should be able to deserialize it into either a `dyanamic` or an `object[]` (or `dynamic[]`) – Flydog57 May 28 '21 at 14:21
  • @SamuelFinatto https://stackoverflow.com/a/44318621/6181153 – Jay Mason May 28 '21 at 14:21
  • @JayMason still with keys... I want to map a json array. – Samuel Finatto May 28 '21 at 14:22
  • @JayMason but that is an invalid json object. This question is about a valid json array. – gnud May 28 '21 at 14:22
  • You aren't going to be able to use any keys or classes with this. It's just an array of stuff. – Flydog57 May 28 '21 at 14:23
  • @Flydog57 yeah, i mentioned that later in post, but this json is very big, up there is just an example. If I suppose to use dynamic, there is some access like "object[1][0][2][1]"... – Samuel Finatto May 28 '21 at 14:23
  • 1
    You can't deserialize it if it doesn't have a constant format (Element 1 contained 4 items, Element 2 contained 3). You either have to change it to utilize keys, or you have to manually parse the data and build your own objects. – Jay Mason May 28 '21 at 14:24
  • @JayMason that was my feeling... :( I didn't want to map each value on this array. But if there is any method able to take, like in sequence, i will manually map that... – Samuel Finatto May 28 '21 at 14:26
  • If there's some meaning to the data, you could walk through it (with a simple loop(s)) and parse it into something you can use later. If it's just clutter in a closet, you're probably on your own – Flydog57 May 28 '21 at 14:26
  • If the data has no structure, what are you actually going to do with it anyway? You can't reliably process it into anything useful. – DavidG May 28 '21 at 14:32
  • 1
    What serializer are you using? If using Json.NET see [How to deserialize an array of values with a fixed schema to a strongly typed data class?](https://stackoverflow.com/q/39461518/3744182). – dbc May 28 '21 at 14:32
  • @DavidG because I'm receiving like that. I know it's not right, but I can recognize all values to insert into a class object. And this values I need to map to a object to be used in the code. If anyone have already some option, then maybe I'll close this question :( – Samuel Finatto May 28 '21 at 14:36
  • 1
    Then tell us how you recognise the values that go into your classes and we can tell you how to do it. – DavidG May 28 '21 at 14:37
  • 1
    you can use `JArray jsonArray = JArray.Parse(DataFromJson());` .. With `jsonArray.Children()` you can use `LINQ` expression to get the value of any property. – Akshay G May 28 '21 at 14:38
  • 1
    There is no property in your case, I am not exactly sure how u can recognize just based on the value.. But JArray will definitely help you in iterating over the data. With the `HasValues` property, you can iterate to the most innermost array. You can never map an array of data to a class. – Akshay G May 28 '21 at 14:48

2 Answers2

0

I received a lot of feedbacks regarding that (thanks!), so here is the conclusions:

  • as a JSON array, this kind of information is not correct to map to a class object. This is not the functional proposal.

  • JArray can be really helpful to interate between all the information, and then take it to a class. If needed, you could manually create a specific method to include each value in your class object.

  • Another option is to use a dynamic object, accessing as indexes.

  • Make sure that this information can not be received in another way, as a JSON with key and values, and map to a class.

If there is some useful update about that, I will edit here.

Samuel Finatto
  • 147
  • 1
  • 1
  • 7
0

Well there is one option available, using Cinchoo ETL - an open source library to load such data to objects out of the box.

Define object as below

[ChoSourceType(typeof(object[]))]
[ChoTypeConverter(typeof(ChoArrayToObjectConverter))]
public class foo
{
    [ChoArrayIndex(0)]
    public long prop1 { get; set; }
    [ChoArrayIndex(1)]
    public double prop2 { get; set; }
}

Then parse the JSON as below

string json = @"
[
    [
        1618170480000,
        ""59594.60000000"",
        ""59625.00000000"",
        ""59557.13000000""
    ],
    [
        1618170540000
    ]
]";

using (var r = ChoJSONReader<foo>.LoadText(json))
{
    foreach (var rec in r)
        Console.WriteLine(ChoUtility.Dump(rec));
}
Cinchoo
  • 6,088
  • 2
  • 19
  • 34