50 Amazing & Cool Photoshop Action Tutorials

With Photoshop actions, you can create cool photo effects with a single click. You can use them to apply fun filters, add color effects, or even turn a photo into art. 

So if you know how to create actions, you have a powerful tool in your hands! In this article, I’ll show you a collection of our Photoshop action tutorials for photo and text effects.

And if you’ve never heard of Photoshop actions or are not sure how to use them, you can use these tutorials as an intro:

Painting and Drawing Photoshop Actions

Some photos would look great as a painting or a drawing, but you don’t have to be an artist to achieve such an effect— you can use special actions to turn photos into paintings! You can choose from watercolors, oil, drawing, or digital painting effects. In these tutorials, you’ll learn how to create such effects and how to use them as actions.

Photo Effect Photoshop Actions

If you have a good photo, you can make it even better by adding a special effect to it. You can change its colors, add some magical lighting, and play with shadows. There are so many things you can do to enhance a photo without making it obviously manipulated! And in these tutorials you’ll learn how to add such effects.

Cool Photo Effect Photoshop Actions

But maybe you don’t care about being subtle—maybe you want to add some magic to your photos. In these tutorials, you’ll learn how to add cool photo effects that turn your photos into something completely new—works of art on their own. Give new life to your photos with these fun photo filters!

Text Effect Photoshop Actions

Actions can be applied to text as well. You can change colors, shape, and even add special materials to the letters, making them much more appealing than ordinary text. In these tutorials, you’ll learn how to add such special effects to your text in Photoshop with actions.

Typography Photo Effect Photoshop Actions

A picture is worth a thousand words, but words can make a picture, too! In these tutorials, you’ll learn how to use text to enhance photos and transform them into eye-catching artworks.

Animated Photo Effect Photoshop Actions

If you want to add some life to a photo, you can create an animation out of it. And did you know that it’s possible to animate photos with Photoshop actions, too? In these tutorials, you’ll learn how to do it!

Conclusion

Photoshop actions are a real life-saver if you need to apply a cool photo effect quickly. But before you can use such an action, you must create it. But did you know that many artists publish their Photoshop actions on Envato Elements, ready to download and use? Whenever you need cool Photoshop portrait actions, photo effects, or magical text effects, take a look at the gallery and see if they have what you need!

photo effects photoshop actions

{excerpt}
Read More

How to Make a 3D Text Effect in Photoshop

Final product image
What You’ll Be Creating

If you’re interested in Photoshop text effects, in this tutorial I’ll show you how to create a 3D text in Photoshop in just a few steps. We’re going to adjust its shape, set the light sources, create a material, and then render it all. Just keep in mind you need Photoshop CC or CS6 Extended for this.

Follow along with us over on our Envato Tuts+ YouTube Channel:

How to Make a 3D Text Effect in Photoshop

Step 1

Create a New File. Type some text in a big, bold font. If you like, you can download the one I used, Streetwear.

type text in photoshop

Step 2

With the text layer selected, go to 3D > New 3D Extrusion From Selected Layer.

turn text into 3d in photoshop

Step 3

Your text will be turned into a 3D object with some default settings. Before we start changing them, let’s set the scene first. Click the 3D text and go to the Coordinates tab in the Properties panel. Change the X angle to 90 degrees.

rotate 3d text down

Step 4

Select the first tool in the upper bar, and click somewhere outside of the object to move the camera. Drag it until you see a red-blue cross.

rotate 3d camera in photoshop

Step 5

Go to the Coordinates tab and adjust the second column to only include a 90-degree rotation on the X axis.

change camera coordinates

Step 6

If needed, drag the cross more to the center and place the 3D text in the center, too.

center 3d camera

Step 7

Let’s add a background that our object will be interacting with. Go back to the layers and draw a rectangular shape.

add 3d background

Step 8

Go to 3D > New Mesh from Layer > Depth Map to > Plane.

turn rectangle into 3d in photoshop

Step 9

Select both layers and go to 3D > Merge 3D Layers.

merge 3d layers

Step 10

Select the last tool in the upper bar and resize the rectangular shape to fill the whole scene. You can adjust the thickness of the letters by dragging the shape up by its Z axis (the blue arrow)

move in z axis

Step 11

Let’s adjust the setting of the 3D text now. Go to the first tab in the Properties panel and click Shape Presets. Select one from the list, keeping in mind that they can all still be adjusted.

change shape preset

Step 12

To adjust the shape, go to the Cap tab. Here you have access to many settings to achieve the result you need. I decided to use a simple contour with a very thin bevel and a slight indentation.

adjust shape preset options

Step 13

Time to set the lights! Scroll down the list to find Infinite Light. Drag it to cast a nice shadow under the letters.

adjust infinite light

Step 14

You can change the Softness of the shadow and the Intensity of the light in the Properties panel.

change sfotness of shadow

Step 15

Let’s add one more light. Click the white card on the bottom of the 3D panel and select New Infinite Light.

add new light

Step 16

