{"id":7153,"date":"2022-07-26T12:34:40","date_gmt":"2022-07-26T13:04:40","guid":{"rendered":"https:\/\/ultering.com\/it4us\/?p=7153"},"modified":"2022-10-15T18:37:55","modified_gmt":"2022-10-15T19:07:55","slug":"angular-reading-json-files","status":"publish","type":"post","link":"https:\/\/ultering.com\/it4us\/?p=7153","title":{"rendered":"angular: Reading JSON files"},"content":{"rendered":"<p>Follow the example.<\/p>\n<p>The JSON file:<\/p>\n<p>_config.json<\/p>\n<pre class=\"\">{\r\n  \"devpg\": {\r\n    \"username\": \"johndoe\",\r\n    \"password\": \"secret\",\r\n    \"database\": \"mydb\",\r\n    \"host\": \"127.0.0.1\",\r\n    \"dialect\": \"postgres\",\r\n    \"port\": \"5432\",\r\n    \"url\": \"postgres:\/\/johndoe:secret@127.0.0.1:5432\/mydb\"\r\n  },\r\n  \"prodpg\": {\r\n    \"username\": \"server1\",\r\n    \"password\": \"secret\",\r\n    \"database\": \"mydb\",\r\n    \"host\": \"localhost\",\r\n    \"dialect\": \"postgres\",\r\n    \"port\": \"5432\",\r\n    \"url\": \"postgres:\/\/server1:secret@localhost:5432\/mydb\"\r\n  },\r\n  \"sessionKey\": \"alfja1234\",\r\n}\r\n\r\n<\/pre>\n<p>The component.ts file<\/p>\n<pre class=\"\">import { Component, OnInit } from '@angular\/core';\r\nconst _config = require('..\/..\/_config\/_config.json');\r\n\r\n@Component({\r\n  selector: 'app-config03',\r\n  templateUrl: '.\/config03.component.html',\r\n  styleUrls: ['.\/config03.component.scss']\r\n})\r\nexport class Config03Component implements OnInit {\r\n\r\n  config: any;\r\n\r\n  constructor() { }\r\n\r\n  ngOnInit(): void {\r\n    let json = JSON.stringify(_config);\r\n    this.config = JSON.parse(json);\r\n    this.setenv();\r\n  }\r\n\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<!--CusAds0-->\n<div style=\"font-size: 0px; height: 0px; line-height: 0px; margin: 0; padding: 0; clear: both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>Follow the example. The JSON file: _config.json { &#8220;devpg&#8221;: { &#8220;username&#8221;: &#8220;johndoe&#8221;, &#8220;password&#8221;: &#8220;secret&#8221;, &#8220;database&#8221;: &#8220;mydb&#8221;, &#8220;host&#8221;: &#8220;127.0.0.1&#8221;, &#8220;dialect&#8221;: &#8220;postgres&#8221;, &#8220;port&#8221;: &#8220;5432&#8221;, &#8220;url&#8221;: &#8220;postgres:\/\/johndoe:secret@127.0.0.1:5432\/mydb&#8221; }, &#8220;prodpg&#8221;: { &#8220;username&#8221;: &#8220;server1&#8221;, &#8220;password&#8221;: &#8220;secret&#8221;, &#8220;database&#8221;: &#8220;mydb&#8221;, &#8220;host&#8221;: &#8220;localhost&#8221;, &#8220;dialect&#8221;: &#8220;postgres&#8221;, &#8220;port&#8221;: &#8220;5432&#8221;, &#8220;url&#8221;: &#8220;postgres:\/\/server1:secret@localhost:5432\/mydb&#8221; }, &#8220;sessionKey&#8221;: &#8220;alfja1234&#8221;, } The component.ts file import { Component, OnInit } from &#8216;@angular\/core&#8217;; &#8230; <a href=\"https:\/\/ultering.com\/it4us\/?p=7153\" class=\"more-link\">Read More<span class=\"screen-reader-text\"> &#8220;angular: Reading JSON files&#8221;<\/span> &raquo;<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"footnotes":""},"categories":[192],"tags":[],"class_list":["post-7153","post","type-post","status-publish","format-standard","hentry","category-angular"],"_links":{"self":[{"href":"https:\/\/ultering.com\/it4us\/index.php?rest_route=\/wp\/v2\/posts\/7153","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ultering.com\/it4us\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ultering.com\/it4us\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ultering.com\/it4us\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/ultering.com\/it4us\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7153"}],"version-history":[{"count":1,"href":"https:\/\/ultering.com\/it4us\/index.php?rest_route=\/wp\/v2\/posts\/7153\/revisions"}],"predecessor-version":[{"id":7212,"href":"https:\/\/ultering.com\/it4us\/index.php?rest_route=\/wp\/v2\/posts\/7153\/revisions\/7212"}],"wp:attachment":[{"href":"https:\/\/ultering.com\/it4us\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ultering.com\/it4us\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7153"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ultering.com\/it4us\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}