[Swift5] How to analyze complex JSON and get the index of the element that satisfies the condition

Introduction

If you don't understand JSON analysis, please refer to the article here.

** [Swift5] Get array (individual) information from JSON using library'SwiftyJSON'** https://qiita.com/nkekisasa222/items/84c40bbfacfaca9f38a9

Check JSON data

joyCountArray.


[{
  "document_tone" : {
    "tones" : [
      {
        "tone_name" : "Joy",
        "score" : 0.53749899999999995,
        "tone_id" : "joy"
      }
    ]
  }
}, {
  "document_tone" : {
    "tones" : [

    ]
  },
  "sentences_tone" : [
    {
      "tones" : [

      ],
      "text" : "Kozai \"Aza\" Haruka: Haruka, Ie no Haruka: Haruka, Ie no Haruka, 'The body is too clean.'",
      "sentence_id" : 0
    },
    {
      "tones" : [

      ],
      "text" : "(Entame NEXT)",
      "sentence_id" : 1
    }
  ]
}, {
  "document_tone" : {
    "tones" : [
      {
        "tone_id" : "joy",
        "score" : 0.67608400000000002,
        "tone_name" : "Joy"
      },
      {
        "tone_id" : "analytical",
        "score" : 0.92818699999999998,
        "tone_name" : "Analytical"
      }
    ]
  }
}, {
  "document_tone" : {
    "tones" : [
      {
        "tone_name" : "Joy",
        "score" : 0.74235700000000004,
        "tone_id" : "joy"
      }
    ]
  },
  "sentences_tone" : [
    {
      "sentence_id" : 0,
      "tones" : [

      ],
      "text" : "Ariyoshi is also a lavatory?"
    },
    {
      "sentence_id" : 1,
      "tones" : [
        {
          "tone_name" : "Joy",
          "tone_id" : "joy",
          "score" : 0.75596300000000005
        }
      ],
      "text" : "Mihopa's \"overwhelmable beauty Body\" sexy water shoe shots are released (Entame NEXT)"
    }
  ]
}, {
  "sentences_tone" : [
    {
      "text" : "Yuzuno Mizusawa, a beautiful woman with a hairstyle of blue hair, is released from hair color to a single one-peoped kamiwanpi with a blue-leg.",
      "sentence_id" : 0,
      "tones" : [
        {
          "tone_id" : "joy",
          "tone_name" : "Joy",
          "score" : 0.76069399999999998
        }
      ]
    },
    {
      "sentence_id" : 1,
      "text" : "(Entame NEXT)",
      "tones" : [

      ]
    }
  ],
  "document_tone" : {
    "tones" : [
      {
        "tone_id" : "joy",
        "score" : 0.76504700000000003,
        "tone_name" : "Joy"
      }
    ]
  }
}, {
  "sentences_tone" : [
    {
      "text" : "Princess Inster of the Graviar world, Inster Saki Saki, said, \"I may not have felt any resistance to escaping from the beginning.\"",
      "sentence_id" : 0,
      "tones" : [
        {
          "tone_id" : "joy",
          "score" : 0.64593199999999995,
          "tone_name" : "Joy"
        },
        {
          "tone_id" : "tentative",
          "score" : 0.86333300000000002,
          "tone_name" : "Tentative"
        }
      ]
    },
    {
      "text" : "(Entame NEXT)",
      "sentence_id" : 1,
      "tones" : [

      ]
    }
  ],
  "document_tone" : {
    "tones" : [
      {
        "score" : 0.65832100000000005,
        "tone_name" : "Joy",
        "tone_id" : "joy"
      },
      {
        "score" : 0.98048800000000003,
        "tone_name" : "Tentative",
        "tone_id" : "tentative"
      }
    ]
  }
}, {
  "document_tone" : {
    "tones" : [
      {
        "score" : 0.70347899999999997,
        "tone_name" : "Joy",
        "tone_id" : "joy"
      }
    ]
  }
}, {
  "sentences_tone" : [
    {
      "sentence_id" : 0,
      "text" : "\"Shiratama\" in \"The Edge of the Bend\" and the dog dog \"Shiratama\" (The Dog Warrior), a dog in the Twin Till, the dog of the Twin Tin.",
      "tones" : [

      ]
    },
    {
      "sentence_id" : 1,
      "text" : "(Entame Next)",
      "tones" : [

      ]
    }
  ],
  "document_tone" : {
    "tones" : [

    ]
  }
}, {
  "document_tone" : {
    "tones" : [

    ]
  }
}, {
  "document_tone" : {
    "tones" : [
      {
        "tone_name" : "Analytical",
        "score" : 0.61642600000000003,
        "tone_id" : "analytical"
      }
    ]
  }
}, {
  "document_tone" : {
    "tones" : [
      {
        "score" : 0.58929500000000001,
        "tone_id" : "analytical",
        "tone_name" : "Analytical"
      }
    ]
  }
}, {
  "sentences_tone" : [
    {
      "text" : "Perfume, grag, grag, chalen, grag, grag!",
      "sentence_id" : 0,
      "tones" : [

      ]
    },
    {
      "text" : "The two shots that were too beautiful were the subject of the buzzo (Entame Next).",
      "sentence_id" : 1,
      "tones" : [
        {
          "tone_name" : "Joy",
          "score" : 0.80030299999999999,
          "tone_id" : "joy"
        }
      ]
    }
  ],
  "document_tone" : {
    "tones" : [
      {
        "tone_name" : "Joy",
        "score" : 0.79371400000000003,
        "tone_id" : "joy"
      }
    ]
  }
}, {
  "document_tone" : {
    "tones" : [
      {
        "tone_id" : "joy",
        "score" : 0.63750700000000005,
        "tone_name" : "Joy"
      }
    ]
  }
}, {
  "document_tone" : {
    "tones" : [
      {
        "tone_name" : "Joy",
        "score" : 0.83795299999999995,
        "tone_id" : "joy"
      }
    ]
  }
}, {
  "sentences_tone" : [
    {
      "sentence_id" : 0,
      "tones" : [
        {
          "tone_name" : "Joy",
          "tone_id" : "joy",
          "score" : 0.67072399999999999
        }
      ],
      "text" : "\"Bomb Girl,\" \"Bomb Girl,\" with a limited number of New Hare Acaler in the public, \"It's getting more and more cute.\""
    },
    {
      "sentence_id" : 1,
      "tones" : [

      ],
      "text" : "(Entame NEXT)"
    }
  ],
  "document_tone" : {
    "tones" : [
      {
        "score" : 0.69078499999999998,
        "tone_id" : "joy",
        "tone_name" : "Joy"
      }
    ]
  }
}, {
  "document_tone" : {
    "tones" : [

    ]
  }
}, {
  "sentences_tone" : [
    {
      "sentence_id" : 0,
      "tones" : [
        {
          "tone_id" : "tentative",
          "tone_name" : "Tentative",
          "score" : 0.71630099999999997
        }
      ],
      "text" : "\"I'm happy just looking at the scene of the popular Cossprayer ShenInin uniform.\""
    },
    {
      "sentence_id" : 1,
      "tones" : [

      ],
      "text" : "(Entame NEXT)"
    }
  ],
  "document_tone" : {
    "tones" : [
      {
        "score" : 0.94484500000000005,
        "tone_name" : "Tentative",
        "tone_id" : "tentative"
      }
    ]
  }
}, {
  "document_tone" : {
    "tones" : [
      {
        "score" : 0.77552100000000002,
        "tone_name" : "Joy",
        "tone_id" : "joy"
      }
    ]
  },
  "sentences_tone" : [
    {
      "sentence_id" : 0,
      "tones" : [
        {
          "tone_id" : "joy",
          "score" : 0.62795699999999999,
          "tone_name" : "Joy"
        }
      ],
      "text" : "Followers: 32.4 million!"
    },
    {
      "sentence_id" : 1,
      "tones" : [
        {
          "tone_id" : "joy",
          "tone_name" : "Joy",
          "score" : 0.75022100000000003
        }
      ],
      "text" : "The Queen of the Gravier and the Queen of the Gravies of the Gravies and the Queen of the Sainting and the Mine Tree"
    }
  ]
}, {
  "document_tone" : {
    "tones" : [
      {
        "score" : 0.83205300000000004,
        "tone_id" : "joy",
        "tone_name" : "Joy"
      }
    ]
  }
}, {
  "document_tone" : {
    "tones" : [
      {
        "tone_name" : "Joy",
        "tone_id" : "joy",
        "score" : 0.53014799999999995
      }
    ]
  }
}, {
  "document_tone" : {
    "tones" : [

    ]
  }
}, {
  "document_tone" : {
    "tones" : [

    ]
  }
}, {
  "sentences_tone" : [
    {
      "text" : "\"The thighs are thicker.\"",
      "tones" : [

      ],
      "sentence_id" : 0
    },
    {
      "text" : "Yuho HONDA, Cute and Sexual Dresses (Entame NEXT)",
      "tones" : [
        {
          "tone_id" : "joy",
          "score" : 0.75571100000000002,
          "tone_name" : "Joy"
        }
      ],
      "sentence_id" : 1
    }
  ],
  "document_tone" : {
    "tones" : [
      {
        "tone_id" : "joy",
        "score" : 0.73423000000000005,
        "tone_name" : "Joy"
      }
    ]
  }
}, {
  "document_tone" : {
    "tones" : [
      {
        "tone_id" : "joy",
        "score" : 0.67583000000000004,
        "tone_name" : "Joy"
      }
    ]
  },
  "sentences_tone" : [
    {
      "text" : "\"Detective Conan\" with voice actors and sumptues, \"Detective Conan,\" which is too much of a love for \"Detective Conan,\" and \"Sumitai Edogawa.\"",
      "tones" : [
        {
          "tone_id" : "joy",
          "tone_name" : "Joy",
          "score" : 0.70681300000000002
        }
      ],
      "sentence_id" : 0
    },
    {
      "text" : "(\"Entame NEXT\")",
      "tones" : [

      ],
      "sentence_id" : 1
    }
  ]
}, {
  "document_tone" : {
    "tones" : [
      {
        "tone_name" : "Analytical",
        "tone_id" : "analytical",
        "score" : 0.84622399999999998
      },
      {
        "tone_name" : "Tentative",
        "tone_id" : "tentative",
        "score" : 0.61621599999999999
      }
    ]
  },
  "sentences_tone" : [
    {
      "text" : "\"I don't feel comfortable with the Dokidoki\" (Entame NEXT), which is also known as \"Suta Akari,\" and the girl's favorite helcey.",
      "tones" : [
        {
          "score" : 0.58152999999999999,
          "tone_name" : "Joy",
          "tone_id" : "joy"
        }
      ],
      "sentence_id" : 0
    },
    {
      "text" : "\" I don't feel like a dog",
      "tones" : [
        {
          "tone_name" : "Analytical",
          "score" : 0.94455100000000003,
          "tone_id" : "analytical"
        }
      ],
      "sentence_id" : 1
    }
  ]
}, {
  "document_tone" : {
    "tones" : [
      {
        "tone_name" : "Analytical",
        "score" : 0.82533800000000002,
        "tone_id" : "analytical"
      }
    ]
  }
}, {
  "document_tone" : {
    "tones" : [

    ]
  }
}, {
  "document_tone" : {
    "tones" : [

    ]
  }
}, {
  "sentences_tone" : [
    {
      "sentence_id" : 0,
      "text" : "Mo Musume.Makino Mari, a swimsuit, and a sneaker's legendable offshot: \"You really are a goddess.\"",
      "tones" : [
        {
          "tone_name" : "Analytical",
          "score" : 0.50676299999999996,
          "tone_id" : "analytical"
        }
      ]
    },
    {
      "sentence_id" : 1,
      "tones" : [

      ],
      "text" : "(Entame NEXT)"
    }
  ],
  "document_tone" : {
    "tones" : [
      {
        "tone_name" : "Analytical",
        "tone_id" : "analytical",
        "score" : 0.64375800000000005
      }
    ]
  }
}, {
  "document_tone" : {
    "tones" : [
      {
        "score" : 0.66994399999999998,
        "tone_name" : "Joy",
        "tone_id" : "joy"
      }
    ]
  }
}, {
  "document_tone" : {
    "tones" : [
      {
        "tone_name" : "Joy",
        "score" : 0.91817700000000002,
        "tone_id" : "joy"
      }
    ]
  }
}, {
  "document_tone" : {
    "tones" : [
      {
        "score" : 0.61852099999999999,
        "tone_name" : "Joy",
        "tone_id" : "joy"
      }
    ]
  }
}, {
  "document_tone" : {
    "tones" : [
      {
        "tone_id" : "joy",
        "tone_name" : "Joy",
        "score" : 0.50842900000000002
      }
    ]
  },
  "sentences_tone" : [
    {
      "text" : "Queen Inster in the Graviar world, Inster-Sakuchimi, said: \"Anyway, I'm aware of this by shooting.\"",
      "tones" : [

      ],
      "sentence_id" : 0
    },
    {
      "text" : "(\"Entame Next\")",
      "tones" : [

      ],
      "sentence_id" : 1
    }
  ]
}, {
  "document_tone" : {
    "tones" : [
      {
        "tone_id" : "sadness",
        "score" : 0.503888,
        "tone_name" : "Sadness"
      },
      {
        "tone_id" : "analytical",
        "score" : 0.57936699999999997,
        "tone_name" : "Analytical"
      }
    ]
  }
}, {
  "document_tone" : {
    "tones" : [
      {
        "tone_id" : "joy",
        "tone_name" : "Joy",
        "score" : 0.84084999999999999
      }
    ]
  }
}]

