Tips on extracting specific information from nested JSON objects

I have a packet with the following data and I need to extract a specific part:

"data":"YOeNkAAg1wQAYjm/pg==

Is there a way to achieve this using JavaScript in node-red?

 {
 "payload": "lora/01-01-01-01-01-01-01-01/39-31-37-33-5b-37-67-19/packet_sent
{
\"appeui\":\"01-01-01-01-01-01-01-01\", 
\"codr\":\"4/5\",
\"data\":\"YOeNkAAg1wQAYjm/pg==\",
\"datr\":\"SF7BW125\",
\"deveui\":\"39-31-37-33-5b-37-67-19\",
\"freq\":868.29999999999995,
\"gweui\":\"00-80-00-00-a0-00-24-6d\",
\"id\":0,
\"ipol\":true,
\"mhdr\":\"60e78d900020d704\",
\"mic\":\"6239bfa6\",
\"modu\":\"LORA\",
\"ncrc\":true,
\"opts\":\"\",
\"port\":0,
\"powe\":11,
\"rfch\":0,
\"seqn\":1239,
\"size\":13,
\"tmst\":3491353235,
\"twnd\":1
}",
  "fromip": "127.0.0.1:35068",
  "ip": "127.0.0.1",
  "port": 35068,
  "_msgid": "193b00a8.e6c4ff"
}

Answer №1

var src = {
    "payload": "lora/01-01-01-01-01-01-01-01/39-31-37-33-5b-37-67-19/packet_sent {\"appeui\":\"01-01-01-01-01-01-01-01\",\"codr\":\"4/5\",\"data\":\"YOeNkAAg1wQAYjm/pg==\",\"datr\":\"SF7BW125\",\"deveui\":\"39-31-37-33-5b-37-67-19\",\"freq\":868.29999999999995,\"gweui\":\"00-80-00-00-a0-00-24-6d\",\"id\":0,\"ipol\":true,\"mhdr\":\"60e78d900020d704\",\"mic\":\"6239bfa6\",\"modu\":\"LORA\",\"ncrc\":true,\"opts\":\"\",\"port\":0,\"powe\":11,\"rfch\":0,\"seqn\":1239,\"size\":13,\"tmst\":3491353235,\"twnd\":1}",
    "fromip": "127.0.0.1:35068",
    "ip": "127.0.0.1",
    "port": 35068,
    "_msgid": "193b00a8.e6c4ff"
}
var payload = src.payload;
payload = JSON.parse(payload.substr(payload.indexOf('{')));
console.log(payload.data);
console.log('"data":"' + payload.data + '"');
var finalResult = {};
finalResult.data = payload.data;
console.log(JSON.stringify(finalResult));

After removing the unnecessary part, the JSON structure could look like this:

{
    "payload": {
        "appeui": "01-01-01-01-01-01-01-01",
        "codr": "4/5",
        "data": "YOeNkAAg1wQAYjm/pg==",
        "datr": "SF7BW125",
        "deveui": "39-31-37-33-5b-37-67-19",
        "freq": 868.3,
        "gweui": "00-80-00-00-a0-00-24-6d",
        "id": 0,
        "ipol": true,
        "mhdr": "60e78d900020d704",
        "mic": "6239bfa6",
        "modu": "LORA",
        "ncrc": true,
        "opts": "",
        "port": 0,
        "powe": 11,
        "rfch": 0,
        "seqn": 1239,
        "size": 13,
        "tmst": 3491353235,
        "twnd": 1
    },
    "fromip": "127.0.0.1:35068",
    "ip": "127.0.0.1",
    "port": 35068,
    "_msgid": "193b00a8.e6c4ff"
}

Answer №2

If the result consistently appears as data...==, you can locate the data within the payload between data and == using the following code:

var result = array.payload.substring(
    response.payload.lastIndexOf("data") + -1, 
    response.payload.lastIndexOf("==") + 3
);

var response = {
  "payload": "lora/01-01-01-01-01-01-01-01/39-31-37-33-5b-37-67-19/packet_sent {\"appeui\":\"01-01-01-01-01-01-01-01\",\"codr\":\"4/5\",\"data\":\"YOeNkAAg1wQAYjm/pg==\",\"datr\":\"SF7BW125\",\"deveui\":\"39-31-37-33-5b-37-67-19\",\"freq\":868.29999999999995,\"gweui\":\"00-80-00-00-a0-00-24-6d\",\"id\":0,\"ipol\":true,\"mhdr\":\"60e78d900020d704\",\"mic\":\"6239bfa6\",\"modu\":\"LORA\",\"ncrc\":true,\"opts\":\"\",\"port\":0,\"powe\":11,\"rfch\":0,\"seqn\":1239,\"size\":13,\"tmst\":3491353235,\"twnd\":1}",
  "fromip": "127.0.0.1:35068",
  "ip": "127.0.0.1",
  "port": 35068,
  "_msgid": "193b00a8.e6c4ff"
}

var result = response.payload.substring(
    response.payload.lastIndexOf("data") + -1, 
    response.payload.lastIndexOf("==") + 3
);

console.log(result)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Navigate audio tracks with JavaScript

I'm currently facing an issue with using a button to switch between two audio tracks being played in the browser. The method I have implemented switches to the second track, but it doesn't change the audio that is played afterward - instead, it s ...

