{"id":1517,"date":"2022-01-21T17:23:34","date_gmt":"2022-01-21T17:23:34","guid":{"rendered":"http:\/\/sophyonline.com\/blog\/?p=1517"},"modified":"2022-01-21T18:43:37","modified_gmt":"2022-01-21T18:43:37","slug":"xcode-assets-how-to-extract-multiple-colors-json-values-into-a-csv-file","status":"publish","type":"post","link":"http:\/\/sophyonline.com\/blog\/2022\/01\/xcode-assets-how-to-extract-multiple-colors-json-values-into-a-csv-file\/","title":{"rendered":"Xcode Assets: How to extract multiple Colors.json values into a CSV file"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">When you have an app with lots of colors, even colors for different targets (as our application at the moment have) is difficult to organize it, or find a hex value since we&#8217;re depending exclusively on the <strong>Color.asset<\/strong> and Xcode doesn&#8217;t yet provide us with this capability. To find colors we will need the semantic name that it was given, and if you&#8217;re trying to migrate to a design system, or clean assets, this well, turns out to be challenging &#x1f609;<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"995\" height=\"836\" src=\"http:\/\/sophyonline.com\/blog\/wp-content\/uploads\/2022\/01\/color_assets-1.png\" alt=\"\" class=\"wp-image-1526\" srcset=\"http:\/\/sophyonline.com\/blog\/wp-content\/uploads\/2022\/01\/color_assets-1.png 995w, http:\/\/sophyonline.com\/blog\/wp-content\/uploads\/2022\/01\/color_assets-1-300x252.png 300w, http:\/\/sophyonline.com\/blog\/wp-content\/uploads\/2022\/01\/color_assets-1-768x645.png 768w\" sizes=\"auto, (max-width: 995px) 100vw, 995px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If we want then to extract all these colors somehow, into a CSV file so we can handle them, something like the next image, we will need to research a bit how.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"560\" src=\"http:\/\/sophyonline.com\/blog\/wp-content\/uploads\/2022\/01\/Screenshot-2022-01-21-at-17.07.29-1024x560.png\" alt=\"\" class=\"wp-image-1519\" srcset=\"http:\/\/sophyonline.com\/blog\/wp-content\/uploads\/2022\/01\/Screenshot-2022-01-21-at-17.07.29-1024x560.png 1024w, http:\/\/sophyonline.com\/blog\/wp-content\/uploads\/2022\/01\/Screenshot-2022-01-21-at-17.07.29-300x164.png 300w, http:\/\/sophyonline.com\/blog\/wp-content\/uploads\/2022\/01\/Screenshot-2022-01-21-at-17.07.29-768x420.png 768w, http:\/\/sophyonline.com\/blog\/wp-content\/uploads\/2022\/01\/Screenshot-2022-01-21-at-17.07.29-1536x839.png 1536w, http:\/\/sophyonline.com\/blog\/wp-content\/uploads\/2022\/01\/Screenshot-2022-01-21-at-17.07.29.png 1874w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The first step is to check how Xcode creates these files and what&#8217;s the content inside it:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We go to<strong> Colors.xcassets<\/strong> and check out that each grouping of colors have inside a <strong>Contents.json<\/strong> file. This is good, since JSON has a lot of external support to parse it using third party apps, libraries, etc.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"296\" height=\"79\" src=\"http:\/\/sophyonline.com\/blog\/wp-content\/uploads\/2022\/01\/color-xcassets-content-files.png\" alt=\"\" class=\"wp-image-1521\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The next step is to check how&#8217;s that JSON constructed. And as the image shows, we have a <strong><em>components<\/em><\/strong> property with the RGB values as individual properties. This is also a good news for us, since we can absolutely parse these values!<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"245\" height=\"260\" src=\"http:\/\/sophyonline.com\/blog\/wp-content\/uploads\/2022\/01\/image-1.png\" alt=\"\" class=\"wp-image-1522\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Now that we have a clear image of the file and properties of the JSON, the next question is: what should we use to parse it?. So, I&#8217;ve asked around and found out that there&#8217;s actually a very useful CLI tool provided by <a href=\"https:\/\/github.com\/stedolan\/jq\">Sted Holland <\/a>called <strong><a href=\"https:\/\/stedolan.github.io\/jq\/\">jq<\/a><\/strong>. Thanks to <a href=\"https:\/\/twitter.com\/j4n0\">@j4n0<\/a> for the tip!!!<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><em>jq is like&nbsp;<code>sed<\/code>&nbsp;for JSON data &#8211; you can use it to slice and filter and map and transform structured data with the same ease that&nbsp;<code>sed<\/code>,&nbsp;<code>awk<\/code>,&nbsp;<code>grep<\/code>&nbsp;and friends let you play with text<\/em><\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">And indeed is good. I found out several resources and tutorials, and it was really easy to grasp. I will add them here at the end of the post as references.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So basically we can navigate to the components property, and create 3 rows with the properties red, green and blue that will be added to the csv file. We use the @csv to indicate jq that those 3 will be added to that <strong>csv file<\/strong> and we bulk the result into the generated csv file called <strong>test1.csv<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><meta charset=\"utf-8\"><code>jq -r '.colors[]?.color.components? | [.red, .green, .blue] | @csv' &gt;&gt; test11.csv<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The other thing left is to loop into the <strong><em>Contents.json<\/em><\/strong> files and apply the <strong><em>jq<\/em><\/strong>. We then use the <em><strong>find<\/strong><\/em> command and at the end of this command we&#8217;ll use <strong><em>print<\/em><\/strong> command to print&nbsp; the file name&nbsp;<em>after<\/em>&nbsp;the matching lines because we want to have the name of the color of such RGB values.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>find . -name \"Contents.json\" -type f -exec jq -r '.colors[]?.color.components? | [.red, .green, .blue] | @csv' &gt;&gt; test11.csv '{}' \\; -print<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now, the result will be something like the next image bellow. You will need just to adjust a little bit the csv file and that&#8217;s it. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"685\" height=\"331\" src=\"http:\/\/sophyonline.com\/blog\/wp-content\/uploads\/2022\/01\/image-2-1.png\" alt=\"\" class=\"wp-image-1539\" srcset=\"http:\/\/sophyonline.com\/blog\/wp-content\/uploads\/2022\/01\/image-2-1.png 685w, http:\/\/sophyonline.com\/blog\/wp-content\/uploads\/2022\/01\/image-2-1-300x145.png 300w\" sizes=\"auto, (max-width: 685px) 100vw, 685px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>References<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/stackoverflow.com\/questions\/57242240\/jq-object-cannot-be-csv-formatted-only-array\">https:\/\/stackoverflow.com\/questions\/57242240\/jq-object-cannot-be-csv-formatted-only-array<\/a><\/li><li><a href=\"https:\/\/stackoverflow.com\/questions\/32960857\/how-to-convert-arbitrary-simple-json-to-csv-using-jq\">https:\/\/stackoverflow.com\/questions\/32960857\/how-to-convert-arbitrary-simple-json-to-csv-using-jq<\/a><\/li><li><a href=\"https:\/\/cameronnokes.com\/blog\/jq-cheatsheet\/\">https:\/\/cameronnokes.com\/blog\/jq-cheatsheet\/<\/a><\/li><li><a href=\"https:\/\/earthly.dev\/blog\/jq-select\/\">https:\/\/earthly.dev\/blog\/jq-select\/<\/a><\/li><li><a href=\"https:\/\/unix.stackexchange.com\/questions\/163845\/using-jq-to-extract-values-and-format-in-csv\/227950#227950\">https:\/\/unix.stackexchange.com\/questions\/163845\/using-jq-to-extract-values-and-format-in-csv\/227950#227950<\/a><\/li><li><a href=\"https:\/\/stedolan.github.io\/jq\/tutorial\/\">https:\/\/stedolan.github.io\/jq\/tutorial\/<\/a><\/li><li><a href=\"https:\/\/unix.stackexchange.com\/questions\/163845\/using-jq-to-extract-values-and-format-in-csv\/227950?newreg=1f8668c401894de992ffd5170ead084f\">https:\/\/unix.stackexchange.com\/questions\/163845\/using-jq-to-extract-values-and-format-in-csv\/227950?newreg=1f8668c401894de992ffd5170ead084f<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>When you have an app with lots of colors, even colors for different targets (as our application at the moment have) is difficult to organize it, or find a hex value since we&#8217;re depending exclusively on the Color.asset and Xcode doesn&#8217;t yet provide us with this capability. To find colors we will need the semantic <a class=\"read-more\" href=\"http:\/\/sophyonline.com\/blog\/2022\/01\/xcode-assets-how-to-extract-multiple-colors-json-values-into-a-csv-file\/\">&hellip;&nbsp;<span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","footnotes":""},"categories":[3,16],"tags":[44,17,45],"class_list":["post-1517","post","type-post","status-publish","format-standard","hentry","category-development","category-xcode","tag-color-json","tag-xcode","tag-xcode-assets"],"_links":{"self":[{"href":"http:\/\/sophyonline.com\/blog\/wp-json\/wp\/v2\/posts\/1517","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/sophyonline.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/sophyonline.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/sophyonline.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/sophyonline.com\/blog\/wp-json\/wp\/v2\/comments?post=1517"}],"version-history":[{"count":16,"href":"http:\/\/sophyonline.com\/blog\/wp-json\/wp\/v2\/posts\/1517\/revisions"}],"predecessor-version":[{"id":1543,"href":"http:\/\/sophyonline.com\/blog\/wp-json\/wp\/v2\/posts\/1517\/revisions\/1543"}],"wp:attachment":[{"href":"http:\/\/sophyonline.com\/blog\/wp-json\/wp\/v2\/media?parent=1517"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/sophyonline.com\/blog\/wp-json\/wp\/v2\/categories?post=1517"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/sophyonline.com\/blog\/wp-json\/wp\/v2\/tags?post=1517"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}