16

I am writing a function node in node-red that is taking in a JSON object with arbitrary key val pairs:

{ 30000c690b61: "m8Jp_M7Lc0",
  30000c290bdc65: "S3qg3Rkl8Y", 
  30000c290bdf1c: "KsLpfVrR4W", 
  30000c290be5d0: "oXasuCWV_q", 
  30000c29e618: "6Q67v-gJkS" … }

I would like to access the first key pair element in this object, store it, and then delete it. I have tried multiple things, but since it is node-red, it seems to behave different

Dalorzo
  • 19,834
  • 7
  • 55
  • 102
Sharon Soleman
  • 191
  • 1
  • 2
  • 10
  • 1
    Possible duplicate of [How to remove a property from a JavaScript object?](http://stackoverflow.com/questions/208105/how-to-remove-a-property-from-a-javascript-object) – Dalorzo Feb 07 '17 at 18:23
  • 3
    It might be helpful to show one of the things you've tried, and how the behavior differed between node-red and another JS environment. – apsillers Feb 07 '17 at 18:24
  • 1
    it is not a duplicate, since that link you posted answers about a key that is known - I would just like to remove the first element ( I cant just do delete myObject[30000c690b61 ]; since the key always changes... – Sharon Soleman Feb 07 '17 at 18:29
  • Ive tried getting the first element using [0], or doing a for loop to get the first key and val, but both didnt work – Sharon Soleman Feb 07 '17 at 18:32

5 Answers5

33
var firstKey = Object.keys(myObject)[0];
delete myObject[firstKey ];
Vladu Ionut
  • 8,075
  • 1
  • 19
  • 30
15

Getting the "first" element of a JSON object expression is difficult, since JSON objects are not intended to be ordered collections. They are "ordered" in JSON only because they must have a string serialization that is an ordered sequence of characters, but two JSON object expressions with differently-ordered properties are meant to convey identical semantics.

If you are willing to trust your JavaScript environment to preserve the ordering of keys when iterating (which is not an assumption defined in the ECMAScript spec, but may be true in your environment's implementation), you can do:

var myObj = JSON.parse("{ ... }");
var firstKey = Object.keys(myObj)[0];
delete myObj[firstKey];

If you do not want to make such an unsafe assumption, you need to read the JSON string and manually determine the key name between the first set of quotation marks. This involves some effort, because you must also handle escaped quotation marks that may appear within the key name itself.

apsillers
  • 112,806
  • 17
  • 235
  • 239
1
const defaultvalue =yourObject[Object.keys(yourObject).at(0)]

To get first element from JSON Object

Fayiz TP
  • 11
  • 1
0

The Following Code will get First element key pair from the JSON Object and Delete It.

var resultJSON = '{ "30000c690b61": "m8Jp_M7Lc0",
  "30000c290bdc65": "S3qg3Rkl8Y", 
  "30000c290bdf1c": "KsLpfVrR4W", 
  "30000c290be5d0": "oXasuCWV_q", 
  "30000c29e618": "6Q67v-gJkS"}';
var result = $.parseJSON(resultJSON);
$.each(result, function(k, v) {
  //display the key and value pair
  alert(k + ' is ' + v);
  //For Delete the first key pair
   delete result['k'];
  exit;
});
Thilak
  • 19
  • 4
0
first_key = next(iter(json))
first_value = json[first_key]
dna123
  • 1
  • 2
  • 1
    Please avoid code only answer, and add some explanation. Especially when answering to old questions, it is important to explain why your answer is different, and even better than existing answers. – chrslg Jun 05 '23 at 15:52
  • Please read "[answer]" and "[Explaining entirely code-based answers](https://meta.stackoverflow.com/q/392712/128421)". It helps more if you supply an explanation why this is the preferred solution and explain how it works. We want to educate, not just provide code. – the Tin Man Jun 05 '23 at 23:08