Turn off the first light for a moment and drag the new light so as to illuminate mostly the contours of the letters. Keep the shadow
soft, too.

adjust new light

Step 17

Go to the Materials tab now. Select all the materials of the text and add some colors to them. Diffuse is the main color of the object. Specular is the color of the highlights and shine. Ambient is the color of the shadow. Keep these colors in the same hue range for a natural effect.

add diffuse color
add specular color
add ambient color

Step 18

Once you’ve set all the colors, create a New Material.

create new material

Step 19

Click the Rectangle on the list and add the material to it.

add material to object

Step 20

If you want to make the shadows brighter, you can also add some ambient light to the scene. Find Environment on the 3D list and change Global Ambient to a bright color. You can increase the intensity of the ambient light, too.

change global light

Step 21

When you’re done, click Render to see how your scene really looks. It can take a while to finish rendering, but even a few passes can show you what to expect.

render 3d in photoshop

Step 22

If you noticed something you want to fix, just hit Escape to stop rendering and adjust the settings that you want to change, like the intensity of light, the direction of the light sources, or the properties of the material. Feel free to repeat this until you’re completely happy with the end result.

3d text effect photoshop

3D Text Effects

Cinematic 3D Text Effects

So now you know how to create a basic 3D text effect, but it still takes a lot of effort to create something more complex. If you want to save time, there are plenty of ready-made 3D text effects available on Envato Elements. 

For example, this pack contains eight text effects based on popular movie titles. Just pick the one that you want to edit, type your own text, and see the magic happen.

Cinematic 3D Text Effects
Cinematic 3D Text Effects
3d metal text effect

Real 3D Text Mockups

If you want something more original, this set contains six simple but very effective text effects. You can easily add realistic materials to your letters, and the background will complement the text nicely. You can create text for a flyer or a poster this way—just add your text, change the font, and enjoy the end result.

Real 3D Text Mockups
Real 3D Text Mockups
wooden text effect

3D Gold Text Effects

Was that too simple? Maybe you want something more ornate? This set offers eight text effects that will turn your letters into gold. The texture, the shine, and the details of the shape will all be adjusted to create a realistic effect of golden metal. Just think how much time it would take to create such an effect on your own!

3D Gold Text Effects
3D Gold Text Effects
golden text effect

Rubber 3D Text Effects

You can also create a unique double-3D text with this set. It offers three variations of the same effect: nice, softly shaded letters that bring rubber to mind. You can change the text, font, and even color in this effect.

Rubber 3D Text Effects
Rubber 3D Text Effects
rubber logo text effect

Good Job!

Now you know how to create 3D text in Photoshop. If you want to learn more about creating 3D text effects in many styles, try these tutorials:

{excerpt}
Read More

How to Make a Pamphlet Template in InDesign

Final product image
What You’ll Be Creating

Looking to impress your customers with your next marketing campaign? In this tutorial, we’ll create an eye-catching and professional brochure template that is easy to customize.

Brochures are a staple marketing tool—they get the job done. Visually appealing, versatile and easy to edit: that’s what many look for in a pamphlet template. In this tutorial, we’ll show you how to cover all of those in our brochure design. We will touch on key tools that will make it easier for you or your clients to edit a template. If you dabble in InDesign, this tutorial is easy to follow. 

In a hurry? We’ve got amazing pamphlet templates and InDesign brochure templates over at Envato Elements and GraphicRiver. Go check them out! 

What You Will Need

You’ll need access to Adobe InDesign and Adobe Illustrator—if you don’t have the software, you can download a trial from the Adobe website. You’ll also need the following assets:

Download the assets and make sure the font is installed on your system before starting. When you are ready, we can dive in!

1. Setting Up a Trifold Brochure InDesign File

Step 1

In InDesign, go to File > New. Name the document Pamphlet Template. We will create a DL trifold brochure template. Set the file to the following dimensions:

  • Width to 98 mm
  • Height to 210 mm 
  • Orientation to Portrait
  • Units to Millimetres
  • Uncheck Facing Pages
  • Margins to 10 mm 
  • Bleeds to 3 mm (it’s best to seek your professional printer’s preference)

Click Create.

Set up a DL trifold brochure file

Step 2

A trifold brochure has a panel that folds in, and this panel is shorter by 1 mm. We will create a second Master Page with this new size. 

Head over to the Pages panel. If you don’t have it active, head over to Windows > Pages. The top portion of the panel contains the Master Pages. Right-click on A-Master > New Master. On the New Master window, make sure Based on Master is set to [Master]. Set the Width to 97 mm. Click OK.

Create a new Master page to fit the fold in the brochure

Step 3

On the Pages panel, right-click on the B-Master we just created and select Apply Master. In the Apply Master window, under To Pages, enter 1, 6 (this is to specify that we are changing Pages 1 and 6). Click OK.

In the Master Page Size Conflict window, click Use Master Page Size.

Apply Master pages to pages 1 and 6

Step 4

Now that we have the page sizes set up, we can move the pages to set up the spreads. Pages 1-3 will be the outside spread, and Pages 4-6 will be the inside spreads. 

