Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

156
Vistas
Json String posting in slack without formating

I am trying to post test result on slack. Post message is going but it is posting as whole json string.

Here is my code:

testITArray = ["Test 1", "Test 2", "Test 3r"];
        testITStatusArray = [":white_check_mark:", ":x:", ":x:"];

        var edited = "{";
        for (var i = 0; i < testITArray.length; i++) {
            edited +=
                '"type": "context","elements": [{"type": "mrkdwn","text": "' +
                testITArray[i] +
                '"},{"type": "mrkdwn","text": " ' +
                testITStatusArray[i] +
                ' "}],';
        }
        edited = edited.slice(0, -1);
        edited += "}";

        var asJSON = JSON.stringify(edited);
        axios.post("https://hooks.slack.com/XXXX",
            {
                text: `${asJSON}`,
            }
        );

Tried this option also
         // axios.post("https://hooks.slack.com/XXXX",asJSON,{
         //headers: {
           //  'Content-Type': 'application/json'
           //}
}
            
        );

Here is the output i am getting

See Actual Result

See Expected Result

Where I am doing mistake?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

there are few things wrong with this code

1 you are composing a json string so you don't need to use JSON.stringify

2 you json string contains the same keys over and over so they will be overwritten by the last one

3 it's not clear what the shape of the array should be

4 it's not a best practice to compone a json string in js because it easily can lead to an error

I tried to guess the shape of the json that you need

Please let me know if it is what you were looking for

const yourResult = '{"type": "context","elements": [{"type": "mrkdwn","text": "Test 1"},{"type": "mrkdwn","text": " :white_check_mark: "}],"type": "context","elements": [{"type": "mrkdwn","text": "Test 2"},{"type": "mrkdwn","text": " :x: "}],"type": "context","elements": [{"type": "mrkdwn","text": "Test 3r"},{"type": "mrkdwn","text": " :x: "}]}'


console.log('your string', yourResult)
console.log('parsed result', JSON.parse(yourResult))


const testITArray = ["Test 1", "Test 2", "Test 3r"];
const testITStatusArray = [":white_check_mark:", ":x:", ":x:"];

const resultArray = testITArray.map((t, i) => {
  return {
    type: "context",
    elements: [t, testITStatusArray[i]].map(text => ({
      type: 'mrkdwn',
      text
    }))
  }
})

const resultSingleObject = testITArray.flatMap((t, i) => [t, testITStatusArray[i]])
  .reduce((res, text) => ({
    ...res,
    elements: [...res.elements, {
      type: 'mrkdwn',
      text
    }]
  }), {
    type: 'context',
    elements: []
  })


console.log('array', resultArray)
console.log('array json', JSON.stringify(resultArray))
console.log('object', resultSingleObject)
console.log('object json', JSON.stringify(resultSingleObject))

about 4 years ago · Juan Pablo Isaza Denunciar

0

Thanks to @R4ncid, here is my solution. I have added JSON array within block string.

const json = "{ blocks: " + `${JSON.stringify(resultArray)}` + " }";
const res = axios.post(
        "https://hooks.slack.com/XXX",
        `${json}`
    );
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda