How to read JSON that contains field that can have different types

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite
1












I am writing a callout in Apex with returns a JSON file with a field that does not have a consistent type. The JSON returned is quite complicated, so I have been trying to read the JSON with a Apex Class structure within salesforce, but this struggles with a varying field type. Is there any way to deal with this in Apex? The location of this varying field (in a fairly simple object list) makes me wonder if I could read only this list as a string so only have to use a nextToken etc for this section? simplified JSON and class structure below. value is the field that varies in type.



`




"users": [

"id": 461774,
"email": "test@test.co.uk",
"name": "test Student",
"sub_network_ids": [
18307
],
"custom_attributes": [

"id": 2310,
"name": "isReady",
"value": true,
"type": "CustomizableAttribute::Boolean"
,

"id": 2323,
"name": "othernames",
"value": "something",
"type": "CustomizableAttribute::Text"

]

]

`


Code:



public class HivebriteUserList
public List<HivebriteUserRecords> users;


public class HivebriteUserRecords
public String name;
public String email;
public List<HivebriteUserCustomAtt> custom_attributes;
public List<Integer> sub_network_ids;



public class HivebriteUserCustomAtt
public String id;
public String name;
public String value;










share|improve this question









New contributor




Anthony Mealand is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    up vote
    1
    down vote

    favorite
    1












    I am writing a callout in Apex with returns a JSON file with a field that does not have a consistent type. The JSON returned is quite complicated, so I have been trying to read the JSON with a Apex Class structure within salesforce, but this struggles with a varying field type. Is there any way to deal with this in Apex? The location of this varying field (in a fairly simple object list) makes me wonder if I could read only this list as a string so only have to use a nextToken etc for this section? simplified JSON and class structure below. value is the field that varies in type.



    `




    "users": [

    "id": 461774,
    "email": "test@test.co.uk",
    "name": "test Student",
    "sub_network_ids": [
    18307
    ],
    "custom_attributes": [

    "id": 2310,
    "name": "isReady",
    "value": true,
    "type": "CustomizableAttribute::Boolean"
    ,

    "id": 2323,
    "name": "othernames",
    "value": "something",
    "type": "CustomizableAttribute::Text"

    ]

    ]

    `


    Code:



    public class HivebriteUserList
    public List<HivebriteUserRecords> users;


    public class HivebriteUserRecords
    public String name;
    public String email;
    public List<HivebriteUserCustomAtt> custom_attributes;
    public List<Integer> sub_network_ids;



    public class HivebriteUserCustomAtt
    public String id;
    public String name;
    public String value;










    share|improve this question









    New contributor




    Anthony Mealand is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





















      up vote
      1
      down vote

      favorite
      1









      up vote
      1
      down vote

      favorite
      1






      1





      I am writing a callout in Apex with returns a JSON file with a field that does not have a consistent type. The JSON returned is quite complicated, so I have been trying to read the JSON with a Apex Class structure within salesforce, but this struggles with a varying field type. Is there any way to deal with this in Apex? The location of this varying field (in a fairly simple object list) makes me wonder if I could read only this list as a string so only have to use a nextToken etc for this section? simplified JSON and class structure below. value is the field that varies in type.



      `




      "users": [

      "id": 461774,
      "email": "test@test.co.uk",
      "name": "test Student",
      "sub_network_ids": [
      18307
      ],
      "custom_attributes": [

      "id": 2310,
      "name": "isReady",
      "value": true,
      "type": "CustomizableAttribute::Boolean"
      ,

      "id": 2323,
      "name": "othernames",
      "value": "something",
      "type": "CustomizableAttribute::Text"

      ]

      ]

      `


      Code:



      public class HivebriteUserList
      public List<HivebriteUserRecords> users;


      public class HivebriteUserRecords
      public String name;
      public String email;
      public List<HivebriteUserCustomAtt> custom_attributes;
      public List<Integer> sub_network_ids;



      public class HivebriteUserCustomAtt
      public String id;
      public String name;
      public String value;










      share|improve this question









      New contributor




      Anthony Mealand is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I am writing a callout in Apex with returns a JSON file with a field that does not have a consistent type. The JSON returned is quite complicated, so I have been trying to read the JSON with a Apex Class structure within salesforce, but this struggles with a varying field type. Is there any way to deal with this in Apex? The location of this varying field (in a fairly simple object list) makes me wonder if I could read only this list as a string so only have to use a nextToken etc for this section? simplified JSON and class structure below. value is the field that varies in type.



      `




      "users": [

      "id": 461774,
      "email": "test@test.co.uk",
      "name": "test Student",
      "sub_network_ids": [
      18307
      ],
      "custom_attributes": [

      "id": 2310,
      "name": "isReady",
      "value": true,
      "type": "CustomizableAttribute::Boolean"
      ,

      "id": 2323,
      "name": "othernames",
      "value": "something",
      "type": "CustomizableAttribute::Text"

      ]

      ]

      `


      Code:



      public class HivebriteUserList
      public List<HivebriteUserRecords> users;


      public class HivebriteUserRecords
      public String name;
      public String email;
      public List<HivebriteUserCustomAtt> custom_attributes;
      public List<Integer> sub_network_ids;



      public class HivebriteUserCustomAtt
      public String id;
      public String name;
      public String value;







      apex json callout parser






      share|improve this question









      New contributor




      Anthony Mealand is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Anthony Mealand is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 3 hours ago









      Pranay Jaiswal

      9,59231949




      9,59231949






      New contributor




      Anthony Mealand is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 3 hours ago









      Anthony Mealand

      61




      61




      New contributor




      Anthony Mealand is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Anthony Mealand is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Anthony Mealand is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          3
          down vote













          For this situation, I would use JSON.desertializeUntyped which returns nested maps (for objects) and lists (for arrays). That allows the platform code to deal with all JSON related issues (such as escaping and syntax checking) without you having to write any code (such as the hard to get right nextToken stuff).



          Your job is then to check what is in the maps -get using the names you expect - and lists and convert them to whatever data structure you want e.g. the classes you list. You are able to randomly walk about the model as you like rather than having to think about handling the nodes from first to last as you would with the JSONParser class.



          If the variation is only by the type of a field, then you can store the value in an Object type property in your data structure:



          public class HivebriteUserCustomAtt
          public String id;
          public String name;
          public Object value;



          and handle the various types elsewhere.



          Or if you want to look at the types straight after parsing use e.g.:



          Object value = m.get('value');
          if (value instanceOf String)
          String s = (String) value;
          ...
          else if (value instanceOf Boolean)
          Boolean b = (Boolean) value;
          ...
          ...





          share|improve this answer






















            Your Answer







            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "459"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );






            Anthony Mealand is a new contributor. Be nice, and check out our Code of Conduct.









             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f237155%2fhow-to-read-json-that-contains-field-that-can-have-different-types%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            3
            down vote













            For this situation, I would use JSON.desertializeUntyped which returns nested maps (for objects) and lists (for arrays). That allows the platform code to deal with all JSON related issues (such as escaping and syntax checking) without you having to write any code (such as the hard to get right nextToken stuff).



            Your job is then to check what is in the maps -get using the names you expect - and lists and convert them to whatever data structure you want e.g. the classes you list. You are able to randomly walk about the model as you like rather than having to think about handling the nodes from first to last as you would with the JSONParser class.



            If the variation is only by the type of a field, then you can store the value in an Object type property in your data structure:



            public class HivebriteUserCustomAtt
            public String id;
            public String name;
            public Object value;



            and handle the various types elsewhere.



            Or if you want to look at the types straight after parsing use e.g.:



            Object value = m.get('value');
            if (value instanceOf String)
            String s = (String) value;
            ...
            else if (value instanceOf Boolean)
            Boolean b = (Boolean) value;
            ...
            ...





            share|improve this answer


























              up vote
              3
              down vote













              For this situation, I would use JSON.desertializeUntyped which returns nested maps (for objects) and lists (for arrays). That allows the platform code to deal with all JSON related issues (such as escaping and syntax checking) without you having to write any code (such as the hard to get right nextToken stuff).



              Your job is then to check what is in the maps -get using the names you expect - and lists and convert them to whatever data structure you want e.g. the classes you list. You are able to randomly walk about the model as you like rather than having to think about handling the nodes from first to last as you would with the JSONParser class.



              If the variation is only by the type of a field, then you can store the value in an Object type property in your data structure:



              public class HivebriteUserCustomAtt
              public String id;
              public String name;
              public Object value;



              and handle the various types elsewhere.



              Or if you want to look at the types straight after parsing use e.g.:



              Object value = m.get('value');
              if (value instanceOf String)
              String s = (String) value;
              ...
              else if (value instanceOf Boolean)
              Boolean b = (Boolean) value;
              ...
              ...





              share|improve this answer
























                up vote
                3
                down vote










                up vote
                3
                down vote









                For this situation, I would use JSON.desertializeUntyped which returns nested maps (for objects) and lists (for arrays). That allows the platform code to deal with all JSON related issues (such as escaping and syntax checking) without you having to write any code (such as the hard to get right nextToken stuff).



                Your job is then to check what is in the maps -get using the names you expect - and lists and convert them to whatever data structure you want e.g. the classes you list. You are able to randomly walk about the model as you like rather than having to think about handling the nodes from first to last as you would with the JSONParser class.



                If the variation is only by the type of a field, then you can store the value in an Object type property in your data structure:



                public class HivebriteUserCustomAtt
                public String id;
                public String name;
                public Object value;



                and handle the various types elsewhere.



                Or if you want to look at the types straight after parsing use e.g.:



                Object value = m.get('value');
                if (value instanceOf String)
                String s = (String) value;
                ...
                else if (value instanceOf Boolean)
                Boolean b = (Boolean) value;
                ...
                ...





                share|improve this answer














                For this situation, I would use JSON.desertializeUntyped which returns nested maps (for objects) and lists (for arrays). That allows the platform code to deal with all JSON related issues (such as escaping and syntax checking) without you having to write any code (such as the hard to get right nextToken stuff).



                Your job is then to check what is in the maps -get using the names you expect - and lists and convert them to whatever data structure you want e.g. the classes you list. You are able to randomly walk about the model as you like rather than having to think about handling the nodes from first to last as you would with the JSONParser class.



                If the variation is only by the type of a field, then you can store the value in an Object type property in your data structure:



                public class HivebriteUserCustomAtt
                public String id;
                public String name;
                public Object value;



                and handle the various types elsewhere.



                Or if you want to look at the types straight after parsing use e.g.:



                Object value = m.get('value');
                if (value instanceOf String)
                String s = (String) value;
                ...
                else if (value instanceOf Boolean)
                Boolean b = (Boolean) value;
                ...
                ...






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 3 hours ago

























                answered 3 hours ago









                Keith C

                92k1087194




                92k1087194




















                    Anthony Mealand is a new contributor. Be nice, and check out our Code of Conduct.









                     

                    draft saved


                    draft discarded


















                    Anthony Mealand is a new contributor. Be nice, and check out our Code of Conduct.












                    Anthony Mealand is a new contributor. Be nice, and check out our Code of Conduct.











                    Anthony Mealand is a new contributor. Be nice, and check out our Code of Conduct.













                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f237155%2fhow-to-read-json-that-contains-field-that-can-have-different-types%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Comments

                    Popular posts from this blog

                    Long meetings (6-7 hours a day): Being “babysat” by supervisor

                    Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

                    Confectionery