1
  • I am trying to import JSON file into Sample variable but only first few characters are displayed from Sample variable.
  • The sample.json is 20,00,000 characters, When i print Sample variable on Console only first 3,756 characters are printed.Is there any limitations on the characters that can be printed through console.log?
  • Complete data persists in Sample variable, I verified it by searching for strings that occur at the end of sample.json file

    var Sample = require('./sample.json');
    export default class proj extends Component {
      constructor(props) {
        super(props);
        this.state = {
          locations: [],
        };
      }
    
      loadOnEvent() {   
          console.log(Sample);
         //this.state={ locations : Sample };
      }
    }
    

Is there any other way to print data in Sample variable.

0

2 Answers 2

2

You have to convert json to string using JSON.stringify before logging.

/* ... */
  loadOnEvent() {   
      console.log(JSON.stringify(Sample));
     //this.state={ locations : Sample };
  }
/* ... */
Sign up to request clarification or add additional context in comments.

1 Comment

Is it possible to be doing with parsing to String
0

Try to use another way to load. Use fetch if file is remote or use fs if file is local.

If it is memory problem supposed by @Shota consider to use server side processing requests to json file. It is good solution to setup microservice which load json file at startup and handle requests to data struct parsed from json file.


Answer for webpack use case:

Configure webpack to use file-loader or copy-webpack-plugin for specifically this file because it enough big. Consider to load it in parallel with webpack bundle. If your application have big parts which need not each case they must be moved to separated bundles.

8 Comments

Shall i install webpack from this URL:npmjs.com/package/react-native-webpack
I confuse tag, I just see for webpack tag and do not check that question have tag react-native. Seems you have another problem. I will remove answer. (try to use another way to load fs or fetch). If fs or fetch helpfull - please leave feedback.
Ok thanks, i tried fetch but not able to load local file using it
Please check updated question, the problem seems to be with console.log @oklas
May be console have some limitations or it is bug. Are you wabt to reveal it. Why you need to output full data? (try to output it partially) For example browser show partially data but its console is interactive and may be expanded. Pulling such big file for most cases is not good idea consider to create microservice.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.