Parse JSON help

Hi!

I need a little help to parse the following JSON.
I need to get the items inside the “body”, to use them in a set node. My webhook node is getting the following JSON exactly:

[
  {
    "headers": {
      "host": "example.com",
      "x-request-id": "k8d663a223bdd91d0f19caa846805b86",
      "x-real-ip": "127.0.0.1",
      "x-forwarded-for": "127.0.0.1",
      "x-forwarded-host": "example.com",
      "x-forwarded-port": "443",
      "x-forwarded-proto": "https",
      "x-forwarded-scheme": "https",
      "x-scheme": "https",
      "content-length": "2284",
      "user-agent": "Faraday v1.10.2",
      "accept-encoding": "gzip;q=1.0,deflate;q=0.6,identity;q=0.3",
      "accept": "*/*",
      "traceparent": "00-23bd54c49dab55094525e6b2daaa9642-4409c19693c4cfd0-00",
      "tracestate": "2474923@nr=0-0-2474923-341290141-4409c19693c4cfd0-28f7288b05fcd95b-0-0.126801-1673392962875",
      "newrelic": "jnJ2IjpbMCwxXSwiZCI6eyJ0eSI6IkFwcCIsImFjIjoiMjQ3NDkyMyIsImFwIjoiMzQxMjkwMTQxIiwidHgiOiIyOGY3Mjg4YjA1ZmNkOTViIiwidHIiOiIyM2JkNTRjNDlkYWI1NTA5NDUyNWU2YjJkYWFhOTY0MiIsInNhIjpmYWxzZSwicHIiOjAuMTI2ODAxLCJ0aSI6MTY3MzM5Mjk2Mjg3NSwiaWQiOiI0NDA5YzE5NjkzYzRjZmQwIn19",
      "content-type": "application/x-www-form-urlencoded"
    },
    "params": {},
    "query": {},
    "body": {
      "{\"quiz_name\":\"EXAMPLE TITLE - 11/01/2023\",\"quiz_id\":\"123\",\"response_id\":\"77Tz6xX8\",\"first_name\":\"John\",\"last_name\":\"Doe\",\"full_name\":\"Jon Doe\",\"email\":\"[email protected]\",\"emails\":[\"[email protected]\"],\"products\":[{\"name\":\"Some product name / 5 GB / Full Pack\",\"score\":1.091837,\"url\":\"https://example.com/path/something": "5 GB / Full Pack\\u0026attribute_example=ExampleAttribute\",\"price\":\"150.0\",\"price_with_currency\":\"150,00 USD\",\"image_url\":\"https://example.com/images/image.png\",\"position\":0,\"sku\":\"\",\"id\":14457,\"variant_id\":14462,\"description\":\"Product description\"}],\"blocks\":[{\"id\":\"yaToNJ8\",\"type\":\"HeadingBlock\",\"position\":0},{\"id\":\"4RTp729\",\"type\":\"ContentBlock\",\"position\":1},{\"id\":\"qoT7Z4N\",\"type\":\"ProductsBlock\",\"position\":2,\"products\":[{\"name\":\"Some product name / 5 GB / Full Pack\",\"id\":14457,\"url\":\"https://example.com/products/pack?attribute_size=ExampleAttribute\\u0026attribute_example=ExampleAttribute\",\"price\":\"150.0\",\"image_url\":\"https://example.com/images/image.png\",\"variant_id\":14462,\"position\":0,\"score\":1.091837,\"price_with_currency\":\"150,00 USD\"}]}],\"answers\":[{\"question_title\":\"Hello!↵↵\",\"question_id\":\"0MiVNYr\",\"values\":[\"Jon Doe\"]},{\"question_title\":\"Hello, {{slide:0MiVNYr}}\",\"question_id\":\"PjivGA7\",\"values\":[\"[email protected]\"]},{\"question_title\":\"Lets go. \",\"question_id\":\"bJizwe3\",\"choice_label\":\"Yes please \"},{\"question_title\":\"Ok.\",\"question_id\":\"1BiMNRn\",\"choice_label\":\"I'd like the cheapest option\"},{\"question_title\":\"¡Allright!\",\"question_id\":\"ZMiRjeR\",\"choice_label\":\"I want the full pack\"},{\"question_title\":\"¡Excellent!\",\"question_id\":\"8JiL9lb\",\"choice_label\":\"First category\"}],\"permalink\":\"https://example.com/#results-nmHB8aV-77Tz6xX8\",\"permalink_hash\":\"#results-nmHB8aV-77Tz6xX8\",\"created_at\":\"2023-01-10T23:22:41Z\"}"
    }
  }
]

In the SET node, I need to get the data individually (example: quiz_name, first_name, last_name, etc…)

I have already tried with {{JSON.parse($node[“Webhook”].json[“body”])[0].quiz_name}} and so on, but it gives me and [Object null] in the expression.

How should I get the values?

Thank you in advance!

The body data doesn’t look like an array. Try removing the [0] in your expression

I’m still getting [Object: null]

What output do you get from just the JSON.parse of the body?

@pemontto same, [Object: null]

Ok that definitely doesn’t look right. Can you share an example workflow so we can get a better idea?

@pemontto here’s the example workflow

That’s quite an odd object. The stringified JSON is the key of the body object and also partially the value… see this updated workflow that extracts the first object key and first object value as examples. Then combines and parses to get the quiz name.

I’d suggest whatever is making the request should update the format they send it in.

3 Likes

Hey thank you very much @pemontto that’s a very straightforward solution! :muscle:
Unfortunately, we cannot change this horrible response format, as it is handled by a provided external service API :frowning:

For last, could you please give me a hint on how should I get the data in the sub array “products”?
Using the same example as before, I need to set as products.name: “Some product name / 5 GB / Full Pack”

Thank you in advance!

In reply to my own last question, the solution to get the data in the sub array “products", name is:

{{ JSON.parse(Object.keys($node["Mock Data"].json.body)[0] + Object.values($node["Mock Data"].json.body)[0]).products[0].name }}

Hope it helps others!
Thanks again @pemontto :pray:

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.