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 2 years ago.
Improve this question
I have scraped a webpage using BeautifulSoup and requests and following piece of code is a portion from the whole HTML page.
$create(Web.Scheduler, {
"model": '{"apt":[0]}',
"timeZone": "UTC",
"_uniqueId": "94b3535a4bdaaf329cab5b7fde996f14",
"allowEdit": false,
"people":
"[{\"id\": 1, \"name\": \"John\", \"age\": \"30\"},{\"id\": 2, \"name\": \"Jane\", \"age\": \"27\"}]",
"attributes": {},
"groupBy": "name",
});
There is an object with key "people". How can I extract the value of this key using regex i.e.
[{"id": 1, "name": "John", "age": "30"},{"id": 2, "name": "Jane", "age": "27"}]
so that I can parse it as a Python dictionary. I can confirm that string "people" (with quotes) is unique on the whole page.
So far, I've seen some solutions to extract strings enclosed in quotes. But in this case, I have to use a pattern that matches "people": and maybe "attributes" key after it can also be used as a stopping point, I have to extract what's in the middle of these two. Thanks in advance!
You can use re/ast modules to extract the data. For example:
import re
from ast import literal_eval
txt = r'''
$create(Web.Scheduler, {
"model": '{"apt":[0]}',
"timeZone": "UTC",
"_uniqueId": "94b3535a4bdaaf329cab5b7fde996f14",
"allowEdit": false,
"people":
"[{\"id\": 1, \"name\": \"John\", \"age\": \"30\"},{\"id\": 2, \"name\": \"Jane\", \"age\": \"27\"}]",
"attributes": {},
"groupBy": "name",
});
'''
data = re.search(r'("people".*?),\s*\n', txt, flags=re.S).group(1)
data = literal_eval('{' + data + '}')
# print the result:
print(data['people'])
Prints:
[{"id": 1, "name": "John", "age": "30"},{"id": 2, "name": "Jane", "age": "27"}]
Related
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 last month.
Improve this question
I am sorry if this is a newbie question, I am recently started to work with Javascript.
I've got a string that looks like this:
myString = "#101|Maria|Smith|Teacher|26000|Football #102|Albert|Sullivan|Pianist|85000|Swimming"
My desired outcome would be a json like the following:
[
{
"id": "101"
"Name": "Maria",
"LastName": "Smith"
"Job": "Teacher",
"Salary": "26000",
"FavSport": "Football"
},
{
"id": "102"
"Name": "Albert",
"LastName": "Sullivan"
"Job": "Pianist",
"Salary": "85000",
"FavSport": "Swimming"
}
]
I have tried using myString.split('#') but it returns an array that is still separated by vertical bars and that I am not able to separate. Any ideas are highly appreciated
Split by space " " to get first your groups
Then, split by pipe "|" to get an array of values
Then .map() your array to a new Array of objects
const myString = "#101|Maria|Smith|Teacher|26000|Football #102|Albert|Sullivan|Pianist|85000|Swimming";
const groups = myString.split(" ");
const result = groups.map((groupString) => {
const [id, Name, LastName, Job, Salary, FavSport] = groupString.split("|");
return {id: id.replace("#", ""), Name, LastName, Job, Salary, FavSport};
});
console.log(result)
Then, to convert it to a JSON String use:
const json = JSON.stringify(result);
PS: Anyways, I would highly discourage you from using that input string format
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 1 year ago.
Improve this question
Hey please don't roast me. So i have a JSON data like this
data": {
"person": [
{
"id": "xx1",
"name": "John DOe",
},
],
"person": [
{
"id": "xx2",
"name": "John Snow",
},
],
}
For example, I want to store every person data whose name contains "o". How could I do that? Thank you
JSON object ,if have duplicate keys, it will replace the first one with the most bottom one .
In your sample data, there is two "person" keys. Therefore, at the end, your said data will succumb to this.
{"data":{"person":[{"id":"xx2","name":"John Snow"}]}}
Therefore, to clarify your data, IMHO, this is supposed to be like this in the first place.
"data": {
"person": [
{
"id": "xx1",
"name": "John DOe",
},
{
"id": "xx2",
"name": "John Snow",
},
]
}
Then you can treat data.person as an array and use Array.prototype function to filter out your desired details.
The filter method should be
const personWithNameO = data.person.filter((v) => v.name.includes("o") );
console.log(personWithNameO);
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 2 years ago.
Improve this question
I do have json like below
[
[{
"id": "4e181c1b-0a68-425e-9eb7-df36324b6cdb",
"date_actual": {
"value": "2020-10-21T13:15:00"
},
"Date": "20201021",
"name": "abc"
},
{
"id": "4e181c1b-0a68-425e-9eb7-df36324qqq",
"date_actual": {
"value": "2020-1-21T13:15:00"
},
"Date": "2020102",
"name": "xyz"
}
]
]
want to modified like below
[
{
"id": "4e181c1b-0a68-425e-9eb7-df36324b6cdb",
"date_actual": "2020-10-21T13:15:00",
"Date": "20201021",
"name": "abc"
},
{
"id": "4e181c1b-0a68-425e-9eb7-df36324qqq",
"date_actual": "2020-10-21T13:15:00",
"Date": "2020102",
"name": "xyz"
}
]
My service class code is
const rows = await bigqueryClient.query(queryData);
console.log("old json"+JSON.stringify(rows));
return rows;
how to solve this issue using nestjs any way please help me ?
This isn't specific to NodeJS or NestJS, but is regular old JavaScript. As you have an array of values you can use the Array.prototype.map method to map to a new array
return origArray.map((val) => ({ ...val, date_actual: val.date_actual.value }))
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 3 years ago.
Improve this question
I have an object like below:
Parent: { Child1: [ {name:'grandchild1', value:'abc', checked:true}, {name:'grandchild2', value:'pqr', checked:false} ], Child2: [ {name:'grandchild3', value:'abcd', checked:false}, {name:'grandchild4', value:'pqrs', checked:true} ], parent2{...........}.... };
How can I make it nested JSON.
Just like in root: parent1, parent2...
Child: children1, ....( Corresponding to parent)
Grandchildren: based on children
Please guide me how can I make it?
To make a JSON object you just have to follow JSON syntax
In your example it will look like:
{
"parent": {
"child1": [{
"name": "grandchild1",
"value": "abc",
"checked": "true"
}, {
"name": "grandchild2",
"value": "pqr",
"checked": "false"
}]
}
}
Please note that in JSON, string values must be written with double quotes.
If you need to get JSON string from object programmatically, you can write in javascript:
var myJSON = JSON.stringify(obj);
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I want to create a JSON data in this format where I want to give a JSON string in the value element , but it is giving error that there is something wrong in JSON format. What is the mistake I am making
{
"metafield": {
"namespace": "inventory",
"key": "test",
"value": "[{"5":10,"8":10}]",
"value_type": "string"
}
}
You have to escape the " ,here is how you may write it
{
"metafield": {
"namespace": "inventory",
"key": "test",
"value": "[{\"5\":10,\"8\":10}]",
"value_type": "string"
}
}
when you passed in value as array or object in JSON string no need to " like "[{'5':10,'8':10}]" just passed array object value like this [{"5":10,"8":10}]
Completely working this string
{
"metafield":
{
"namespace": "inventory",
"key": "test",
"value": [{"5":10,"8":10}],
"valuetype": "string"
}
}
If you write "[{'5':10,'8':10}]" it consider as a simple string.
you can either use ' or \" for this format like 'string' or \"string\"
I don't know you requirement but might be you are doing something wrong with array, may be you need JSON in below format
{
"metafield": {
"namespace": "inventory",
"key": "test",
"value": [{
"5": 10,
"8": 10
}],
"value_type": "string"
}
}
Use single quotes for the attributes of the objects in your array:
{
"metafield":{
"namespace":"inventory",
"key":"test",
"value":"[{'5':10,'8':10}]",
"value_type":"string"
}
}
You can recheck the validity of your JSON here: https://jsonformatter.curiousconcept.com. This service will give you more details of your ERROR.
You json format is incorrect.
use this:
{
"metafield": {
"namespace": "inventory",
"key": "test",
"value": [{
"5": 10
}, {
"8": 10
}],
"value_type": "string"
}
}
{
"metafield": {
"namespace": "inventory",
"key": "test",
"value": "[{"5":10,"8":10}]",
"value_type": "string"
}
}
As above "value": "[{"5":10,"8":10}]",, If you want to pass "value" key value as array than this way is wrong to make json string. If you want to pass "value" key value as array than that key value as "value": [{"5":10,"8":10}], for this reason you got invalid json sting error.
I hope this will helps.