Home > Back-end >  Specific data from JSON in javascript
Specific data from JSON in javascript

Time:06-10

I have the following JSON data, from which I only need from

{"Date":"2021-01-31........ to ....10.9,"windDir":"SSE"}
{
    "Data": {
        "data": [
            {
                "Date": "2021-01-31T00:00:00.000Z",
                "DewPt": 0.7,
                "HiWind": 86.9,
                "HiWindDir": "SSE",
                "Rain": 53.2,
                "Rain Rate": 28.0,
                "SenTerm": 4.7,
                "absolutotmax": 17.9,
                "absolutotmin": -7.6,
                "hum": 74.9,
                "tmax": 5.1,
                "tmed": 5.0,
                "tmin": 4.8,
                "wind": 11.4,
                "windDir": "SSE"
            },
            {
                "Date": "2021-02-28T00:00:00.000Z",
                "DewPt": 4.6,
                "HiWind": 70.8,
                "HiWindDir": "SE",
                "Rain": 27.8,
                "Rain Rate": 20.2,
                "SenTerm": 9.3,
                "absolutotmax": 21.4,
                "absolutotmin": 2.2,
                "hum": 73.0,
                "tmax": 9.8,
                "tmed": 9.6,
                "tmin": 9.5,
                "wind": 10.3,
                "windDir": "SSE"
            },
            {
                "Date": "2021-10-31T00:00:00.000Z",
                "DewPt": 7.5,
                "HiWind": 59.5,
                "HiWindDir": "SSE",
                "Rain": 26.2,
                "Rain Rate": 111.8,
                "SenTerm": 13.3,
                "absolutotmax": 26.9,
                "absolutotmin": 3.4,
                "hum": 68.0,
                "tmax": 13.9,
                "tmed": 13.7,
                "tmin": 13.5,
                "wind": 7.0,
                "windDir": "SSE"
            },
            {
                "Date": "2021-11-30T00:00:00.000Z",
                "DewPt": 3.4,
                "HiWind": 69.2,
                "HiWindDir": "SSE",
                "Rain": 120.6,
                "Rain Rate": 24.2,
                "SenTerm": 7.1,
                "absolutotmax": 16.8,
                "absolutotmin": 0.5,
                "hum": 77.5,
                "tmax": 7.4,
                "tmed": 7.3,
                "tmin": 7.2,
                "wind": 10.1,
                "windDir": "N"
            },
            {
                "Date": "2021-12-31T00:00:00.000Z",
                "DewPt": 4.2,
                "HiWind": 85.3,
                "HiWindDir": "SSE",
                "Rain": 14.4,
                "Rain Rate": 6.4,
                "SenTerm": 7.6,
                "absolutotmax": 15.8,
                "absolutotmin": -3.1,
                "hum": 78.9,
                "tmax": 7.9,
                "tmed": 7.8,
                "tmin": 7.6,
                "wind": 10.9,
                "windDir": "SSE"
            }
        ],
        "schema": {
            "fields": [
                { "name": "Date", "type": "datetime" },
                { "name": "tmed", "type": "number" },
                { "name": "tmax", "type": "number" },
                { "name": "absolutotmax", "type": "number" },
                { "name": "tmin", "type": "number" },
                { "name": "absolutotmin", "type": "number" },
                { "name": "hum", "type": "number" },
                { "name": "DewPt", "type": "number" },
                { "name": "wind", "type": "number" },
                { "name": "windDir", "type": "string" },
                { "name": "HiWind", "type": "number" },
                { "name": "HiWindDir", "type": "string" },
                { "name": "SenTerm", "type": "number" },
                { "name": "Rain", "type": "number" },
                { "name": "Rain Rate", "type": "number" }
            ],
            "pandas_version": "1.4.0",
            "primaryKey": ["Date"]
        }
    }
}

I have tried this:

var alldata = JSON.parse(jsondata);
var finaldata = [ usedata.data ];

I am basically new to javascript, I have no clue how to select only the data in bold.

Thanks in advance!

CodePudding user response:

First, make sure you are importing the json correctly.

Use console.log(alldata) to view the original data.

Then just select the object you want to be visible:

var finaldata = alldata.Data.data;

CodePudding user response:

const obj = `{
    "Data": {
        "data": [
            {
                "Date": "2021-01-31T00:00:00.000Z",
                "DewPt": 0.7,
                "HiWind": 86.9,
                "HiWindDir": "SSE",
                "Rain": 53.2,
                "Rain Rate": 28.0,
                "SenTerm": 4.7,
                "absolutotmax": 17.9,
                "absolutotmin": -7.6,
                "hum": 74.9,
                "tmax": 5.1,
                "tmed": 5.0,
                "tmin": 4.8,
                "wind": 11.4,
                "windDir": "SSE"
            },
            {
                "Date": "2021-02-28T00:00:00.000Z",
                "DewPt": 4.6,
                "HiWind": 70.8,
                "HiWindDir": "SE",
                "Rain": 27.8,
                "Rain Rate": 20.2,
                "SenTerm": 9.3,
                "absolutotmax": 21.4,
                "absolutotmin": 2.2,
                "hum": 73.0,
                "tmax": 9.8,
                "tmed": 9.6,
                "tmin": 9.5,
                "wind": 10.3,
                "windDir": "SSE"
            },
            {
                "Date": "2021-10-31T00:00:00.000Z",
                "DewPt": 7.5,
                "HiWind": 59.5,
                "HiWindDir": "SSE",
                "Rain": 26.2,
                "Rain Rate": 111.8,
                "SenTerm": 13.3,
                "absolutotmax": 26.9,
                "absolutotmin": 3.4,
                "hum": 68.0,
                "tmax": 13.9,
                "tmed": 13.7,
                "tmin": 13.5,
                "wind": 7.0,
                "windDir": "SSE"
            },
            {
                "Date": "2021-11-30T00:00:00.000Z",
                "DewPt": 3.4,
                "HiWind": 69.2,
                "HiWindDir": "SSE",
                "Rain": 120.6,
                "Rain Rate": 24.2,
                "SenTerm": 7.1,
                "absolutotmax": 16.8,
                "absolutotmin": 0.5,
                "hum": 77.5,
                "tmax": 7.4,
                "tmed": 7.3,
                "tmin": 7.2,
                "wind": 10.1,
                "windDir": "N"
            },
            {
                "Date": "2021-12-31T00:00:00.000Z",
                "DewPt": 4.2,
                "HiWind": 85.3,
                "HiWindDir": "SSE",
                "Rain": 14.4,
                "Rain Rate": 6.4,
                "SenTerm": 7.6,
                "absolutotmax": 15.8,
                "absolutotmin": -3.1,
                "hum": 78.9,
                "tmax": 7.9,
                "tmed": 7.8,
                "tmin": 7.6,
                "wind": 10.9,
                "windDir": "SSE"
            }
        ],
        "schema": {
            "fields": [
                { "name": "Date", "type": "datetime" },
                { "name": "tmed", "type": "number" },
                { "name": "tmax", "type": "number" },
                { "name": "absolutotmax", "type": "number" },
                { "name": "tmin", "type": "number" },
                { "name": "absolutotmin", "type": "number" },
                { "name": "hum", "type": "number" },
                { "name": "DewPt", "type": "number" },
                { "name": "wind", "type": "number" },
                { "name": "windDir", "type": "string" },
                { "name": "HiWind", "type": "number" },
                { "name": "HiWindDir", "type": "string" },
                { "name": "SenTerm", "type": "number" },
                { "name": "Rain", "type": "number" },
                { "name": "Rain Rate", "type": "number" }
            ],
            "pandas_version": "1.4.0",
            "primaryKey": ["Date"]
        }
    }
}`

const parsedObj = JSON.parse(obj);
const dataYouWant = parsedObj.Data.data.map(entry => {
  return {
    Date: entry.Date,
    windDir: entry.windDir
  }
})

console.log(dataYouWant)
  • Related