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 3 years ago.
Improve this question
data:
{\"id\":123,\"channel\":\"private_A25BHd\",\"text\":{\"content\":\"{\\"m\\":\\"event\\",\\"p\\":{\\"id\\":123123123,\\"aid\\":125123123,\\"sym\\":\\"BITMEX:XBTUSD\\",\\"res\\":\\"1\\",\\"desc\\":\\"test
message on line1\\ntest message on
line2\\",\\"snd\\":false,\\"snd_file\\":\\"alert\/fired\\",\\"snd_duration\\":0.0,\\"popup\\":true,\\"fire_time\\":123123,\\"bar_time\\":123123,\\"cross_int\\":true}}\",\"channel\":\"alert\"}}
It unfortunately comes in as a string, though I'd like it to change into a dict if possible.
use json library in python
import json
json.loads(your_string)
You can use the function json.loads() which parse the string in to JSON object(Python dictionary) by importing the json library.
Code :
import json
json_string = '{\"id\":123,\"channel\":\"private_A25BHd\",\"text\":{\"content\":\"{\\"m\\":\\"event\\",\\"p\\":{\\"id\\":123123123,\\"aid\\":125123123,\\"sym\\":\\"BITMEX:XBTUSD\\",\\"res\\":\\"1\\",\\"desc\\":\\"test message on line1\\ntest message on line2\\",\\"snd\\":false,\\"snd_file\\":\\"alert\/fired\\",\\"snd_duration\\":0.0,\\"popup\\":true,\\"fire_time\\":123123,\\"bar_time\\":123123,\\"cross_int\\":true}}\",\"channel\":\"alert\"}}'
parsed_string = json.loads(json_string)
print(parsed_string)
Output :
{'text': {'channel': 'alert', 'content': '{"m":"event","p":{"id":123123123,"aid":125123123,"sym":"BITMEX:XBTUSD","res":
"1","desc":"test message on line1\ntest message on line2","snd":false,"snd_file":"alert/fired","snd_duration":0.0,"popu
p":true,"fire_time":123123,"bar_time":123123,"cross_int":true}}'}, 'channel': 'private_A25BHd', 'id': 123}
& if you want to simply print any specific value, you can use
print(parsed_string["id"])
it prints the id value which is 123
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 1 year ago.
Improve this question
For example i have JSON like this
{
"1":"12",
"2":"13",
"3":"14"
}
How can i wrap them like
"data":{
"1":"12",
"2":"13",
"3":"14"
}
Any lib we can do this ?
Thank you a lots
const json = `{
"1": "34.4",
"dog":"Snoopy",
"more": [ 1, 2, 3, 4 ]
}`
const myObj = {
data: JSON.parse(json)
}
console.log(myObj);
Simply that would do it. JSON is simply a string format, representing a javascript object. So within myObj, I can assign that to an object property and parse that JSON string into an object.
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 an API response coming in like this:
[{'name': 'men', 'slug': 'men'}, {'name': 'women', 'slug': 'women'}]
When I stringify it it becomes
const raw = JSON.stringify(resp)
"[{'name': 'men', 'slug': 'men'}, {'name': 'women', 'slug': 'women'}]"
I want it in this format,
'[{"name": "men", "slug": "men"}, {"name": "women", "slug": "women"}]'
I tried with replaceAll, and replace functions, but I am not able to convert it properly, can anyone help me resolve this issue?
I suggest fix the API to return valid JSON.
Other solitions:
Fast but dangerous: use eval() function, ie.:
eval('var data = ' + resp)
const raw = JSON.stringify(data)
Safer but tedious: write own parser of this almost-JSON API response.
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 3 years ago.
Improve this question
I am getting object from back end and in front end I am parsing JSON object but the result I am getting object object.
Here is my Code.
JSON (data contains the following JSON)
{
"sc_sub":"ab",
"sc_sub1":"abc"
}
var lclObj = JSON.parse(data);
var a = lclObj[0].sc_sub;
I made changes to object as array, Now the Problem is I am sending one by one values as array from back end I am getting two arrays as
[{
"sc_sub":"ab",
"sc_sub1":"abc"
}]
[{
"sc_sub":"ab",
"sc_sub1":"abc"
}]
How to remove previous array and set new one?
You can use this code:
var useremail = #Html.Raw(Json.Encode(Data))
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 5 years ago.
Improve this question
I need to set a User Expando field value in a Javascript Function in liferay. It is possible to do?
You can use the json api like this way
Liferay.Service(
'/expandovalue/add-value',
{
companyId: 20154,
className: 'com.liferay.portal.model.User',
tableName: 'CUSTOM_FIELDS',
columnName: 'test',
classPK: 30924,
data: 'test'
},
function(obj) {
console.log(obj);
}
);
where
columnName is the name of your custom field
classPK is the entity id in this case userId
data the value of the custom field
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 8 years ago.
Improve this question
I have this Json file located in different folder than my js file:
{"Title":"test1", "content":"test2"}, {"Title":"test1", "content":"test2"}
I want to read this file and turn the json into a JavaScript array of objects.
I can use jQuery.
Here you go: http://jsfiddle.net/p3p5P/
Use jQuery.getJSON to load JSON-encoded data from the server using a GET HTTP request. Then use jQuery.parseJSON to takes a well-formed JSON string and return the resulting JavaScript object.
//$.getJSON("yourJsonFile.json", function(myJson) {
//console.log(json); // this will show the info in the console
//hard-code the json for this fiddle example, you will load it with the getJSON statement above
var myJson = '[{"Title":"title1","content":"content1"},{"Title":"title2","content":"content2"}]';
var myJsonObj = $.parseJSON(myJson);
console.log(myJsonObj); //you now have an array of objects.
alert(myJsonObj[0].Title); //how to reference the first title
alert(myJsonObj[1].Title); //how to reference the second title
alert(myJsonObj[0].content); //how to reference the first content
alert(myJsonObj[1].content); //how to reference the second content
//});
NOTE: In this example I turned your json data into an array by enclosing the entire string within [ ]. So you will want to edit your json and change this...
{"Title":"title1","content":"content1"},{"Title":"title2","content":"content2"}
into this...
[{"Title":"title1","content":"content1"},{"Title":"title2","content":"content2"}]
Use jQuery.getJSON to get Json from file,
and then use jQuery.parseJSON to transform into a JS array.
$.getJSON( "ajax/test.json", function( data ) {
var jsarray = jQuery.parseJSON(data);
});