Getting first element of a JSON Array with Alamofire/SwiftyJSON












2















I have an Alamofire request, which gives me JSON as a result. Here is my JSON:



"user" : {
"field_first_name" : {
"und" : [
{
"value" : "Christopher",
"format" : null,
"safe_value" : "Christopher"
}
]
},


The "und" value is an array, which usually only has one value in it. I'd like to grab the first element of this array at all times, as occasionally this field will have multiple elements, but the first is always the correct result.



The code below works, until I get to the ".first". This returns a value of type (String, JSON) and I cannot figure out how to get this item "firstValue" back into usable format. Whenever I try to use



Alamofire.request(url, method: .post, parameters:parameters, headers: headers)
.responseJSON { response in

if let data = response.result.value {
let json = JSON(json: data)


let firstValue = json["user"]["field_first_name"]["und"].first
print(firstValue!)
}
}


firstValue then becomes of type (String, JSON) and I have no idea how to handle this. What I'd like to get is the value for key "value" out of "firstValue.



This is what "firstValue" prints out:



("0", {
"safe_value" : "Christopher",
"value" : "Christopher",
"format" : null
})


Is this approach salvageable? I could do a for in loop, get the first result, and tell it to ignore the rest, but that doesn't seem right. Thanks for any help.










share|improve this question























  • Try this answer, instead of using first use array and then first and let me know if it works

    – carlos salzar
    Jun 9 '17 at 16:19













  • I found a solution to this: firstValue is a tuple, of type (String, JSON), and I only need the second part of the tuple. So, if I do print(firstValue.1["value"]) it works

    – cdeangelus
    Jun 9 '17 at 18:01


















2















I have an Alamofire request, which gives me JSON as a result. Here is my JSON:



"user" : {
"field_first_name" : {
"und" : [
{
"value" : "Christopher",
"format" : null,
"safe_value" : "Christopher"
}
]
},


The "und" value is an array, which usually only has one value in it. I'd like to grab the first element of this array at all times, as occasionally this field will have multiple elements, but the first is always the correct result.



The code below works, until I get to the ".first". This returns a value of type (String, JSON) and I cannot figure out how to get this item "firstValue" back into usable format. Whenever I try to use



Alamofire.request(url, method: .post, parameters:parameters, headers: headers)
.responseJSON { response in

if let data = response.result.value {
let json = JSON(json: data)


let firstValue = json["user"]["field_first_name"]["und"].first
print(firstValue!)
}
}


firstValue then becomes of type (String, JSON) and I have no idea how to handle this. What I'd like to get is the value for key "value" out of "firstValue.



This is what "firstValue" prints out:



("0", {
"safe_value" : "Christopher",
"value" : "Christopher",
"format" : null
})


Is this approach salvageable? I could do a for in loop, get the first result, and tell it to ignore the rest, but that doesn't seem right. Thanks for any help.










share|improve this question























  • Try this answer, instead of using first use array and then first and let me know if it works

    – carlos salzar
    Jun 9 '17 at 16:19













  • I found a solution to this: firstValue is a tuple, of type (String, JSON), and I only need the second part of the tuple. So, if I do print(firstValue.1["value"]) it works

    – cdeangelus
    Jun 9 '17 at 18:01
















2












2








2








I have an Alamofire request, which gives me JSON as a result. Here is my JSON:



"user" : {
"field_first_name" : {
"und" : [
{
"value" : "Christopher",
"format" : null,
"safe_value" : "Christopher"
}
]
},


The "und" value is an array, which usually only has one value in it. I'd like to grab the first element of this array at all times, as occasionally this field will have multiple elements, but the first is always the correct result.



The code below works, until I get to the ".first". This returns a value of type (String, JSON) and I cannot figure out how to get this item "firstValue" back into usable format. Whenever I try to use



Alamofire.request(url, method: .post, parameters:parameters, headers: headers)
.responseJSON { response in

if let data = response.result.value {
let json = JSON(json: data)


let firstValue = json["user"]["field_first_name"]["und"].first
print(firstValue!)
}
}


firstValue then becomes of type (String, JSON) and I have no idea how to handle this. What I'd like to get is the value for key "value" out of "firstValue.



This is what "firstValue" prints out:



("0", {
"safe_value" : "Christopher",
"value" : "Christopher",
"format" : null
})


Is this approach salvageable? I could do a for in loop, get the first result, and tell it to ignore the rest, but that doesn't seem right. Thanks for any help.










share|improve this question














I have an Alamofire request, which gives me JSON as a result. Here is my JSON:



"user" : {
"field_first_name" : {
"und" : [
{
"value" : "Christopher",
"format" : null,
"safe_value" : "Christopher"
}
]
},


The "und" value is an array, which usually only has one value in it. I'd like to grab the first element of this array at all times, as occasionally this field will have multiple elements, but the first is always the correct result.



The code below works, until I get to the ".first". This returns a value of type (String, JSON) and I cannot figure out how to get this item "firstValue" back into usable format. Whenever I try to use



Alamofire.request(url, method: .post, parameters:parameters, headers: headers)
.responseJSON { response in

if let data = response.result.value {
let json = JSON(json: data)


let firstValue = json["user"]["field_first_name"]["und"].first
print(firstValue!)
}
}


firstValue then becomes of type (String, JSON) and I have no idea how to handle this. What I'd like to get is the value for key "value" out of "firstValue.



This is what "firstValue" prints out:



("0", {
"safe_value" : "Christopher",
"value" : "Christopher",
"format" : null
})


Is this approach salvageable? I could do a for in loop, get the first result, and tell it to ignore the rest, but that doesn't seem right. Thanks for any help.







json swift alamofire swifty-json






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jun 9 '17 at 14:29









cdeangeluscdeangelus

9114




9114













  • Try this answer, instead of using first use array and then first and let me know if it works

    – carlos salzar
    Jun 9 '17 at 16:19













  • I found a solution to this: firstValue is a tuple, of type (String, JSON), and I only need the second part of the tuple. So, if I do print(firstValue.1["value"]) it works

    – cdeangelus
    Jun 9 '17 at 18:01





















  • Try this answer, instead of using first use array and then first and let me know if it works

    – carlos salzar
    Jun 9 '17 at 16:19













  • I found a solution to this: firstValue is a tuple, of type (String, JSON), and I only need the second part of the tuple. So, if I do print(firstValue.1["value"]) it works

    – cdeangelus
    Jun 9 '17 at 18:01



















Try this answer, instead of using first use array and then first and let me know if it works

– carlos salzar
Jun 9 '17 at 16:19







Try this answer, instead of using first use array and then first and let me know if it works

– carlos salzar
Jun 9 '17 at 16:19















I found a solution to this: firstValue is a tuple, of type (String, JSON), and I only need the second part of the tuple. So, if I do print(firstValue.1["value"]) it works

– cdeangelus
Jun 9 '17 at 18:01







I found a solution to this: firstValue is a tuple, of type (String, JSON), and I only need the second part of the tuple. So, if I do print(firstValue.1["value"]) it works

– cdeangelus
Jun 9 '17 at 18:01














2 Answers
2






active

oldest

votes


















0














I know this is over a year late, but this should work with your particular data. It's quite a long line of code though. I would break it up to make it more readable.



if let firstValue = json["user"]["field_first_name"] ["und"].arrayValue[0]["value"].string {
print(firstValue)
{





share|improve this answer































    0














    I believe you should be able to do something like this



       if let firstValue = json["user"]["field_first_name"]["und"].first?.1 {
    print(firstValue)
    }





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      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',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f44460417%2fgetting-first-element-of-a-json-array-with-alamofire-swiftyjson%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      I know this is over a year late, but this should work with your particular data. It's quite a long line of code though. I would break it up to make it more readable.



      if let firstValue = json["user"]["field_first_name"] ["und"].arrayValue[0]["value"].string {
      print(firstValue)
      {





      share|improve this answer




























        0














        I know this is over a year late, but this should work with your particular data. It's quite a long line of code though. I would break it up to make it more readable.



        if let firstValue = json["user"]["field_first_name"] ["und"].arrayValue[0]["value"].string {
        print(firstValue)
        {





        share|improve this answer


























          0












          0








          0







          I know this is over a year late, but this should work with your particular data. It's quite a long line of code though. I would break it up to make it more readable.



          if let firstValue = json["user"]["field_first_name"] ["und"].arrayValue[0]["value"].string {
          print(firstValue)
          {





          share|improve this answer













          I know this is over a year late, but this should work with your particular data. It's quite a long line of code though. I would break it up to make it more readable.



          if let firstValue = json["user"]["field_first_name"] ["und"].arrayValue[0]["value"].string {
          print(firstValue)
          {






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Oct 2 '18 at 16:26









          skymookskymook

          1,7962028




          1,7962028

























              0














              I believe you should be able to do something like this



                 if let firstValue = json["user"]["field_first_name"]["und"].first?.1 {
              print(firstValue)
              }





              share|improve this answer




























                0














                I believe you should be able to do something like this



                   if let firstValue = json["user"]["field_first_name"]["und"].first?.1 {
                print(firstValue)
                }





                share|improve this answer


























                  0












                  0








                  0







                  I believe you should be able to do something like this



                     if let firstValue = json["user"]["field_first_name"]["und"].first?.1 {
                  print(firstValue)
                  }





                  share|improve this answer













                  I believe you should be able to do something like this



                     if let firstValue = json["user"]["field_first_name"]["und"].first?.1 {
                  print(firstValue)
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 1 at 21:58









                  BrianBrian

                  858




                  858






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f44460417%2fgetting-first-element-of-a-json-array-with-alamofire-swiftyjson%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      MongoDB - Not Authorized To Execute Command

                      in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

                      How to fix TextFormField cause rebuild widget in Flutter