Is it possible to merge createStackNavigator with createBottomTabNavigator for enhanced navigation functionality

Current Situation : My app currently has three tabs for navigation (School, Admin, Family); I am now facing a challenge as I want to add additional screens that do not require tabs for navigation. These screens will be accessed using this.props.navigati ...

What is the process for updating the dropdown list value based on the radio button that has been selected?

Is there a way to retrieve the value of a radio button and utilize it in the where clause? <input type="radio" name="radiobtn" value = "Yes" onclick="GetLockIn();" >Yes <input type="radio" name="radiobtn" value = "No" onclick="Ge ...

Loop through two sets of data and perform multiplication on specific attributes when they match

I have a unique item that looks like this Object {SD00002:1,SD00011:3,SD00002:6} The values of the properties are set automatically. Currently, I have an array of similar objects as shown in the image below: https://i.sstatic.net/pSkvf.jpg My goal is to ...

Exploring the concept of JSON within node.js

As I work on creating an Alexa skill using node.js, I'm facing a challenge in defining a JSON element. Specifically, I need to gather all the titles from a news API and include them as variables in my code. Although I have successfully logged the titl ...

What is the best method for conducting comprehensive testing of all projects and libraries within NestJS (nx)?

Our NestJS project has been established with multiple libraries through Nx. We have successfully run tests on individual projects/libraries using the following command: npx nx test lib1 --coverage While this method works well, we are faced with numerous l ...

Deactivate DropDownList within Update Panel with JQuery

Below is the Update Panel that I am working on: <asp:ScriptManager ID="ScriptManager1" runat="server" /> <asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server"> <Triggers> <asp:AsyncPostBackTrigger ControlID="d ...

Clearing cookies in Express.js can be a challenging task

I've tried multiple options, but I can't seem to delete the cookie. Can anyone help me understand this? I have attempted using set maxage, expires, setheaders, but nothing seems to delete the cookie. It's impossible. res.cookie("refres ...

The process of exporting a dataframe to JSON in a particular format

Is there a way to modify the format of a json file exported using a downloadButton in a Shiny App? The current code successfully exports a data frame to json, but I want it to have a different structure. Here's the code that currently works: library(s ...

"Troubleshooting Image Loading Issues in Next.js: A Guide to Reloading Unresponsive

https://i.sstatic.net/FLdrA.jpgHello, I am facing an issue with rendering 300 images from IPFS data. Occasionally, around 3-4 of the images fail to load and result in a 404 error. Even after refreshing the page, these unloaded images remain inaccessible. I ...

The internal style and script specified within the <head> section are not being rendered

Within my Joomla website using the T3 template, I inserted the following "Custom Code" just before the closing </head> tag: <style type="text/stylesheet"> div.t3-sidebar.t3-sidebar-right{ background: #F8F8F8 none repeat scroll 0% 0%; ...

How to hide the header on a specific page using Angular

Currently, I am working on an Angular project and my requirement is to hide the header block specifically on the login page. Despite attempting to hide the header on the login page, it seems that my efforts have been unsuccessful so far. Is there anyone wh ...

Implementing Partial Login and Registration Views using AngularJS in conjunction with MVC5 and ASP.NET Identity

Embarking on the journey of creating a Single Page Application with log-in/register functionality using MVC5, ASP.NET Identity, and Angular feels like diving into a vast ocean of web development technologies. Despite being new to this realm, I delved into ...

How can we stop the constant fluctuation of the last number on a number input field with a maxLength restriction

In my ReactJS code, I have an input field that looks like this: <input type="number" onKeyPress={handleKeyPress} maxLength={3} min="0" max="999" step=".1" onPaste={handlePaste} /> Below are the functions associated w ...

Bits of code and the internet

When it comes to displaying code on the web, there are a few key steps involved: Encoding HTML entities Formatting The most basic workflow would involve: Start with a code snippet like: <html> I'm a full page html snippet <html>. ...

Tips for resolving the issue: "Encountered error loading module script..." in Angular 8 and Electron 5

I have been working on developing an Electron 5 app using Angular 8. Despite following numerous online tutorials, I keep encountering the same error. Initially, I set up a new project and ran ng serve --open, which successfully displayed the default angul ...

The HTML textarea is not updating properly when using jQuery's keypress event

I am facing an issue with my forms on a webpage, each containing an html textarea: <textarea name="Comment" class="inputTextArea">Hello World!</textarea> There is a javascript event handler that automatically submits the fo ...

There was a syntax error found in the JSON input when $.ajax() was utilized, resulting in

I'm currently working on a website that requires implementing a chat feature. The project is running locally, but I've encountered an error: SyntaxError: Unexpected end of JSON input Despite searching online for a solution, nothing seems to w ...

Is it possible to use both the filter $select.search and the limitTo $select.infiniteCurrentLimit on a single ui-select field?

I am facing a challenge with my ui-select field which utilizes infinite-scroll functionality due to having a large number of options. However, it becomes cumbersome to scroll down and find the desired option among so many choices. <ui-select-choices ...

Should I avoid declaring global app variables in Angular?

After browsing several examples online, I have come across a common pattern in AngularJS code. The basic structure involves creating a new module using the angular.module() method and then retrieving it in other files within the same module. var app = ang ...