On the Main Menu of the Pages panel, make sure that Allow Document Pages to Shuffle is unchecked. Drag Page 2 towards Page 1 to combine them. Combine Pages 1-3 as one spread and Pages 4-6 as a second spread. Make sure that you have the BAA and AAB Master Pages configuration, as shown in the image below.

To exit the Master Pages mode, double-click on the regular pages thumbnail.

combine pages 1-3 and 4-6 to create separate spreads

Step 5

For this trifold brochure template tutorial, we will work with three layers. This will make it easier to update the template when it needs new information. 

Bring up the Layers panel by going to Window > Layers. Double-click on Layer 1 and rename it Vectors. In the Layers panel main menu, select New Layer. Name it Images. Click OK.

Create a new Layer named Text. Click OK.

Add layers names vectors photos and text

Step 6

Adding color swatches will make it easier to change up the colors to adjust to brand guidelines. Head over to Window > Color > Swatches to expand the Swatches panel. Choose New Color Swatch button from the main menu. Add the following three colors:

  • Purple: C=80 M=100 Y=0 K=15 
  • Yellow: C=0 M=25 Y=90 K=0
  • Orange: C=10 M=80 Y=90 K=0

Click Add and OK after you input each color value. 

Add color swatches

Step 7

Open up the Paragraph Styles panel by going to Windows > Styles > Paragraph Styles. From the main menu, select New Paragraph Style. 

In the New Paragraph Style option window, set the Style Name to Title. Select Basic Character Formats from the left side menu and use the following settings:

  • Font Family: Bw Stretch
  • Font Style: Heavy
  • Size: 24 pt
Create a new paragraph style

Perfect! Now let’s create a few more styles, and then we will be ready to format text boxes. Use the following settings for Copy:

  • Font Family: Addington CF
  • Font Style: Regular
  • Size: 10 pt
  • Use the following settings for Pull-quote:
  • Font Family: Bw Stretch
  • Font Style: Heavy
  • Size: 30 pt
  • Tracking: -15
Add stles for pull-quote copy and title

Perfect! Now you are all set to start the design. Remember that the steps above are key when you want to create a template. It will be easier for your clients or you to edit the brochure in the future with new colors and fonts.

2. How to Create Vectors in Your Pamphlet Design

Step 1

On the Layers panel, select the Vector layer. Using the Rectangle Tool (M) to draw a rectangle on Page 2 of the pamphlet. 

On the Swatches panel, set the color to orange.

Cover page 2 with a rectangle and set the color to orange

Step 2

Using the Rectangle Tool (M), draw two rectangles on Page 3. It is not necessary for the page to be divided exactly in half—we can come back to this later. 

On the Swatches panel, set the top object to yellow and the bottom object to purple.

Cover page 3 with two rectangles that fit on the top and bottom of the panel Set the color to yellow and purple

Step 3

Draw two rectangles that cover the whole panel on Page 4. Using the Swatches panel, color the top object purple and the bottom object yellow. 

Using the Direct Selection Tool (A), select the top right corner of the bottom object. Using the Up Arrow key on your keyboard, move the point upwards until you’ve created a steep slope. It doesn’t have to be the same measurements as the image below—you can add your own spin to it. 

Create two rectangles on page 4 Create an angle on one of them by using the direct selection tool

Step 4

Repeat the same action on Page 6. This time, the slope should be on the opposite side and at a different height. I’ve set the color of the top object to orange and the bottom object to purple.

Repeat the same action on page 6

Perfect! Let’s lock these by going to the Layers panel and locking the Vectors layer.

Lock the vectors layer

3. How to Add Images to Your Pamphlet Design

Step 1

On the Layers panel, select the Images layer. 

On the toolbar, select the Rectangle Frame Tool (F). Draw a rectangle that fits Page 1, making sure you are also including the bleeds on the top, left, and bottom sides.

Create a frame that fits page 1

Step 2

With the frame selected, press Command-D to Place an image. Locate the African elementary school girl image and click Open. 

Using the Direct Selection Tool (A), select the image within the frame. Head over to the Control panel and activate the Constrain Proportion for Scaling button. Set the Percentage to 70%. Move the image until you’ve placed the subject in the center of the panel. 

Place an image

Step 3

Often, images won’t have the same color grading. It is useful to have an overlay to achieve an even look throughout the project. Using the Rectangle Tool (M), create a rectangle over the image. 

Using the Swatches panel, set the color to purple. Head over to the Effects panel (Windows > Effects) and set the Blending Mode to Color. 

create an overlay rectangle to achieve even color grading

Step 4

Head over to the inside panels, and let’s start on Page 4. Select the Ellipse Frame Tool (F) from the toolbar. Click on the document to open the Ellipse option window. Set the Width and Height to 60 mm. Click OK. 

On the Swatches panel, set the color of the Stroke to [Paper]. Head over to the Control panel, and set the Stroke Weight to 5 pt. 

create an ellipse

Step 5