Suppose that JSON format data exists like this, and you want to get the index number of an element that satisfies a certain condition. The conditions specified this time are that tone_name is" Joy "and" score "is 0.5 or more.

Put the condition into the code

JsonAnalysisModel.swift


class JsonAnalysisModel {
    
    //Properties used in JSON parsing
    var count      = 0
    var arrayCount = 35
    var sampleJsonArray: [JSON] = []
    var joyCountArray  : [Any]  = []
    
    #abridgement
    
    //Method creation
    func startJsonAnalysis() {
        
        //Check JSON data (#Data displayed by checking JSON data)
        print(sampleJsonArray.debugDescription)
        
        //Iterate as many times as there are elements in sampleJsonArray (-Consistency at 1)
        for i in 0...arrayCount-1 {
            
            //JSON data"Joy" &&score is 0.Narrow down the conditions with 5 or more elements(Search for elements here)
            if sampleJsonArray[i]["document_tone"]["tones"][count]["score"] > 0.5 && sampleJsonArray[i]["document_tone"]["tones"][count]["tone_name"] == "Joy" {
                
                //Get the index number of the element that meets the conditions (-Consistency at 1)
                joyCountArray.append(sampleJsonArray[0].count+i-1)
            }
        }
        //Confirmation of acquisition result
        print("joyCountArray: \(joyCountArray)")
    }
}

