Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

204
Views
Read and use JSON data from file in File Directory

I am trying to access some / all the data from the xmltojson.json file. Does anyone know how to assign the data to an variable, using javascript?

const jsonfile = require('jsonfile')
const file = 'xmltojson.json'
jsonfile.readFile(file, function (err, obj) {
  if (err) console.error(err)
  console.dir(obj)
})

enter image description here

about 4 years ago · Santiago Gelvez
2 answers
Answer question

0

Here are tow methods to solve this questions.

  1. NodeJS

1.1 If you use read method in Node and determined to needn't any packages or budle or module in this file, you can use it:

const fileContext = JsonFileName.readFileAsync(file);
const jsonContext = Jason.parse(fileContext)

1.2 use require.

const file = require('fileName.json')

Caution:

  • require can't dynamic change content when json file changed.
  • require just support files' suffix is json, like '*.json'.
  1. If you write this code in webpack, just import

    import './fileName.json'

about 4 years ago · Santiago Gelvez Report

0

You can just import your .json file with

import jsondata from './xmltojson.json'

in your script.js file.

Then, If you want to reach the ItemStartDate data, you can use dot notation with indexes as usual:

const itemstartdate = jsondata.MBS_XML.Data[0].ItemStartDate;
about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!