Select the circle and press Command-D to Place an image. Locate the School girl writing in class .jpg and click Open. 

Using the Direct Selection Tool (A), select the image within the circle. You can resize the image by dragging any of the eight points around the image. Press Shift as you drag to resize the image proportionally. In the image below, you can see I’ve resized it just enough to fit the circle.

Place an image within the circle

Step 6

Using the Ellipse Tool (L), draw another circle measuring 6 mm in Width and Height. Place this ellipse over the circular image we added above.

On the Swatches panel, set the color of the Stroke to [Paper]. Press X to switch to the Fill color and set the color to Black. Head over to the Control panel, and set the Stroke Weight to 5 pt. 

Head over to the Effects panel, and set the Blending Mode to Color.

Add an overlay to the circled image

Step 7

Using the Align panel, select both images and press the Align Horizontal Center button and the Align Vertical Center button. To Group both of these ellipses, press Command-G. 

To align the grouped objects to the center of the panel, head over to the Align panel. Set the Align To: option to Page and click the Align Horizontal Center button.

align both ellipses

Step 8

Duplicate the grouped ellipses by pressing Option and dragging to Page 6. Ungroup the ellipses by pressing Shift-Command-G, and move the overlay ellipse to the side. 

Select the circular image and press Command-D to Place another image. In the Place option window, select the Young family with kids image. Head over to the bottom and check the Replace Selected Item option. Click Open. 

Using the Direct Selection Tool (A), resize and move the image. Remember to press Shift to resize the image proportionally. 

resize the image to fit the circle

When you are done, align both ellipses with the Align panel. Group them by pressing Command-G and align them again to the panel. Head over to the Align panel. Set the Align To: option to Page and click the Align Horizontal Center button.

align the image and the overlay with the align panel

Step 9

Add a logo to Pages 2 and 3 by pressing Command-D and locating the logo you wish to add. For this tutorial, I will be adding the Xavier logo template. To resize the vector proportionally, press Shift and drag one of the points. Place both logos in the bottom left portion of the page.

add a logo to pages 2 and 3

4. How to Add Text to Your Pamphlet Design

Step 1

On the Layers panel, lock the Photos layer and select the Text layer. 

Using the Text Tool (T), create two text boxes. Add a title to the top text box and a smaller tagline to the bottom text box. Head over to the Character panel and use the following settings:

  • Font: Bw Stretch
  • Font Family: Heavy
  • Size: 80 pt
  • Leading: 70
  • Tracking: -10

On the Swatches panel, set the top text box to purple and the bottom text box to orange. Place both text boxes on top of each other, right at the edge of both rectangles we drew earlier.

Feel free to unlock the Vectors layer and move the rectangles to fit the text. 

add a title to page 3

Step 2

Head over to the interior of the pamphlet (Pages 4-6). Using the Text Tool (T), draw a text box at the top of Page 3, and using the Paragraph panel, format the text box to Title. 

Draw another text box under the circular image, add text, and format it to Copy on the Paragraph panel. 

add text to the page 4

Step 3

On Page 5, create a text box that fits the top half of the page. Use the Paragraph panel to format the text and the Swatches panel to change the colors. 

on page 5 add text to the top half of the panel

For the statistics, use the Ellipse Tool (L). Click on the page to open the Ellipse option window, and set the Width and Height to 20 mm. Draw a second ellipse that measures 23 mm. 

On the Swatches panel, select the purple Fill color for the smaller circle and the purple Stroke color for the bigger circle. Select this last circle and head over to the Control bar. Set the Stroke Weight to 1 pt and the Stroke Type to Dotted.

Create an ellipse and set the fill to purple Create a second ellipse and set the stroke to purple and stroke weight to 1pt

Select the Type Tool (T) from the toolbar. Click on the filled circle to add text, and set the format to the Pull-quote style. Head over to the Control bar and select the Align Center button.

Add text to the ellipse

Step 4

Select both circles and Group them by pressing Command-G. Duplicate the group twice by pressing Shift-Command and dragging down. 

Select all three groups and head over to the Align panel. Set the Align To: Selection option, and select the Distribute Vertical Center button.

duplicate and align the statistics ellipses

Using the Type Tool (T), create a text box to the right of each statistic. Format the text as Copy on the Paragraph panel.

add a text box to the right of each statistic number

Step 5

Let’s work on Page 6. Create a text box using the Type Tool (T) at the top of the page. Format the type with the Paragraph panel and change the color to [Paper] on the Swatches panel. 

on page 6 create a text box on the top of the panel

Add a text box under the image. Set the Paragraph panel to Pull-quote and the Swatches panel to [Paper]. The quote caption can be set independently on the Control bar or the Character panel. I’ve set the Font Family to a combination of Heavy and Book and a Size 10 pt. 

add a pull-quote below the image

Step 6

Let’s head over to Page 2. Using the Type Tool (T), add a text box on the top of the page. 

On page 2 add text on the top portion of the panel

To create a table, create a text box and double-click to activate it. While selecting it, head over to Table > Insert Table. In the Insert Table option window, set the Body Rows to 10 and the Columns to 2. Click OK. 

