Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have the following string returned from a server:
{"href":"about:blank#&executeFunction<-finishedStroke&&arguments<-{\"base64DataUrl\":\"data:image/png;base64,iVBORw0KGgoAAErkJggg==\"}&"}
I want to extract data:image/png;base64,iVBORw0KGgoAAErkJggg== (the base64DataURL part) from it. What's the best way to do this with Javascript?
Please note that I am asking how to parse values from JSON. This is not the same as this question, which asks how to extract fields from nested objects and arrays.
What I've tried: I have tried
JSON.parse(<string>).href.split('base64DataUrl":"')[1].split('"')[0]
which yields the right answer, but I'm hoping for a more concise solution.
This is JSON, but the part you're interested in isn't exposed as part of the object. So I would suggest that you just consider it a string and use a string-parsing method, like a regular expression.
const string = `{"href":"about:blank#&executeFunction<-finishedStroke&&arguments<-{\"base64DataUrl\":\"data:image/png;base64,iVBORw0KGgoAAErkJggg==\"}&"}`;
const dataURL = /"base64DataUrl":"(.*?(?="))/gm.exec(string)[1];
console.log(dataURL);
Since the server is not returning valid JSON, you will have to parse it with indexOf and substring.
let y = stringFromServer;
let a = y.indexOf('data:');
let b = y.indexOf('\"}',a);
let dataUrl = y.substring(a,b);
Here is my try
let sample = {"href":"about:blank#&executeFunction<-finishedStroke&&arguments<-{\"base64DataUrl\":\"data:image/png;base64,iVBORw0KGgoAAErkJggg==\"}&"}
let splitter = sample.href.split("<-")
console.log(JSON.parse(splitter[2].substring(0, splitter[2].length - 1))["base64DataUrl"]
)
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 days ago.
Improve this question
I have been searching for the correct regex for converting this string to JSON. It works if there is a single object in the array, but I believe the comma is messing it up somehow.
const json = "{\"userId\":44, \"userName\": \"Jim Coleman\"},{\"userId\":33515, \"userName\": \"Grace Mamaradlo\"}";
const obj = JSON.parse(json.replace(/("[^"]*"\s*:\s*)(\d{17,})/g, '$1"$2"'));
console.log(obj);
There was nothing wrong except that the list of objects was not contained within square brackets
const json = "{\"userId\":44, \"userName\": \"Jim Coleman\"},{\"userId\":33515, \"userName\": \"Grace Mamaradlo\"}";
console.log(JSON.parse(`[${json}]`))
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
I have string data aa = {"PC-lab-network-452":[{"version":4,"addr":"10.186.32.137","OS-EXT-IPS:type":"fixed","OS-EXT-IPS-MAC:mac_addr":"fa:16:3e:39:38:ac"}]}
in javaScript and I've to extract the exact IP address --10.186.32.137 from this data
I'm trying this command--
b = aa.match(\10.186.32.137\g) but it also matches the pattern like 10.186.32.13. I need to match the exact pattern. Any help to fix this?
One of the most simple regex pattern would be:
const aa = `{"PC-lab-network-452":[{"version":4,"addr":"10.186.32.137","OS-EXT-IPS:type":"fixed","OS-EXT-IPS-MAC:mac_addr":"fa:16:3e:39:38:ac"}]}`
const reg = new RegExp(/..\....\...\..../g)
const res = aa.match(reg)
console.log(res[0]);
but as posted in comments why not use JSON.parse?
Parse the string with JSON.parse(STRING) then access your network object ("PC-lab-network-452") JSON.parse(aa)["PC-lab-network-452"] then access any valid array index JSON.parse(aa)["PC-lab-network-452"][0] then access the addr property JSON.parse(aa)["PC-lab-network-452"][0].addr
If you want to solve this without regex. Try this :
const a = {"PC-lab-network-452":[{"version":4,"addr":"10.186.32.137","OS-EXT-IPS:type":"fixed","OS-EXT-IPS-MAC:mac_addr":"fa:16:3e:39:38:ac"}]};
Object.keys(a).forEach(item => {
const ipExist = a[item].find(obj => obj.addr === "10.186.32.137");
if (ipExist) {
console.log(ipExist.addr);
}
else {
console.log('IP not found');
}
});
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
This is a little bit of my code. I took out the complex parts. It is all on the script tag.
var trueenemyscoutx = '_2'
var tryeenemyscouty = '2'
var type_22 = "none"
var move1;
enemymove = "type" + trueenemyscoutx + trueenemyscouty;
var move1 = (enemymove.valueOf()).valueOf()
In the post, enemymove is a string constructed by concatenating "type" and two other variables, each of which is also a string.
The resultant string contains no information about the variables used and so the general answer to the question is no, it is not possible using the approach taken.
Obvious alternatives include:
Keep move a string, but use a separator (e.g. colon, comma or space) between component substrings. This allows extracting an array of the components using move.split(separator).
Use an array or simple object to hold move information instead of a string in the first place.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have an url that looks like this:
https://example.com/?category=123&dk=sports&dk=groupcompanyinsider&dk=local&lang=en
Is it possible to return every dk parameter separately? (no matter if there will be 1 or 5 dk parameters) so i would get separately sports, groupcompanyinsider, local.
If its not possible maybe there is a way to return all of them in one string like dk=sports&dk=groupcompanyinsiderlocal&dk=local ?
You can use the built-in javascript class URLSearchParams for this.
You can then transform this into the string you want with string concatenation and a foreach.
const url = "https://example.com/?category=123&dk=sports&dk=groupcompanyinsider&dk=local&lang=en";
var params = new URLSearchParams(url);
var result = "";
// concatenate individual values of the 'dk' query parameter
params.getAll('dk').forEach(function (item) {
result += '&dk=' + item;
});
result = result.substr(1); // remove starting '&' from the result;
console.log(result);
The result should contain your desired string.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
We have a link at the end of which is a lot of JSON. How can we turn the contents of the link into a string, and then render that JSON as objects?
EDIT
We have decided to use other methods, but thank you for your help. We have accepted the answer we found most useful
This is the only answer I can give with the amount of info you provided
document.body.innerHTML = jsonData.toString();
EDIT: alternativly if its a regular JS object you would need to do:
document.body.innerHTML = JSON.stringify(jsonData);
I hope this helps
You could try something like that to show complex JSON to a user, with indent:
var pre = document.createElement("pre");
var textNode = document.createTextNode(JSON.stringify(someObject, null, 4));
pre.appendChild(textNode);
document.body.appendChild(pre);
Possible Duplicate of :Rendering json using jquery.
or try var myJSONObject = JSON.parse(myJSONString);
var jsonString = JSON.stringify(myJsonObject);
Try this way,,
document.body.innerHTML = JSON.stringify(jsondata);
This may help you