Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

132
Views
Get payload from String

I have this String:

['TEST1-560', '{"data":[{"price":0.0815,"volume":0.2,"car":"BLUE"}],"isMasterFrame":false}']

I want to get the keys 'TEST1-560' which is always fist and "car" value.

Do you know how I can implement this?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

This is a very, very scuffed code, but it should work for your purpose if you have a string and you want to go through it. This can definitely be shortened and optimized, but assuming you have the same structure it will be fine.:

// Your data
var z = `['TEST1-560', '{"data":[{"price":0.0815,"volume":0.2,"car":"BLUE"}],"isMasterFrame":false}']`;
var testName = z.substring(2).split("'")[0];
var dividedVar = z.split(",");
for (var ind in dividedVar) {
  if (dividedVar[ind].split(":")[0] === '"car"') {
    var car = dividedVar[ind].split(":")[1].split("}")[0].substring(1,dividedVar[ind].split(":")[1].split("}")[0].length-1);
    console.log(car)
  }
}
console.log(testName);

output:

BLUE
TEST1-560

In a real application, you don't need to log the results, you can simply use the variables testName,car. You can also put this in a function if you want to handle many data, e.g.:

function parseData(z) {
    var testName = z.substring(2).split("'")[0];
    var dividedVar = z.split(",");
    for (var ind in dividedVar) {
        if (dividedVar[ind].split(":")[0] === '"car"') {
            var car = dividedVar[ind].split(":")[1].split("}")[0].substring(1, dividedVar[ind].split(":")[1].split("}")[0].length - 1);
        }
    }
    return [testName, car]
}

This will return the variables values in an array you can use

about 4 years ago · Juan Pablo Isaza Report

0

const arr = ['TEST1-560', '{"data":[{"price":0.0815,"volume":0.2,"car":"BLUE"}],"isMasterFrame":false}']

const testValue = arr[0];
const carValue = JSON.parse(arr[1]).data[0].car;

console.log(testValue);

console.log('-----------');
console.log(carValue);

If your structure is always the same, your data can be extracted like above.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!