insert a table on a text box

Double-click on the table to activate it and select all the rows and columns. Head over to the Control bar, and you will notice a small window showing a table. To edit this, you have to choose which parts of the table you’d like to edit. In this case, click on the horizontal lines to deactivate them. Set the Stroke Color to [None].

Deactivate the three vertical lines and activate the three horizontal lines. Set Stroke Color to [Paper] and the Stroke Weight to 0.5 pt.

using the control bar add horizontal strokes to the table

Head over to Window > Type & Tables > Table to open the Table panel. Select all of the first column and set the Row Height to At least and 10 mm. Set the Column Width to 15 mm and select the Align Center button. 

Open the table panel and set the row height to 10 mm and column width to 15 mm

Select the second column, set the Column Width to 63 mm, and select the Align Center button.

Add text using the Type Tool (T), using the Paragraph panel set the text to Copy. Feel free to change the color on the Swatches panel to [Paper]. 

Add text using the type tool

5. How to Export a File for Printing

Before exporting a file for printing, it’s useful to take a look around all the edges of the brochure. This is to make sure all the images and vectors bleeding out are touching the bleeds.

Remember that each fold will be represented by a crop mark. Let your professional printer know to avoid confusion. 

Step 1

To export the file, go to File > Export. Name the file Pamphlet Template and choose Adobe PDF (Print) from the Format dropdown menu. Click Save. 

Export a pdf file

Step 2

In the Export Adobe PDF window, set the Adobe PDF Preset to Press Quality. Under Pages, set Export As Spreads. 

set the PDF preset to Press Quality

On the left side of the panel, select Marks and Bleeds. Check All Printer’s Marks and Use Document Bleed Settings. Click Export. You will have a PDF file ready to be printed. 

Check the marks and bleeds and document bleed settings options

Great Job! You’ve Finished This Tutorial!

In this tutorial, we learned how to make a brochure template. We covered key tools that will help anyone edit a template quickly and easily. Today, we learned to:

  • set up a ready-to-print InDesign file
  • use the Layers panel to divide the elements in a file
  • add color swatches, which can be changed easily to match any brand
  • set up styles in the Paragraph panel 
  • set up tables in an InDesign file
  • export a ready-to-print PDF file

If you would like to explore more template ideas, you can find many customizable brochures and flyers over at Envato Elements and Graphic River. Check it out!

If you liked this tutorial, you might like these:

{excerpt}
Read More

Creating a Side Chained Reverb in Ableton

Reverb is an essential part of any musical production as it adds the necessary depth, space and ambience. 

Adding in reverb to tracks, however, can be troublesome. Often reverbs clutter up the mix and create a unnecessary mud. 

By using Ableton‘s side chain compressor on reverbs, you can avoid creating a muddy mix. In this screencast, I’ll show you how to create a side chained reverb in Ableton 10 to clean up the mix. 

Screencast

{excerpt}
Read More

JSON Decoding in Swift Example – Decoding Nested JSON From The Free Fortnite API

Decoding JSON is a common task, and with the improvements in Swift has become much easier. Decoding nested JSON can still be somewhat complex, and in this example we’ll take data from the Free Fortnite API and use that to illustrate how to parse nested JSON data in Swift.

