unable to read a data from a external JSON file : Ionic










0















I have configured all the URL's in a JSON file called ionic-config.JSON. The data is like



[

"db_url": "http://localhost:3000/users/"

]


I'm the trying to read the data using the GET method.



url: string = '../ionic-config/ionic-config.json';
this.http.get(this.url).subscribe( (data: any) =>
this.db_url = data;
)


But I'm getting an error as 404 no found




GET http://localhost:8100/ionic-config/ionic-config.json 404 (Not
Found)




The file is in the directory src/ionic-config/ionic-config.json










share|improve this question




























    0















    I have configured all the URL's in a JSON file called ionic-config.JSON. The data is like



    [

    "db_url": "http://localhost:3000/users/"

    ]


    I'm the trying to read the data using the GET method.



    url: string = '../ionic-config/ionic-config.json';
    this.http.get(this.url).subscribe( (data: any) =>
    this.db_url = data;
    )


    But I'm getting an error as 404 no found




    GET http://localhost:8100/ionic-config/ionic-config.json 404 (Not
    Found)




    The file is in the directory src/ionic-config/ionic-config.json










    share|improve this question


























      0












      0








      0








      I have configured all the URL's in a JSON file called ionic-config.JSON. The data is like



      [

      "db_url": "http://localhost:3000/users/"

      ]


      I'm the trying to read the data using the GET method.



      url: string = '../ionic-config/ionic-config.json';
      this.http.get(this.url).subscribe( (data: any) =>
      this.db_url = data;
      )


      But I'm getting an error as 404 no found




      GET http://localhost:8100/ionic-config/ionic-config.json 404 (Not
      Found)




      The file is in the directory src/ionic-config/ionic-config.json










      share|improve this question
















      I have configured all the URL's in a JSON file called ionic-config.JSON. The data is like



      [

      "db_url": "http://localhost:3000/users/"

      ]


      I'm the trying to read the data using the GET method.



      url: string = '../ionic-config/ionic-config.json';
      this.http.get(this.url).subscribe( (data: any) =>
      this.db_url = data;
      )


      But I'm getting an error as 404 no found




      GET http://localhost:8100/ionic-config/ionic-config.json 404 (Not
      Found)




      The file is in the directory src/ionic-config/ionic-config.json







      angular ionic-framework ionic2 ionic3






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 17:33









      georgeawg

      33k104968




      33k104968










      asked Nov 13 '18 at 17:28









      raj_txraj_tx

      297




      297






















          3 Answers
          3






          active

          oldest

          votes


















          0














          Looks like an issue with your path.



          Try this:



          url: string = 'ionic-config/ionic-config.json';


          Also, your logic for assigning the value of db_url is wrong. You are assigning the whole array rather than the field of a specific object in the array. Use this instead:



          this.db_url = data[0].db_url;





          share|improve this answer























          • I've tried it there is no change in the error. I'm calling it from the service file. The directory is as src/services/AU.ts.

            – raj_tx
            Nov 13 '18 at 18:23


















          0














          You dont need to make a http request to the same server. To use a JSON file within your App:



          For example if your JSON File is in /src/pages/YourPage/myJson.json the go to your YourPage.ts(in the same folder as your JSON) and say import * as jsonData from './myJson.json';



          Now you can use it as normal JSON object without to parse it.



          For example: (because your JSON is an Array of Objects):



          console.log(jsonData[0].db_url);





          share|improve this answer























          • I've tried it using the keyword import as import * as dbUrl from '../assets/ionic-config.json'; But I'm getting the error as Cannot find module '../assets/ionic-config.json'. Consider using '--resolveJsonModule' to import module with '.json' extension . I have pasted the json file in the assets folder.

            – raj_tx
            Nov 14 '18 at 13:41












          • I see your error. Try import * as dbUrl from '../../assets/ionic-config.json'

            – Jonathan
            Nov 14 '18 at 14:02











          • Your link referred to the wrong folder, but now it should work

            – Jonathan
            Nov 14 '18 at 14:02











          • No, I use directed to the folder properly. I use vscode it shows dropdowns so I have navigated correctly. I have tried your code also it shows the same error.

            – raj_tx
            Nov 14 '18 at 15:03


















          0














          You can choose any of the following ways.



          1. Using an Api Call



          a. Just move the ionic-config.json file to assets folder for better code flow.Then make the api call like this.



          return this.http.get('assets/ionic-config.json')
          .map(res=> return res.json);


          b. in your .ts file subscribe for the api call like this



          <method_name_in_service_file>.subscribe(resp=>
          this.db_url = resp;
          ,error=>
          console.log(error)
          )


          2. Directly import from the ionic-config file



          like this



          import * as dbUrl from '<path to the config file>'


          you will get the url from dbUrl[0].db_url






          share|improve this answer























          • I've tried it using the keyword import as import * as dbUrl from '../assets/ionic-config.json'; But I'm getting the error as Cannot find module '../assets/ionic-config.json'. Consider using '--resolveJsonModule' to import module with '.json' extension . I have pasted the json file in the assets folder.

            – raj_tx
            Nov 14 '18 at 13:41












          • Just use assets/<filename>

            – Anand Raj
            Nov 15 '18 at 5:19










          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%2f53286533%2funable-to-read-a-data-from-a-external-json-file-ionic%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          3 Answers
          3






          active

          oldest

          votes








          3 Answers
          3






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Looks like an issue with your path.



          Try this:



          url: string = 'ionic-config/ionic-config.json';


          Also, your logic for assigning the value of db_url is wrong. You are assigning the whole array rather than the field of a specific object in the array. Use this instead:



          this.db_url = data[0].db_url;





          share|improve this answer























          • I've tried it there is no change in the error. I'm calling it from the service file. The directory is as src/services/AU.ts.

            – raj_tx
            Nov 13 '18 at 18:23















          0














          Looks like an issue with your path.



          Try this:



          url: string = 'ionic-config/ionic-config.json';


          Also, your logic for assigning the value of db_url is wrong. You are assigning the whole array rather than the field of a specific object in the array. Use this instead:



          this.db_url = data[0].db_url;





          share|improve this answer























          • I've tried it there is no change in the error. I'm calling it from the service file. The directory is as src/services/AU.ts.

            – raj_tx
            Nov 13 '18 at 18:23













          0












          0








          0







          Looks like an issue with your path.



          Try this:



          url: string = 'ionic-config/ionic-config.json';


          Also, your logic for assigning the value of db_url is wrong. You are assigning the whole array rather than the field of a specific object in the array. Use this instead:



          this.db_url = data[0].db_url;





          share|improve this answer













          Looks like an issue with your path.



          Try this:



          url: string = 'ionic-config/ionic-config.json';


          Also, your logic for assigning the value of db_url is wrong. You are assigning the whole array rather than the field of a specific object in the array. Use this instead:



          this.db_url = data[0].db_url;






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 18:05









          Charis The ProgrammerCharis The Programmer

          304212




          304212












          • I've tried it there is no change in the error. I'm calling it from the service file. The directory is as src/services/AU.ts.

            – raj_tx
            Nov 13 '18 at 18:23

















          • I've tried it there is no change in the error. I'm calling it from the service file. The directory is as src/services/AU.ts.

            – raj_tx
            Nov 13 '18 at 18:23
















          I've tried it there is no change in the error. I'm calling it from the service file. The directory is as src/services/AU.ts.

          – raj_tx
          Nov 13 '18 at 18:23





          I've tried it there is no change in the error. I'm calling it from the service file. The directory is as src/services/AU.ts.

          – raj_tx
          Nov 13 '18 at 18:23













          0














          You dont need to make a http request to the same server. To use a JSON file within your App:



          For example if your JSON File is in /src/pages/YourPage/myJson.json the go to your YourPage.ts(in the same folder as your JSON) and say import * as jsonData from './myJson.json';



          Now you can use it as normal JSON object without to parse it.



          For example: (because your JSON is an Array of Objects):



          console.log(jsonData[0].db_url);





          share|improve this answer























          • I've tried it using the keyword import as import * as dbUrl from '../assets/ionic-config.json'; But I'm getting the error as Cannot find module '../assets/ionic-config.json'. Consider using '--resolveJsonModule' to import module with '.json' extension . I have pasted the json file in the assets folder.

            – raj_tx
            Nov 14 '18 at 13:41












          • I see your error. Try import * as dbUrl from '../../assets/ionic-config.json'

            – Jonathan
            Nov 14 '18 at 14:02











          • Your link referred to the wrong folder, but now it should work

            – Jonathan
            Nov 14 '18 at 14:02











          • No, I use directed to the folder properly. I use vscode it shows dropdowns so I have navigated correctly. I have tried your code also it shows the same error.

            – raj_tx
            Nov 14 '18 at 15:03















          0














          You dont need to make a http request to the same server. To use a JSON file within your App:



          For example if your JSON File is in /src/pages/YourPage/myJson.json the go to your YourPage.ts(in the same folder as your JSON) and say import * as jsonData from './myJson.json';



          Now you can use it as normal JSON object without to parse it.



          For example: (because your JSON is an Array of Objects):



          console.log(jsonData[0].db_url);





          share|improve this answer























          • I've tried it using the keyword import as import * as dbUrl from '../assets/ionic-config.json'; But I'm getting the error as Cannot find module '../assets/ionic-config.json'. Consider using '--resolveJsonModule' to import module with '.json' extension . I have pasted the json file in the assets folder.

            – raj_tx
            Nov 14 '18 at 13:41












          • I see your error. Try import * as dbUrl from '../../assets/ionic-config.json'

            – Jonathan
            Nov 14 '18 at 14:02











          • Your link referred to the wrong folder, but now it should work

            – Jonathan
            Nov 14 '18 at 14:02











          • No, I use directed to the folder properly. I use vscode it shows dropdowns so I have navigated correctly. I have tried your code also it shows the same error.

            – raj_tx
            Nov 14 '18 at 15:03













          0












          0








          0







          You dont need to make a http request to the same server. To use a JSON file within your App:



          For example if your JSON File is in /src/pages/YourPage/myJson.json the go to your YourPage.ts(in the same folder as your JSON) and say import * as jsonData from './myJson.json';



          Now you can use it as normal JSON object without to parse it.



          For example: (because your JSON is an Array of Objects):



          console.log(jsonData[0].db_url);





          share|improve this answer













          You dont need to make a http request to the same server. To use a JSON file within your App:



          For example if your JSON File is in /src/pages/YourPage/myJson.json the go to your YourPage.ts(in the same folder as your JSON) and say import * as jsonData from './myJson.json';



          Now you can use it as normal JSON object without to parse it.



          For example: (because your JSON is an Array of Objects):



          console.log(jsonData[0].db_url);






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 '18 at 6:46









          JonathanJonathan

          113




          113












          • I've tried it using the keyword import as import * as dbUrl from '../assets/ionic-config.json'; But I'm getting the error as Cannot find module '../assets/ionic-config.json'. Consider using '--resolveJsonModule' to import module with '.json' extension . I have pasted the json file in the assets folder.

            – raj_tx
            Nov 14 '18 at 13:41












          • I see your error. Try import * as dbUrl from '../../assets/ionic-config.json'

            – Jonathan
            Nov 14 '18 at 14:02











          • Your link referred to the wrong folder, but now it should work

            – Jonathan
            Nov 14 '18 at 14:02











          • No, I use directed to the folder properly. I use vscode it shows dropdowns so I have navigated correctly. I have tried your code also it shows the same error.

            – raj_tx
            Nov 14 '18 at 15:03

















          • I've tried it using the keyword import as import * as dbUrl from '../assets/ionic-config.json'; But I'm getting the error as Cannot find module '../assets/ionic-config.json'. Consider using '--resolveJsonModule' to import module with '.json' extension . I have pasted the json file in the assets folder.

            – raj_tx
            Nov 14 '18 at 13:41












          • I see your error. Try import * as dbUrl from '../../assets/ionic-config.json'

            – Jonathan
            Nov 14 '18 at 14:02











          • Your link referred to the wrong folder, but now it should work

            – Jonathan
            Nov 14 '18 at 14:02











          • No, I use directed to the folder properly. I use vscode it shows dropdowns so I have navigated correctly. I have tried your code also it shows the same error.

            – raj_tx
            Nov 14 '18 at 15:03
















          I've tried it using the keyword import as import * as dbUrl from '../assets/ionic-config.json'; But I'm getting the error as Cannot find module '../assets/ionic-config.json'. Consider using '--resolveJsonModule' to import module with '.json' extension . I have pasted the json file in the assets folder.

          – raj_tx
          Nov 14 '18 at 13:41






          I've tried it using the keyword import as import * as dbUrl from '../assets/ionic-config.json'; But I'm getting the error as Cannot find module '../assets/ionic-config.json'. Consider using '--resolveJsonModule' to import module with '.json' extension . I have pasted the json file in the assets folder.

          – raj_tx
          Nov 14 '18 at 13:41














          I see your error. Try import * as dbUrl from '../../assets/ionic-config.json'

          – Jonathan
          Nov 14 '18 at 14:02





          I see your error. Try import * as dbUrl from '../../assets/ionic-config.json'

          – Jonathan
          Nov 14 '18 at 14:02













          Your link referred to the wrong folder, but now it should work

          – Jonathan
          Nov 14 '18 at 14:02





          Your link referred to the wrong folder, but now it should work

          – Jonathan
          Nov 14 '18 at 14:02













          No, I use directed to the folder properly. I use vscode it shows dropdowns so I have navigated correctly. I have tried your code also it shows the same error.

          – raj_tx
          Nov 14 '18 at 15:03





          No, I use directed to the folder properly. I use vscode it shows dropdowns so I have navigated correctly. I have tried your code also it shows the same error.

          – raj_tx
          Nov 14 '18 at 15:03











          0














          You can choose any of the following ways.



          1. Using an Api Call



          a. Just move the ionic-config.json file to assets folder for better code flow.Then make the api call like this.



          return this.http.get('assets/ionic-config.json')
          .map(res=> return res.json);


          b. in your .ts file subscribe for the api call like this



          <method_name_in_service_file>.subscribe(resp=>
          this.db_url = resp;
          ,error=>
          console.log(error)
          )


          2. Directly import from the ionic-config file



          like this



          import * as dbUrl from '<path to the config file>'


          you will get the url from dbUrl[0].db_url






          share|improve this answer























          • I've tried it using the keyword import as import * as dbUrl from '../assets/ionic-config.json'; But I'm getting the error as Cannot find module '../assets/ionic-config.json'. Consider using '--resolveJsonModule' to import module with '.json' extension . I have pasted the json file in the assets folder.

            – raj_tx
            Nov 14 '18 at 13:41












          • Just use assets/<filename>

            – Anand Raj
            Nov 15 '18 at 5:19















          0














          You can choose any of the following ways.



          1. Using an Api Call



          a. Just move the ionic-config.json file to assets folder for better code flow.Then make the api call like this.



          return this.http.get('assets/ionic-config.json')
          .map(res=> return res.json);


          b. in your .ts file subscribe for the api call like this



          <method_name_in_service_file>.subscribe(resp=>
          this.db_url = resp;
          ,error=>
          console.log(error)
          )


          2. Directly import from the ionic-config file



          like this



          import * as dbUrl from '<path to the config file>'


          you will get the url from dbUrl[0].db_url






          share|improve this answer























          • I've tried it using the keyword import as import * as dbUrl from '../assets/ionic-config.json'; But I'm getting the error as Cannot find module '../assets/ionic-config.json'. Consider using '--resolveJsonModule' to import module with '.json' extension . I have pasted the json file in the assets folder.

            – raj_tx
            Nov 14 '18 at 13:41












          • Just use assets/<filename>

            – Anand Raj
            Nov 15 '18 at 5:19













          0












          0








          0







          You can choose any of the following ways.



          1. Using an Api Call



          a. Just move the ionic-config.json file to assets folder for better code flow.Then make the api call like this.



          return this.http.get('assets/ionic-config.json')
          .map(res=> return res.json);


          b. in your .ts file subscribe for the api call like this



          <method_name_in_service_file>.subscribe(resp=>
          this.db_url = resp;
          ,error=>
          console.log(error)
          )


          2. Directly import from the ionic-config file



          like this



          import * as dbUrl from '<path to the config file>'


          you will get the url from dbUrl[0].db_url






          share|improve this answer













          You can choose any of the following ways.



          1. Using an Api Call



          a. Just move the ionic-config.json file to assets folder for better code flow.Then make the api call like this.



          return this.http.get('assets/ionic-config.json')
          .map(res=> return res.json);


          b. in your .ts file subscribe for the api call like this



          <method_name_in_service_file>.subscribe(resp=>
          this.db_url = resp;
          ,error=>
          console.log(error)
          )


          2. Directly import from the ionic-config file



          like this



          import * as dbUrl from '<path to the config file>'


          you will get the url from dbUrl[0].db_url







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 '18 at 12:27









          Anand RajAnand Raj

          705529




          705529












          • I've tried it using the keyword import as import * as dbUrl from '../assets/ionic-config.json'; But I'm getting the error as Cannot find module '../assets/ionic-config.json'. Consider using '--resolveJsonModule' to import module with '.json' extension . I have pasted the json file in the assets folder.

            – raj_tx
            Nov 14 '18 at 13:41












          • Just use assets/<filename>

            – Anand Raj
            Nov 15 '18 at 5:19

















          • I've tried it using the keyword import as import * as dbUrl from '../assets/ionic-config.json'; But I'm getting the error as Cannot find module '../assets/ionic-config.json'. Consider using '--resolveJsonModule' to import module with '.json' extension . I have pasted the json file in the assets folder.

            – raj_tx
            Nov 14 '18 at 13:41












          • Just use assets/<filename>

            – Anand Raj
            Nov 15 '18 at 5:19
















          I've tried it using the keyword import as import * as dbUrl from '../assets/ionic-config.json'; But I'm getting the error as Cannot find module '../assets/ionic-config.json'. Consider using '--resolveJsonModule' to import module with '.json' extension . I have pasted the json file in the assets folder.

          – raj_tx
          Nov 14 '18 at 13:41






          I've tried it using the keyword import as import * as dbUrl from '../assets/ionic-config.json'; But I'm getting the error as Cannot find module '../assets/ionic-config.json'. Consider using '--resolveJsonModule' to import module with '.json' extension . I have pasted the json file in the assets folder.

          – raj_tx
          Nov 14 '18 at 13:41














          Just use assets/<filename>

          – Anand Raj
          Nov 15 '18 at 5:19





          Just use assets/<filename>

          – Anand Raj
          Nov 15 '18 at 5:19

















          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%2f53286533%2funable-to-read-a-data-from-a-external-json-file-ionic%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







          這個網誌中的熱門文章

          How to read a connectionString WITH PROVIDER in .NET Core?

          In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

          Museum of Modern and Contemporary Art of Trento and Rovereto