#CSS inside JSON

90 messages Β· Page 1 of 1 (latest)

gusty owl
#

Hey guys πŸ‘‹

I've got a JSON file in which one of the values is a HTML, tough its kinda strange and I am not sure what is the best way to deal with it.

I'm attaching the value for your reference:

[{\"group_order\":0,\"group_name\":\"1\",\"option_text\":\"\\u003cdiv style=\\\"color: rgb(0, 0, 0); font-family: Arial, sans-serif; font-size: 14.6667px; font-style: normal; font-weight: 400; opacity: 1; vertical-align: baseline; top: auto; position: static;\\\"\\u003e\\u003cp\\u003e reduced visibility \\u0026gt; Heavy Rain\\u003c/p\\u003e\\u003c/div\\u003e\"},{\"group_order\":1,\"group_name\":\"1\",\"option_text\":\"\\u003cdiv style=\\\"color: rgb(0, 0, 0); font-family: Arial, sans-serif; font-size: 14.6667px; font-style: normal; font-weight: 400; opacity: 1; vertical-align: baseline; top: auto; position: static;\\\"\\u003e\\u003cp\\u003e reduced visibility \\u0026gt; Lighting\\u003c/p\\u003e\\u003c/div\\u003e\"},{\"group_order\":2,\"group_name\":\"1\",\"option_text\":\"\\u003cdiv style=\\\"color: rgb(0, 0, 0); font-family: Arial, sans-serif; font-size: 14.6667px; font-style: normal; font-weight: 400; opacity: 1; vertical-align: baseline; top: auto; position: static;\\\"\\u003e\\u003cp\\u003e reduced visibility \\u0026gt; Thunderstorm\\u003c/p\\u003e\\u003c/div\\u003e\"},{\"group_order\":3,\"group_name\":\"1\",\"option_text\":\"\\u003cdiv style=\\\"color: rgb(0, 0, 0); font-family: Arial, sans-serif; font-size: 14.6667px; font-style: normal; font-weight: 400; opacity: 1; vertical-align: baseline; top: auto; position: static;\\\"\\u003e\\u003cp\\u003e reduced visibility \\u0026gt; Windy \\u003c/p\\u003e\\u003c/div\\u003e\"}]

How would you guys suggest I convert this into this format:

[group order] [text] [text]

for example:
0 reduced visibility Heavy Rain
1 reduced visibility Lighting
2 reduced visibility Thunderstorm
3 reduced visibility Windy

keen island
#

in what environment (browser or node)?

gusty owl
#

I basically wanted to just strip this info out and just rebuild the JSON API

#

I don't usually touch CSS HTML, so I'm like really confused how I can go about dealing with this

keen island
#

wanna answer the question?

gusty owl
#

browser

gusty owl
keen island
#
const data = JSON.parse(yourJSON);

const el = document.createElement('div');
el.innerHTML = data[0].option_text;
const text = el.textContent; // ' reduced visibility > Heavy Rain'

nasty and probably a better way, but it works. loop or reduce as necessary.

gusty owl
#

wow

#

I'm blown, what was seeming like a nightmare to deal with

#

you just solved it in a giffy

#

thank you so much

#

@keen island would you know how can I go about achieving this in python?

#

@keen island apologies for the ping again, but I'm so confused because that code really looks like a mess to me, how can I beautify it for me to have a proper viewing of it to know whats going on

#

I tried some online tools, but they weren't able to comprehend whats going on in it

keen island
#

what is a mess?

#
const el = document.createElement('div');
for (const entry of data) {
    el.innerHTML = entry.option_text;
    const optionText = el.textContent.trim();
    console.log(`${entry.group_order} ${optionText}`);
}
gusty owl
#
[{\"group_order\":0,\"group_name\":\"1\",\"option_text\":\"\\u003cdiv style=\\\"color: rgb(0, 0, 0); font-family: Arial, sans-serif; font-size: 14.6667px; font-style: normal; font-weight: 400; opacity: 1; vertical-align: baseline; top: auto; position: static;\\\"\\u003e\\u003cp\\u003e reduced visibility \\u0026gt; Heavy Rain\\u003c/p\\u003e\\u003c/div\\u003e\"},{\"group_order\":1,\"group_name\":\"1\",\"option_text\":\"\\u003cdiv style=\\\"color: rgb(0, 0, 0); font-family: Arial, sans-serif; font-size: 14.6667px; font-style: normal; font-weight: 400; opacity: 1; vertical-align: baseline; top: auto; position: static;\\\"\\u003e\\u003cp\\u003e reduced visibility \\u0026gt; Lighting\\u003c/p\\u003e\\u003c/div\\u003e\"},{\"group_order\":2,\"group_name\":\"1\",\"option_text\":\"\\u003cdiv style=\\\"color: rgb(0, 0, 0); font-family: Arial, sans-serif; font-size: 14.6667px; font-style: normal; font-weight: 400; opacity: 1; vertical-align: baseline; top: auto; position: static;\\\"\\u003e\\u003cp\\u003e reduced visibility \\u0026gt; Thunderstorm\\u003c/p\\u003e\\u003c/div\\u003e\"},{\"group_order\":3,\"group_name\":\"1\",\"option_text\":\"\\u003cdiv style=\\\"color: rgb(0, 0, 0); font-family: Arial, sans-serif; font-size: 14.6667px; font-style: normal; font-weight: 400; opacity: 1; vertical-align: baseline; top: auto; position: static;\\\"\\u003e\\u003cp\\u003e reduced visibility \\u0026gt; Windy \\u003c/p\\u003e\\u003c/div\\u003e\"}]
#

This

keen island
#

it's JSON

gusty owl
#

So the actual JSON looks like this

{
  "qtext": "<div style=\"color: rgb(0, 0, 0); font-family: Arial, sans-serif; font-size: 14.6667px; font-style: normal; font-weight: 400; opacity: 1; vertical-align: baseline; top: auto; position: static;\"><p> Mist (BR) is defined as being:</p></div>",
  "choices": "[{\\\"group_order\\\":0,\\\"group_name\\\":\\\"1\\\",\\\"option_text\\\":\\\"\\\\u003cdiv style=\\\\\\\"color: rgb(0, 0, 0); font-family: Arial, sans-serif; font-size: 14.6667px; font-style: normal; font-weight: 400; opacity: 1; vertical-align: baseline; top: auto; position: static;\\\\\\\"\\\\u003e\\\\u003cp\\\\u003e reduced visibility \\\\u0026gt; Heavy Rain\\\\u003c/p\\\\u003e\\\\u003c/div\\\\u003e\\\"},{\\\"group_order\\\":1,\\\"group_name\\\":\\\"1\\\",\\\"option_text\\\":\\\"\\\\u003cdiv style=\\\\\\\"color: rgb(0, 0, 0); font-family: Arial, sans-serif; font-size: 14.6667px; font-style: normal; font-weight: 400; opacity: 1; vertical-align: baseline; top: auto; position: static;\\\\\\\"\\\\u003e\\\\u003cp\\\\u003e reduced visibility \\\\u0026gt; Lighting\\\\u003c/p\\\\u003e\\\\u003c/div\\\\u003e\\\"},{\\\"group_order\\\":2,\\\"group_name\\\":\\\"1\\\",\\\"option_text\\\":\\\"\\\\u003cdiv style=\\\\\\\"color: rgb(0, 0, 0); font-family: Arial, sans-serif; font-size: 14.6667px; font-style: normal; font-weight: 400; opacity: 1; vertical-align: baseline; top: auto; position: static;\\\\\\\"\\\\u003e\\\\u003cp\\\\u003e reduced visibility \\\\u0026gt; Thunderstorm\\\\u003c/p\\\\u003e\\\\u003c/div\\\\u003e\\\"},{\\\"group_order\\\":3,\\\"group_name\\\":\\\"1\\\",\\\"option_text\\\":\\\"\\\\u003cdiv style=\\\\\\\"color: rgb(0, 0, 0); font-family: Arial, sans-serif; font-size: 14.6667px; font-style: normal; font-weight: 400; opacity: 1; vertical-align: baseline; top: auto; position: static;\\\\\\\"\\\\u003e\\\\u003cp\\\\u003e reduced visibility \\\\u0026gt; Windy \\\\u003c/p\\\\u003e\\\\u003c/div\\\\u003e\\\"}]",
  "answer": "3"
}
keen island
#

JSON.parse(data.choices)

gusty owl
keen island
#

ya, that JSON is invalid

#

the first one you gave isn't, but this is

gusty owl
#

yeah thats the proper json but inside choices I was trying to extract
0 reduced visibility Heavy Rain
1 reduced visibility Lighting

#

but was really confused, as I shy away from HTML CSS personally

keen island
#

without valid JSON, good luck

#

what API are you using?

gusty owl
#

this is from the api, I checked many times

#

I scrapped it off from a mcq website

keen island
#

scraping is not an API. if this data is your own creation, it's invalid.

gusty owl
#

my end goal is to just scrape the questions and just make a pdf doc for me to quickly study on the go

keen island
#

no idea what mcq is, but the string in choices is not valid JSON.

#

so, good luck parsing it.

gusty owl
#

what do you think it is

#

I don't think its JSON

#

choices is probably smthn the site reads and displays the options

#

\\u003e reduced visibility \\u0026gt; Heavy Rain I am not sure what kind of data is this

keen island
#

unicode

gusty owl
#

I tried to decode this in unicode

#

but it didn't work

#

the option I'm seeing is using regex, but I just wanna keep that as last resort, I'm sure there's a cleaner way to go about this

keen island
#
JSON.parse(data2.choices.replaceAll(/\\(?!\\)/g, '').replaceAll('\\\\', '\\'))
gusty owl
#

Wow Agoknee, you really made it look so easy

#

I've been trying to parse the JSON, but I keep getting Syntax error

#

i've cross referenced many times, I'm not sure why its throwing that error at me

#
const data = 'JSONstring';
const el = document.createElement('div');
for (const choice of JSON.parse(data.choices.replaceAll(/\\(?!\\)/g, '').replaceAll('\\\\', '\\'))) {
    el.innerHTML = choice.option_text;
    const optionText = el.textContent;
    console.log(`${choice.group_order} ${optionText}`);
}
#

When I run that, I get this error

#

@keen island (sorry for the ping πŸ˜… )

keen island
#

data should be the JS object you were pasting above, not JSON

gusty owl
#

Ohh

#

I still get the replaceAll error tough

keen island
#

because you're attempting to access a property of a string...

gusty owl
#

πŸ˜…

#

how did you get it working this

keen island
#

I used an actual JS object, not a JSON string (like I just said)...

#

whatever you pasted above is already parsed...

gusty owl
#

wouldn't this go into the json and look for choices?

gusty owl
keen island
#

once again, what you pasted is a JS object not a JSON string. yes, it will fail to parse because it's already parsed.

#

wherever you got that, already parsed it. stop trying to parse it again.

gusty owl
#

Ohh got ya

#

I keep getting that error tough

keen island
#

because you aren't listening

gusty owl
#

I'm sorry πŸ˜…

keen island
#

show how you are obtaining the data.

gusty owl
keen island
#

...

#

that is not the last set of data you provided

keen island
#

the string you're assigning to data is data.choices

gusty owl
#

yeah

keen island
#

why would you copy that, when you followed-it up saying that wasn't the actual data?

#

SHOW HOW YOU ARE OBTAINING THE DATA. NOT YOUR RANDOM PASTES INTO CONSOLE

gusty owl
keen island
#

...

gusty owl
#

What I have is a simple JSON file stored on my comp

#

not retrieving it from any api, as its dead

keen island
#

I don't give a shit where you are getting it. I asked how you are getting it. I'm not going to keep prying answers out of you, though...

gusty owl
#

ahh πŸ˜… I'm really sorry to trouble you like this, I my self am not appreciating this

#

I don't follow, how am I getting it as in

#

I'm using chrome console