2

I have incoming json objects. What i want to do is to filter the JSON based on its key. For example I have three incoming json objects.

Sending message: {"deviceId":"test","temperature":11,"latitude":50,"longitude":19,"time":1,"id":398}

Sending message: {"deviceId":"temp","temperature":11,"latitude":50,"longitude":19,"time":1,"id":399}

Sending message: {"deviceId":"temp","temperature":11,"latitude":50,"longitude":19,"time":1,"id":400}

Sending message: {"deviceId":"test","temperature":11,"latitude":50,"longitude":19,"time":1,"id":01}

Sending message: {"deviceId":"temp","temperature":11,"latitude":50,"longitude":19,"time":1,"id":402}

I want only those json objects where deviceid is test and not temp. I am trying to filter the JSON. How can i do this?

6
  • is it coming in form of array of objects? if not push those object in ana array and then filter in array using arry filter prototype Commented Aug 11, 2017 at 5:40
  • parse the JSON strings to javascript objects first ... then use the javascript object like any other i.e. check for obj.deviceId == 'test' - in this case, the JSON string was parsed to an object called obj Commented Aug 11, 2017 at 5:41
  • What code have you tried so far? The method of filtering will be different depending on how the json is coming in to the script. Commented Aug 11, 2017 at 5:41
  • @JaromandaX Yes, now after i apply this condition, how do i return or print the whole json object based on that value 'test' ? Commented Aug 11, 2017 at 5:43
  • I don't know, because your code is lacking Commented Aug 11, 2017 at 5:44

4 Answers 4

1

I'll write you an example

say you have list of

let obj = {
    "deviceId":"test",
    "temperature":11,
    "latitude":50,
    "longitude":19,
    "time":1,
    "id":398
}

Then you can selecte deviceId by obj['deviceId']

for (let index in listOfObj){

    let curObj = listOfObj[index];

    if(curObj['deviceId'] === 'test'){
        //Do whatever you want with the object
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

This is a basic example which describes how you can check whether the deviceId is 'test' or 'temp' and based on that you can run a loop on the object keys and apply whatever logic you want to apply on it.

Hope this helps.

document.addEventListener("DOMContentLoaded",function(e){
var obj = {
    "deviceId":"test",
    "temperature":11,
    "latitude":50,
    "longitude":19,
    "time":1,
    "id":398
}
if(obj["deviceId"]==="test"){
Object.keys(obj).forEach(function(e){
  alert(e+" "+obj[e]);
})
}
})

Comments

0

I suggest you to use Lodash library.

var messages = [{msg1}, {msg2}];
var filteredMessages = _.filter(messages, {deviceId: "test"});
// filteredMessages is an Array of messages with deviceId equals to `test`

3 Comments

why use a library when native javascript Array's already have a filter method
it's because Lodash(or Underscore) is very useful to manipulate data objects in javascript, especially for new learners
native javascript is also very useful to manipulate data objects in javascript, ESPECIALLY for new learners
0

Assuming your objects are in an array, you can achieve this using the native Javascript Array#filter method,

var arr = [
    {"deviceId":"test","temperature":11,"latitude":50,"longitude":19,"time":1,"id":398},
    {"deviceId":"temp","temperature":11,"latitude":50,"longitude":19,"time":1,"id":399},
    {"deviceId":"temp","temperature":11,"latitude":50,"longitude":19,"time":1,"id":400},
    {"deviceId":"test","temperature":11,"latitude":50,"longitude":19,"time":1,"id":01},
    {"deviceId":"temp","temperature":11,"latitude":50,"longitude":19,"time":1,"id":402}
];

var results = arr.filter(function(obj) {
    // Return true to keep the element
    return obj.deviceId === "test";
});

console.log(results);

Comments

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.