How to get first key pair element of JSON object without knowing key and deleting it in node red
Asked Answered
E

5

17

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

Enfleurage answered 7/2, 2017 at 18:20 Comment(4)
Possible duplicate of How to remove a property from a JavaScript object?Preengage
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.Marismarisa
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...Enfleurage
Ive tried getting the first element using [0], or doing a for loop to get the first key and val, but both didnt workEnfleurage
U
34
var firstKey = Object.keys(myObject)[0];
delete myObject[firstKey ];
Urchin answered 7/2, 2017 at 18:34 Comment(0)
M
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.

Marismarisa answered 7/2, 2017 at 18:36 Comment(1)
thank you. and yes, i will have to look into that since i do need to ensure that ordering is ensured.Enfleurage
J
1
const defaultvalue =yourObject[Object.keys(yourObject).at(0)]

To get first element from JSON Object

Jakob answered 10/10, 2022 at 14:4 Comment(0)
G
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;
});
Grate answered 7/2, 2017 at 18:44 Comment(0)
H
0
first_key = next(iter(json))
first_value = json[first_key]
Hatty answered 2/6, 2023 at 12:8 Comment(2)
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.Nuncupative
Please read "How to Answer" and "Explaining entirely code-based answers". 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.Loxodrome

© 2022 - 2024 — McMap. All rights reserved.