Click here to Skip to main content
14,355,411 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi,

I have a JSON file which has an array of channels for a fixture. But if that fixture has multiple cells, some of the channels need to be repeated. My JSON file does this by having an array of each cell, and then another array of the channels that need to be repeated for each cell. These two arrays are inside an array inside the main channels array. I need a way to check if the main channels array has another array inside to determine whether the fixture has multiple cells or not. I cannot change the JSON file since it is coming from another source and I want the fixture files to be easily updateable.

This is my JSON:

{
      "name": "16: Effect RGB",
      "channels": [
        "Dimmer Master",
        "Strobe Master",
        "Programs",
        "Program Speed",
        "Program Fade Time",
        "Program Direction",
        "Program Group Size",
        "Group Offset",
        "Restart Program",
        {
          "insert": "matrixChannels",
          "repeatFor": [
            "Effect Color 1",
            "Effect Color 2",
            "Effect Color 3",
            "Effect Color 4"
          ],
          "channelOrder": "perPixel",
          "templateChannels": [
            "Red $pixelKey",
            "Green $pixelKey",
            "Blue $pixelKey"
          ]
        }
      ]
    },


What I have tried:

I'm unsure really how to check this in C#. I can get it to check how many items are in the main channels array but then obviously when it comes to the array within the channels array, it spits out an error. Any help would be appreciated! Cheers!
Posted
Updated 2 days ago
Comments
Richard MacCutchan 2 days ago
   
"it spits out an error."
What is "it", and what is the error?
Member 14647867 2 days ago
   
Sorry this is the error: Newtonsoft.Json.JsonReaderException: 'Unexpected character encountered while parsing value: {. Path 'modes[15].channels[8]', line 1666, position 9.'
Richard MacCutchan 2 days ago
   
Assuming that is actually: {. Path 'modes[15].channels[8]' then either the full stop after the opening brace, or the single quote before modes is chcharacter 9. I suspect neither of them belong in a JSON string.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Normally I would desalinize the JSON string into a C# model and use C# to do this, using Newton

Newton Json -Deserialize an Object[^]


If you dont want to do this and would rather use the JSON itself I would have a look at this

Get values from JSON object[^]
   
Comments
Richard MacCutchan 2 days ago
   
See comments above, OP is already using NewtonSoft.
Member 14647867 2 days ago
   
The issue is here that I have arrays within arrays within arrays and I need to check the type to see if it is an object or an array
Richard MacCutchan 2 days ago
   
Newtonsoft will do that for you. Your problem is that the JSON text contains invalid characters so the parsing fails.
Simon_Whale 2 days ago
   
I've checked the sample that you gave above in an online Json validator (https://jsonlint.com) and it returned no errors. So I think you will need to review the models that you are parsing into.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100