1

Is there any way to parse/filter the data present in JSON file in a Javascript file.

Basically, I am calling JSON file into my local javascript. I am having trouble in reading specific data and printing.

Can anyone please help.

JSON file contains:

{
    "Data": [
    {

    "name": "John",
    "age": 30
    },

    {
    "joined on":"Jan 2015",
    "working on": "Automation",
    }
]
}

I am trying to read the above JSON file as:

var jfile = require("./Example.json");

var test = JSON.parse(JSON.stringify(jfile))

console.log(test)

I get the output like this:

{ Data:
   [ { name: 'John', age: 30 },
     { 'joined on': 'Jan 2015', 'working on': 'Automation' } ] }

From the above, I am interested in accessing/filtering out only one i.e. "name". I would like to print only the value "John" to the console.

I have tried to use the ".filter" method to the JSON.parse method but it throws me an error as:

JSON.parse(...).filter is not a function

Is there any way to perform this activity?

2
  • 2
    var test = JSON.parse(JSON.stringify(jfile)) — Why?! Commented Feb 18, 2019 at 10:53
  • @Quentin - I am very new to JSON and still learning, saw at various places to use it this way, Hence I tried to implement the same Commented Feb 18, 2019 at 10:55

5 Answers 5

2

You can access it using . dot notation

var a = {
  "Data": [{

      "name": "John",
      "age": 30
    },

    {
      "joined on": "Jan 2015",
      "working on": "Automation",
    }
  ]
}
console.log(a.Data[0].name)

Sign up to request clarification or add additional context in comments.

1 Comment

I have the values in a separate JSON file and I am trying to access these objects in my javascript file. Does the dot notation still work ?
0

filter is an array method.

JSON.parse(...) will not give you an array. It will give you an object with a Data property. The value of that property is an array.

JSON.parse(...).Data.filter.

You can't just ignore parts of your data structure.

Comments

0

If you have multiple items in your array of different shapes, you can use this

  1. Access the Data key with json.Data
  2. map your array to transform its items into names
  3. apply filter(Boolean) to take out those who are undefined

In your case you'll end up with an array containing only one name John

const getName = json => json.Data.map(x => x.name).filter(Boolean);

const json = {
  "Data": [{

      "name": "John",
      "age": 30
    },

    {
      "joined on": "Jan 2015",
      "working on": "Automation",
    }
  ]
};

console.log(getName(json));

Comments

0

Your JSON's main level is an object (not an array) and only arrays have .filter method.
So filter the array under Data key:

var test = JSON.parse(JSON.stringify(jfile)).Data.filter(/*something*/);

But better if you aren't re-parse JSON:

var test = jfile.Data.filter(/*something*/);

Comments

0

As Quentin mentioned in his comment, What is the use of below statement ?

var test = JSON.parse(JSON.stringify(jfile))

You can directly access the name property from the response.

Try this :

var obj = {
	"Data": [{

			"name": "John",
			"age": 30
		},

		{
			"joined on": "Jan 2015",
			"working on": "Automation"
		}
	]
};

// Solution 1
console.log(obj.Data.map(item => item.name).filter(Boolean));

// Solution 2
console.log(obj.Data.filter(item => item.name).map(elem => elem.name));

2 Comments

Why adding json in a variable? what about a local json file with million of lines, how to load it and apply filter on it ?
In OP, there is a small JSOn that's the only reason i put that in a variable. Its all about the requirement.

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.