1

i want to find a value in JSON stringify array

[{"id":"432","temperature":"1","humidity":"1","createat":"0000-00-00 00:00:00"},{"id":"433","temperature":"22.00","humidity":"48","createat":"2015-10-11 19:49:57"},{"id":"434","temperature":"22.40","humidity":"48","createat":"2015-10-11 19:52:02"},{"id":"435","temperature":"22.40","humidity":"48","createat":"2015-10-11 19:55:26"},{"id":"436","temperature":"22.00","humidity":"48","createat":"2015-10-11 19:58:50"},{"id":"437","temperature":"22.00","humidity":"48","createat":"2015-10-11 20:02:14"},{"id":"438","temperature":"22.50","humidity":"51","createat":"2015-10-11 21:23:15"},{"id":"439","temperature":"22.50","humidity":"50","createat":"2015-10-11 21:24:37"},{"id":"440","temperature":"22.50","humidity":"51","createat":"2015-10-11 21:26:17"},{"id":"441","temperature":"22.50","humidity":"51","createat":"2015-10-11 21:26:41"}]

my idea to get this value is to know if what i am adding is not already added

3
  • Iterate and check the properties. Commented Oct 20, 2015 at 23:43
  • why don't you just JSON.parse()? Commented Oct 20, 2015 at 23:45
  • @Guillermo do you want to check the value in javascript ? and add any code you have tried to get the value Commented Oct 20, 2015 at 23:45

3 Answers 3

2
// stringified JSON
stringifiedJson = '[{"id":"432","temperature":"1","humidity":"1","createat":"0000-00-00 00:00:00"},{"id":"433","temperature":"22.00","humidity":"48","createat":"2015-10-11 19:49:57"},{"id":"434","temperature":"22.40","humidity":"48","createat":"2015-10-11 19:52:02"},{"id":"435","temperature":"22.40","humidity":"48","createat":"2015-10-11 19:55:26"},{"id":"436","temperature":"22.00","humidity":"48","createat":"2015-10-11 19:58:50"},{"id":"437","temperature":"22.00","humidity":"48","createat":"2015-10-11 20:02:14"},{"id":"438","temperature":"22.50","humidity":"51","createat":"2015-10-11 21:23:15"},{"id":"439","temperature":"22.50","humidity":"50","createat":"2015-10-11 21:24:37"},{"id":"440","temperature":"22.50","humidity":"51","createat":"2015-10-11 21:26:17"},{"id":"441","temperature":"22.50","humidity":"51","createat":"2015-10-11 21:26:41"}]';

// parse the stringified JSON into a JavaScript object
parsedJson = JSON.parse(stringifiedJson);

// the object in the array you want to check
number = 0

// check if the property exists
if(typeof parsedJson[number].humidity !== 'undefined') {
  // set the property
  parsedJson.humidity = 1;
}

// stringify your object again
stringifiedJson = JSON.stringify(parsedJson);

EDIT: here is a propertyExists function:

var propertyExists = function(stringifiedJson, id, property) {
  // parse the stringified JSON into a JavaScript object
  parsedJson = JSON.parse(stringifiedJson);
  // check if the property exists for a given ID
  for(var i = 0; i < parsedJson.length; i += 1) {
    if(parseInt(parsedJson[i].id) === parseInt(id)) {
      return (typeof parsedJson[i][property] !== 'undefined')
    }
  }
  return false;
}

// stringified json
var stringifiedJson = '[{"id":"432","temperature":"1","humidity":"1","createat":"0000-00-00 00:00:00"},{"id":"433","temperature":"22.00","humidity":"48","createat":"2015-10-11 19:49:57"},{"id":"434","temperature":"22.40","humidity":"48","createat":"2015-10-11 19:52:02"},{"id":"435","temperature":"22.40","humidity":"48","createat":"2015-10-11 19:55:26"},{"id":"436","temperature":"22.00","humidity":"48","createat":"2015-10-11 19:58:50"},{"id":"437","temperature":"22.00","humidity":"48","createat":"2015-10-11 20:02:14"},{"id":"438","temperature":"22.50","humidity":"51","createat":"2015-10-11 21:23:15"},{"id":"439","temperature":"22.50","humidity":"50","createat":"2015-10-11 21:24:37"},{"id":"440","temperature":"22.50","humidity":"51","createat":"2015-10-11 21:26:17"},{"id":"441","temperature":"22.50","humidity":"51","createat":"2015-10-11 21:26:41"}]';

console.log(propertyExists(stringifiedJson, 432, 'humidity'));
Sign up to request clarification or add additional context in comments.

2 Comments

i only want to know if exits a value inside
That's what this answer is telling you. The part that says // check if property exists.
1
....

var jsonArray = [{"id":"432","temperature":"1","humidity":"1",.....

var isPresent = false;
$.each(jsonArray, function(i,v){
    if(jsonArray[i].id == newObject.id){
        isPresent = true;
    }
});
if(!isPresent){
    jsonArray.push(newObject);
}

....

4 Comments

only i want to find a word in array @mike
By 'word', are you talking about the words "id, temperature, humidity...etc"? Because those are properties, which can be found using hasOwnProperty()
with 'word' i am talking about the value indise temperature id or humidity @A.O
There's no jQuery tag or request, there is also the built–in Array.prototype.forEach since ES5. More appropriate is Array.prototype.some. Code–only answers aren't liked, you should explain why your answer fixes the OP's issue.
0

i want to find a value in JSON stringify array

You don't say what value you are trying to find. What you've posted is an Array literal, not JSON. If you want to iterate over the array to determine if it contains an object with a certain property that has a particular value, you can use Array.prototype.some:

function hasPropValue(array, prop, value) {
  return array.some(function(obj) {
    return obj.hasOwnProperty(prop) && obj[prop] === value;
  }
}

some will iterate only over members that exist, and will stop as soon as the callback returns true, otherwise it returns false. If you really do have a string value that is valid JSON, you can do:

hasPropValue(JSON.parse(jsonText), 'id', '432');

var jsonText = '[{"id":"432","temperature":"1","humidity":"1","createat":"0000-00-00 00:00:00"},{"id":"433","temperature":"22.00","humidity":"48","createat":"2015-10-11 19:49:57"},{"id":"434","temperature":"22.40","humidity":"48","createat":"2015-10-11 19:52:02"},{"id":"435","temperature":"22.40","humidity":"48","createat":"2015-10-11 19:55:26"},{"id":"436","temperature":"22.00","humidity":"48","createat":"2015-10-11 19:58:50"},{"id":"437","temperature":"22.00","humidity":"48","createat":"2015-10-11 20:02:14"},{"id":"438","temperature":"22.50","humidity":"51","createat":"2015-10-11 21:23:15"},{"id":"439","temperature":"22.50","humidity":"50","createat":"2015-10-11 21:24:37"},{"id":"440","temperature":"22.50","humidity":"51","createat":"2015-10-11 21:26:17"},{"id":"441","temperature":"22.50","humidity":"51","createat":"2015-10-11 21:26:41"}]'

function hasPropValue(array, prop, value) {
  return array.some(function(obj) {
    return obj.hasOwnProperty(prop) && obj[prop] === value;
  });
}

document.write(hasPropValue(JSON.parse(jsonText), 'id', '432'));

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.