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

4
  • 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. Commented Feb 7, 2017 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... Commented Feb 7, 2017 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 Commented Feb 7, 2017 at 18:32
  • You want to delete the item you just stored from where you stored it? Or do you mean you want to delete it from the original JSON object, not the stored location? Commented Jul 29 at 20:40

6 Answers 6

34
var firstKey = Object.keys(myObject)[0];
delete myObject[firstKey ];
Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

thank you. and yes, i will have to look into that since i do need to ensure that ordering is ensured.
1
const defaultvalue =yourObject[Object.keys(yourObject).at(0)]

To get first element from JSON Object

Comments

1
first_key = next(iter(json))
first_value = json[first_key]

2 Comments

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.
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.
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;
});

Comments

-1

This should be the fastest.

var src = json()
for (var stKey in src) break;

var log = `Deleting ${stKey}:${src[stKey]}\n`

delete src[stKey];

console.log(log + JSON.stringify(src, null, 2));

function json() {
  return {
    "30000c690b61": "m8Jp_M7Lc0",
    "30000c290bdc65": "S3qg3Rkl8Y",
    "30000c290bdf1c": "KsLpfVrR4W",
    "30000c290be5d0": "oXasuCWV_q",
    "30000c29e618": "6Q67v-gJkS"
  };
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.