Let's check the log.

joyCountArray: [0, 2, 3, 4, 5, 6, 11, 12, 13, 14, 17, 18, 19, 22, 23, 29, 30, 31, 32, 34]

Now you have ** analyzed JSON and got the index of the element that satisfies the condition **!

Finally

This time, I posted about ** How to analyze complex JSON and get the index of the element that satisfies the condition ** by applying JSON analysis. Since Swift performs JSON analysis frequently, it must be able to handle various conditions.

Thank you for watching until the end!

Recommended Posts

[Swift5] How to analyze complex JSON and get the index of the element that satisfies the condition
[Swift5] How to get an array and the complement of arrays
[Swift] How to get the document ID of Firebase
[Rails] How to get the URL of the transition source and redirect
[Swift UI] How to get the startup status of the application [iOS]
How to get today's day of the week
[Java] How to get the authority of the folder
How to get the ID of a user authenticated with Firebase in Swift
[Java] How to get the URL of the transition source
[Java] How to get the maximum value of HashMap
[Android] How to get the setting language of the terminal
[Rails] How to get the contents of strong parameters
[Ruby] How to get the tens place and the ones place
[Swift] Use Swity JSON to easily get information from JSON data of the fortune-telling API
How to get the longest information from Twitter as of 12/12/2016
How to check the extension and size of uploaded files
[jsoup] How to get the full amount of a document
[Swift5] How to create a .gitignore file and the code that should be written by default
[Swift] How to dynamically change the height of the toolbar on the keyboard
How to set the IP address and host name of CentOS8
How to get the contents of Map using for statement Memorandum
Method to add the number of years and get the end of the month
How to get the id of PRIMAY KEY auto_incremented in MyBatis
[Java] How to convert from String to Path type and get the path
How to get the length of an audio file in java
This and that of the JDK
How to operate IGV using socket communication, and the story of making a Ruby Gem using that method
[Java] How to get the current date and time and specify the display format
Get the type of an array element to determine if it is an array
How to change the maximum and maximum number of POST data in Spark
[Java] How to get to the front of a specific string using the String class
How to get the absolute path of a directory running in Java
[Java improvement case] How to reach the limit of self-study and beyond
Android development, how to check null in the value of JSON object
[swift5] How to change the color of TabBar or the color of item of TabBar with code
How to create your own annotation in Java and get the value
This and that of Swift corner Radius
How to determine the number of parallels
[Java] How to get the current directory
[Swift] How to implement the countdown function
[Swift] Get the height of Safe Area
How to sort the List of SelectItem
How to find the tens and ones
How to get the date in java
[swift] How to control the behavior when the back button of NavigationBar is pressed
[Swift] How to change the order of Bar Items in Tab Bar Controller [Beginner]
Differences between Java, C # and JavaScript (how to determine the degree of obesity)
[Java] How to easily get the longest character string of ArrayList using stream
How to get the date from the JavaScript Date type that C # developers are addicted to
Exception that occurs when RestTemplate fails to map JSON and class of response
I want to recursively get the superclass and interface of a certain class
How to get only articles and transactions that you commented or messaged [includes]
[Ruby] How to use the map method. How to process the value of an object and get it by hash or symbol.
JDBC promises and examples of how to write
How to find the cause of the Ruby error
Customize how to divide the contents of Recyclerview
[Java] How to get and output standard input
[Swift] How to implement the LINE login function
[swift5] How to implement the Twitter share function
How to add sound in the app (swift)
How to get Class from Element in Java