user11660793
user11660793

Reputation:

Remove object from JSON whose values are NaN using Python?

My final output JSON file is in following format

    [
     {
    "Type": "UPDATE",
    "resource": {
        "site ": "Lakeside mh041",
        "name": "Total Flow",
        "unit": "CubicMeters",
        "device": "2160 LaserFlow Module",
        "data": [
            {
                "timestamp": [
                    "1087009200"
                ],
                "value": [
                    6945.68
                ]
            },
            {
                "timestamp": [
                    "1087095600"
                ],
                "value": [
                    NaN
                ]
            },
            {
                "timestamp": [
                    "1087182000"
                ],
                "value": [
                    7091.62
                ]
            },

I want to remove the whole object if the "value" is NaN.

Expected Output

     [
      {
    "Type": "UPDATE",
    "resource": {
        "site ": "Lakeside mh041",
        "name": "Total Flow",
        "unit": "CubicMeters",
        "device": "2160 LaserFlow Module",
        "data": [
            {
                "timestamp": [
                    "1087009200"
                ],
                "value": [
                    6945.68
                ]
            },
            {
                "timestamp": [
                    "1087182000"
                ],
                "value": [
                    7091.62
                ]
            },

I cannot remove the blank values from my csv file because of the format of the file.

I have tried this:

  with open('Result.json' , 'r') as j:
     json_dict = json.loads(j.read())
     json_dict['data'] = [item for item in json_dict['data'] if 
        len([val for val in item['value'] if isnan(val)]) == 0]

  print(json_dict)

Error - json_dict['data'] = [item for item in json_dict['data'] if len([val for val in item['value'] if isnan(val)]) == 0] TypeError: list indices must be integers or slices, not str

Upvotes: 0

Views: 3333

Answers (2)

Sharan Arumugam
Sharan Arumugam

Reputation: 363

In case you have more than one value for json"value": [...] then,

import json
from math import isnan

json_str = '''
[
    {
        "Type": "UPDATE",
        "resource": {
            "site ": "Lakeside mh041",
            "name": "Total Flow",
            "unit": "CubicMeters",
            "device": "2160 LaserFlow Module",
            "data": [
                {
                     "timestamp": [
                         "1087009200"
                     ],
                    "value": [
                         6945.68
                     ]
                },
                {
                    "timestamp": [
                        "1087095600"
                    ],
                    "value": [
                        NaN
                    ]
                }
            ]
        }
    }
]
'''

json_dict = json.loads(json_str)

for typeObj in json_dict:
    resource_node = typeObj['resource']
    resource_node['data'] = [
        item for item in resource_node['data']
        if len([val for val in item['value'] if isnan(val)]) == 0
    ]

print(json_dict)

Upvotes: 1

Andrej Kesely
Andrej Kesely

Reputation: 195468

For testing if value is NaN you could use math.isnan() function (doc):

data = '''{"data": [
            {
                "timestamp": [
                    "1058367600"
                ],
                "value": [
                    9.65
                ]
            },
            {
                "timestamp": [
                    "1058368500"
                ],
                "value": [
                    NaN
                ]
            },
            {
                "timestamp": [
                    "1058367600"
                ],
                "value": [
                    4.75
                ]
            }
        ]}'''

import json
from math import isnan

data = json.loads(data)
data['data'] = [i for i in data['data'] if not isnan(i['value'][0])]

print(json.dumps(data, indent=4))

Prints:

{
    "data": [
        {
            "timestamp": [
                "1058367600"
            ],
            "value": [
                9.65
            ]
        },
        {
            "timestamp": [
                "1058367600"
            ],
            "value": [
                4.75
            ]
        }
    ]
}

Upvotes: 1

Related Questions