To begin with we’re going to import the foundation library for JSONDecoder and create a string containing the json data (the current data can be found at https://fortnite-public-api.theapinetwork.com/prod09/challenges/get?season=current). 

import Foundation

// create the json string, this from fortniteapi.com containing
// the Fortnite challenges data
let json = "{"language":"en","season":7,"currentweek":7,"star":"https:\\/\\/fortnite–public–files.theapinetwork.com\\/fortnite–br–challenges–star.png","challenges":{"week1":[{"identifier":"e2c420d–928d4bf–8ce0ff2–ec19b37","challenge":"Pick up an item of each rarity","total":5,"stars":5,"difficulty":"normal"},{"identifier":"e2c420d–928d4bf–8ce0ff2–ec19b37","challenge":"Dance in different forbidden locations","total":7,"stars":5,"difficulty":"normal"},{"identifier":"e2c420d–928d4bf–8ce0ff2–ec19b37","challenge":"Play matches with at least one elimination","total":5,"stars":10,"difficulty":"hard"},{"identifier":"e2c420d–928d4bf–8ce0ff2–ec19b37","challenge":"Stage 1: Dance on top of a crown of RV\’s","total":1,"stars":5,"difficulty":"normal"},{"identifier":"e2c420d–928d4bf–8ce0ff2–ec19b37","challenge":"Deal Headshot Damage to opponents","total":500,"stars":5,"difficulty":"normal"},{"identifier":"e2c420d–928d4bf–8ce0ff2–ec19b37","challenge":"Stage 1: Search Ammo Boxes in a single match","total":5,"stars":10,"difficulty":"hard"},{"identifier":"e2c420d–928d4bf–8ce0ff2–ec19b37","challenge":"Eliminate opponents in different Named Locations","total":5,"stars":10,"difficulty":"hard"}],"week2":[{"identifier":"32bb90e–8976aab–5298d5d–a10fe66","challenge":"Search a Chest in different Named Locations","total":7,"stars":5,"difficulty":"normal"},{"identifier":"32bb90e–8976aab–5298d5d–a10fe66","challenge":"Damage opponents with different types of weapons","total":5,"stars":5,"difficulty":"normal"},{"identifier":"32bb90e–8976aab–5298d5d–a10fe66","challenge":"Eliminate opponents in Snobby Shores or Fatal Fields","total":3,"stars":10,"difficulty":"hard"},{"identifier":"32bb90e–8976aab–5298d5d–a10fe66","challenge":"Stage 1: Visit Snobby Shores and Pleasant Park in a single match","total":2,"stars":5,"difficulty":"normal"},{"identifier":"32bb90e–8976aab–5298d5d–a10fe66","challenge":"Play the Sheet Music on the planos near Pleasant Park and Lonely Lodge","total":2,"stars":5,"difficulty":"normal"},{"identifier":"32bb90e–8976aab–5298d5d–a10fe66","challenge":"Complete in a Dance Off at an abandoned mansion","total":1,"stars":10,"difficulty":"hard"},{"identifier":"32bb90e–8976aab–5298d5d–a10fe66","challenge":"Eliminate an opponents from at least 50m away","total":1,"stars":10,"difficulty":"hard"}],"week3":[{"identifier":"d2ddea1–8f00665–ce8623e–36bd4e3","challenge":"Ride a Zipline in different matches","total":5,"stars":5,"difficulty":"normal"},{"identifier":"d2ddea1–8f00665–ce8623e–36bd4e3","challenge":"Stage 1: Land at Lonely Lodge","total":1,"stars":1,"difficulty":"normal"},{"identifier":"d2ddea1–8f00665–ce8623e–36bd4e3","challenge":"Legendary weapon Eliminations","total":2,"stars":10,"difficulty":"hard"},{"identifier":"d2ddea1–8f00665–ce8623e–36bd4e3","challenge":"Search Chests at Polar Peak or Tomato Temple","total":7,"stars":5,"difficulty":"normal"},{"identifier":"d2ddea1–8f00665–ce8623e–36bd4e3","challenge":"Ring a doorbell in different named locations in a single match","total":2,"stars":5,"difficulty":"normal"},{"identifier":"d2ddea1–8f00665–ce8623e–36bd4e3","challenge":"Search between three ski lodges","total":1,"stars":10,"difficulty":"hard"},{"identifier":"d2ddea1–8f00665–ce8623e–36bd4e3","challenge":"Stage 1: Deal damage with Shotguns to opponents","total":200,"stars":3,"difficulty":"normal"}],"week4":[{"identifier":"ad61ab1–43223ef–bc24c7d–2583be6","challenge":"Use an X–4 Stormwing plane in different matches","total":5,"stars":5,"difficulty":"normal"},{"identifier":"ad61ab1–43223ef–bc24c7d–2583be6","challenge":"Launch fireworks","total":3,"stars":5,"difficulty":"normal"},{"identifier":"ad61ab1–43223ef–bc24c7d–2583be6","challenge":"Eliminate opponents at Expedition Outposts","total":3,"stars":10,"difficulty":"hard"},{"identifier":"ad61ab1–43223ef–bc24c7d–2583be6","challenge":"Stage 1: Destroy chairs","total":80,"stars":2,"difficulty":"normal"},{"identifier":"ad61ab1–43223ef–bc24c7d–2583be6","challenge":"Deal damage with a pickaxe to opponents","total":100,"stars":5,"difficulty":"normal"},{"identifier":"ad61ab1–43223ef–bc24c7d–2583be6","challenge":"Eliminate opponents at Happy Hamlet or Pleasent Park","total":3,"stars":10,"difficulty":"hard"},{"identifier":"ad61ab1–43223ef–bc24c7d–2583be6","challenge":"Stage 1: Search the letter \’O\’ west of Pleasent Park","total":1,"stars":2,"difficulty":"normal"}],"week5":[{"identifier":"d09bf41–544a336–5a46c90–77ebb5e","challenge":"Stage 1: Land at Polar Peak","total":1,"stars":1,"difficulty":"normal"},{"identifier":"d09bf41–544a336–5a46c90–77ebb5e","challenge":"Deal damage to opponent\’s structures","total":5000,"stars":5,"difficulty":"normal"},{"identifier":"d09bf41–544a336–5a46c90–77ebb5e","challenge":"Suppressed weapon eliminations","total":3,"stars":10,"difficulty":"hard"},{"identifier":"d09bf41–544a336–5a46c90–77ebb5e","challenge":"Stage 1: Dance on top of a Water Tower","total":1,"stars":1,"difficulty":"normal"},{"identifier":"d09bf41–544a336–5a46c90–77ebb5e","challenge":"Search Chests at Wailing Woods or Paradise Palms","total":7,"stars":5,"difficulty":"normal"},{"identifier":"d09bf41–544a336–5a46c90–77ebb5e","challenge":"Search between a Giant Rock Man, a Crowned Tomato and an Encircled Tree","total":1,"stars":10,"difficulty":"hard"},{"identifier":"d09bf41–544a336–5a46c90–77ebb5e","challenge":"Eliminate an opponent from closer than 5m away","total":3,"stars":10,"difficulty":"hard"}],"week6":[{"identifier":"fbd7939–d674997–cdb4692–d34de86","challenge":"Search an Ammo Box in different Named Locations","total":7,"stars":5,"difficulty":"normal"},{"identifier":"fbd7939–d674997–cdb4692–d34de86","challenge":"Search Chilly Gnomes","total":7,"stars":5,"difficulty":"normal"},{"identifier":"fbd7939–d674997–cdb4692–d34de86","challenge":"Eliminate opponents in Lucky Landing or Tilted Towers","total":3,"stars":10,"difficulty":"hard"},{"identifier":"fbd7939–d674997–cdb4692–d34de86","challenge":"Stage 1: Visit Polar Peak and Tilted Towers in a single match","total":2,"stars":1,"difficulty":"normal"},{"identifier":"fbd7939–d674997–cdb4692–d34de86","challenge":"Slide a Ice Puck over 150m in a single throw","total":1,"stars":5,"difficulty":"normal"},{"identifier":"fbd7939–d674997–cdb4692–d34de86","challenge":"Stage 1: Deal damage with SMGs to opponents","total":200,"stars":3,"difficulty":"normal"},{"identifier":"fbd7939–d674997–cdb4692–d34de86","challenge":"Deal damage with different weapons in a single match","total":5,"stars":10,"difficulty":"hard"}],"week7":[{"identifier":"28dd2c7–955ce92–6456240–b2ff010","challenge":"Visit all Expedition Outposts","total":7,"stars":5,"difficulty":"normal"},{"identifier":"28dd2c7–955ce92–6456240–b2ff010","challenge":"Use a Rift or Rift–To–Go in different matches","total":3,"stars":5,"difficulty":"normal"},{"identifier":"28dd2c7–955ce92–6456240–b2ff010","challenge":"Pistol Eliminations","total":3,"stars":10,"difficulty":"hard"},{"identifier":"28dd2c7–955ce92–6456240–b2ff010","challenge":"Stage 1: Land at Salty Springs","total":1,"stars":1,"difficulty":"normal"},{"identifier":"28dd2c7–955ce92–6456240–b2ff010","challenge":"Search Chests at Loot Lake or Frosty Flights","total":7,"stars":5,"difficulty":"normal"},{"identifier":"28dd2c7–955ce92–6456240–b2ff010","challenge":"Destroy flying X–4 Stormwings","total":1,"stars":10,"difficulty":"hard"},{"identifier":"28dd2c7–955ce92–6456240–b2ff010","challenge":"Stage 1: Damage opponents in a single match","total":200,"stars":3,"difficulty":"normal"}],"week8":[],"week9":[],"week10":[]}}"

If you’re going to code along with this example be sure to place
this at the top of the code.

The image below shows the JSON structure for the challenges
from the API.

The Structure for the JSON we are decoding, visualized using codebeautify.org.

Now to begin with we’ll create a decodable struct for the first
level of data and print that:

// creating a structure that conforms to JSON data is very easy
// using Codable for the first layer of data
struct FortniteChallenges: Codable {
    // in this json we know that all the data exists, you can use
    // optionals just in case
    let language: String
    let season: Int
    let currentweek: Int
    let star: String
}

// we now need to simply decode the data using JSON decoder and pass
// in the struct we created defining the structure of the JSON
if let jsonData = json.data(using: .utf8) {
    let decoder = JSONDecoder()
    // you might notice here i used try! for simplicity, this would crash your
    // program if a JSON value defined does not exist
    let challengesData = try! decoder.decode(FortniteChallenges.self, from: jsonData)

    // we’ll print out the values here
    print("Language: \(challengesData.language)")
    print("Season: \(challengesData.season)")
    print("Current Week: \(challengesData.currentweek)")
    print("Star image url: \(challengesData.star)")
}

As you can see Swift does almost all the work for us as long
as the names and types match those within the JSON data.

One common case is that you’ll have data in the JSON that isn’t always present, while that doesn’t happen here Swift optionals, and we’ll include an extra literal in this example for this case:

struct FortniteChallenges: Codable {
    let language: String
    let season: Int
    let currentweek: Int
    let star: String
    // if a key does not always exist you can use an optional
    // so that JSONEncoder does not throw an error but instead
    // sets the value to nil
    let notreallyhere: String?
}

Now looking at the structure we see that the challenges value
contains a nested array with each week, and within that another array containing
the challenges for that specific week. 

To handle this we’ll create a String dictionary for the week
name, and within that place the challenges.   We’ll then print out the weeks and each challenge from that week.
as in the example:

struct FortniteChallenges: Codable {
    let language: String
    let season: Int
    let currentweek: Int
    let star: String
    // we’ll create a challenges dictionary here for the
    // week string within which we’ll store the data
    // for each challenge
    let challenges: [String: [Challenge]]

    struct Challenge: Codable {
        let identifier: String
        let challenge: String
        let total: Int
        let stars: Int
        let difficulty: Difficulty

        enum Difficulty: String, Codable {
            case normal = "normal"
            case hard = "hard"
        }
    }
}

if let jsonData = json.data(using: .utf8) {
    let decoder = JSONDecoder()
    let challengesData = try! decoder.decode(FortniteChallenges.self, from: jsonData)

    print("Language: \(challengesData.language)")
    print("Season: \(challengesData.season)")
    print("Current Week: \(challengesData.currentweek)")
    print("Star image url: \(challengesData.star)")

    // Iterate through each week of challenges within
    // the data and print the week
    for (week, challenges) in challengesData.challenges {
        print("*******")
        print("Week: \(week)")

        // iterate through each challenge and print the
        // data
        for challenge in challenges {
            print("Identifier: \(challenge.identifier)")
            print("Challenge: \(challenge.challenge)")
            print("Total: \(challenge.total)")
            print("Stars: \(challenge.stars)")
            print("Difficulty: \(challenge.difficulty)")
        }
    }
}

Now you might want further type safety, since we know that Difficulty
can only be normal or hard we’ll create an enum for those possibilities and example
we’ll also use a filter to display only the hard challenges:

struct FortniteChallenges: Codable {
    let language: String
    let season: Int
    let currentweek: Int
    let star: String
    let challenges: [String: [Challenge]]

    struct Challenge: Codable {
        let identifier: String
        let challenge: String
        let total: Int
        let stars: Int
        // we’ll change the difficulty to an enum
        // this will contain two cases for the
        // two difficulty possibilities
        let difficulty: Difficulty

        enum Difficulty: String, Codable {
            case normal = "normal"
            case hard = "hard"
        }
    }
}

if let jsonData = json.data(using: .utf8) {
    let decoder = JSONDecoder()
    let challengesData = try! decoder.decode(FortniteChallenges.self, from: jsonData)

    print("Language: \(challengesData.language)")
    print("Season: \(challengesData.season)")
    print("Current Week: \(challengesData.currentweek)")
    print("Star image url: \(challengesData.star)")

    // loop through the challenges
    for (week, challenges) in challengesData.challenges {
        print("*******")
        print("Week: \(week)")

        // we’ll filter out and show only hard difficulty
        // challenges
        for challenge in challenges where challenge.difficulty.rawValue == "hard" {
            print("Identifier: \(challenge.identifier)")
            print("Challenge: \(challenge.challenge)")
            print("Total: \(challenge.total)")
            print("Stars: \(challenge.stars)")
            print("Difficulty: \(challenge.difficulty)")
        }
    }
}

Trying this code you might have noticed that the challenges
are unsorted, that’s because they are contained with a dictionary. 

In this final example we’ll sort them by week, because the
week values contain both strings and numbers we’ll use the string function
localizedStandardCompare so everything sorts properly (otherwise week10 would appear before week2).

struct FortniteChallenges: Codable {
    let language: String
    let season: Int
    let currentweek: Int
    let star: String
    let challenges: [String: [Challenge]]

    struct Challenge: Codable {
        let identifier: String
        let challenge: String
        let total: Int
        let stars: Int
        let difficulty: Difficulty

        enum Difficulty: String, Codable {
            case normal = "normal"
            case hard = "hard"
        }
    }
}

if let jsonData = json.data(using: .utf8) {
    let decoder = JSONDecoder()
    let challengesData = try! decoder.decode(FortniteChallenges.self, from: jsonData)

    print("Language: \(challengesData.language)")
    print("Season: \(challengesData.season)")
    print("Current Week: \(challengesData.currentweek)")
    print("Star image url: \(challengesData.star)")

    // we’ll sort the challenges dictionary here because
    // the week value contains both text and numbers we’ll
    // use localizedStandardCompare to make sure that they
    // are sorted properly
    let challenges = challengesData.challenges.sorted(by: {
        $0.0.localizedStandardCompare($1.0) == .orderedAscending
        })

    // loop through the new sorted challenges
    for (week, challenges) in challenges {
        print("*******")
        print("Week: \(week)")

        for challenge in challenges where challenge.difficulty.rawValue == "hard" {
            print("Identifier: \(challenge.identifier)")
            print("Challenge: \(challenge.challenge)")
            print("Total: \(challenge.total)")
            print("Stars: \(challenge.stars)")
            print("Difficulty: \(challenge.difficulty)")
        }
    }
}

Hopefully this has helped for anyone decoding JSON
and dealing with JSON that includes nested values.

Original article: JSON Decoding in Swift Example – Decoding Nested JSON From The Free Fortnite API

©2019 iOS App Dev Libraries, Controls, Tutorials, Examples and Tools. All Rights Reserved.