I'm writing this in Python and I want to be able to modify this JSON file
from this,
{
"name": "UVIDOCK",
"date": "03/14/2018",
"cola": "18:18:00",
"colb": "6.70000"
}
to this
window.data = {
"name": "UVIDOCK",
"date": "03/14/2018",
"cola": "18:18:00",
"colb": "6.70000"
}
Appreciate the help in advance.
So do you want a string with your JSON and a variable name? Assuming yes, but keeping in mind that it will not be a valid JSON, the solution will be:
import json
data = { "name": "UVIDOCK", "date": "03/14/2018", "cola": "18:18:00", "colb": "6.70000" }
jsonString = "window.data = " + json.dumps(data)
print (jsonString)
## Output: window.data = {"name": "UVIDOCK", "date": "03/14/2018", "cola": "18:18:00", "colb": "6.70000"}
Related
Hi iam finding the best way to add a string inside the given object.Any help would be appreciated
my String is 'created'
Down Below Is My Data
{
"id": "222",
"list": [
{
"name": "Tony",
}
],
iam trying to insert 'created' in the data like this
{
"id": "222",
"list": [
{
"name": "Tony",
"type":"created"
}
]
The string you've provided looks a lot like JSON data. You can convert a JSON string to an actual javascript object by using the JSON.parse(string) method.
With this object we then can query it's list property - which in your case is an array of objects - and add a new property type to each of the arrays elements. The final step is converting the object back to a JSON string using the JSON.stringify(object) method.
Here's an example:
let str = `{
"id": "222",
"list": [
{
"name": "Tony"
}
]
}`;
let data = JSON.parse(str);
data.list.forEach(element => {
element.type = "created";
});
str = JSON.stringify(data);
console.log(str);
const myObj = {
"id": "222",
"list": [
{
"name": "Tony",
}
],
};
myObj.list[0].type = "created";
This is the way you can do this. But you'd better use secified index of list items;
const index = 0; // Or any other way to get this
myObj.list[index].type = "created";
this is my js code
let content019 = JSON.parse(require("fs").readFileSync("./settings.json"));
// edit or add property
content019.api.client.locations[1] = 999999999999;
//write file
fs.writeFileSync('settings.json', JSON.stringify(content019,null ,2));
i tried using content019.api.client.locations[1] but it changed the inner part to "1": "999....
this is a part of my json file
"1": {
"name": "Germany",
"type": null
},
"2": {
"name": "Singapore",
"type": null
}
},
i want it to only change "1": { to "999999999999": {
even tried content019.api.client.locations.1, didnt work. received error unexpected number
you have to use a string, not a number index. Since your json is invalid, I can only assume that the first part is ok, so you can try
content019.api.client.locations["1"] = 999999999999;
or maybe
content019.api.client.locations["999999"] = content019.api.client.locations["1"];
delete content019.api.client.locations["1"];
Sorry, I'm still new to programming, so please pardon me, I need help adding a new property inside a JSON file. :(
I want it to edit the JSON file~! meow~
animals.json: (Before Adding New Property)
{
"cat": {
"name": "Hiro",
"age": 6
},
"wolf": {
"name": "Kairo",
"age": 3
}
}
index.js (Example Code to add new Property)
var file = require('../../data/animals.json');
file["wolf"].push({gender: "female"})
the new animals.json after running index.js
{
"cat": {
"name": "Hiro",
"age": 6
},
"wolf": {
"name": "Kairo",
"age": 3,
"gender": "female"
}
}
Is this possible? And if so, how? Thank you!
this should work
var file = require('../../data/animals.json');
const fs = require("fs");
file.wolf.gender = "female";
fs.writeFileSync("../../data/animals.json", JSON.stringify(file));
Using dot notation, you can either create or update property on a non null property.
Working with a JSON nested object and I get an error when running in the javascript console "JSON3.html:11 Uncaught SyntaxError: Invalid or unexpected token"
I've verified the JSON via https://jsonformatter.org/json-viewer and that looks ok. The output is only the text in my h2 tag. What am I missing?
Here's the code.
<!DOCTYPE html>
<html>
<body>
<h2>Testing JSON .</h2>
<p id="demo"></p>
<script>
var myJSON = '{
"LevelOne": {
"LevelTwo": {
"location": {
"campus": "SouthWest",
"building": "Biggest",
"floor": "1st",
"room": "101"
},
"quantity": "1",
"section": "ABC",
"task": "abc123456zyx"
}
}
}';
var myObj = JSON.parse(myJSON);
document.getElementById("demo").innerHTML = myJSON.LevelOne.LevelTwo.location;
</script>
</body>
</html>
String concatenation
String that are surrounding using " and ' cannot extend over multiple lines, so have to separate each line and then concatenate them. Example:
var myJSON = '{' +
'"LevelOne": {' +
'"LevelTwo": {' +
'"location": {' +
'"campus": "SouthWest",' +
'"building": "Biggest",' +
'"floor": "1st",' +
'"room": "101"' +
'},' +
'"quantity": "1",' +
'"section": "ABC",' +
'"task": "abc123456zyx"' +
'}' +
'}' +
'}';
console.log(myJSON);
Template Literals
In ES6, template literals were added, that allow to have strings span through multiple lines, provided you use ` instead of " or '. Example:
var myJSON = `{
"LevelOne": {
"LevelTwo": {
"location": {
"campus": "SouthWest",
"building": "Biggest",
"floor": "1st",
"room": "101"
},
"quantity": "1",
"section": "ABC",
"task": "abc123456zyx"
}
}
}`;
console.log(myJSON);
Removes the new lines from the JSON
A simple way to make the JSON 1 line tall is to do JSON.stringify(json) at the browser's console.
Use a normal JSON
You can just use the normal object notation of the JSON instead of the JSON and then if you want to you can convert it back to a string using JSON.stringify. Example:
var myJSON = {
"LevelOne": {
"LevelTwo": {
"location": {
"campus": "SouthWest",
"building": "Biggest",
"floor": "1st",
"room": "101"
},
"quantity": "1",
"section": "ABC",
"task": "abc123456zyx"
}
}
};
console.log(JSON.stringify(myJSON));
console.log(myJSON);
JavaScript Object Notation (JSON) is a standard text-based format for representing structured data based on JavaScript object syntax. It is commonly used for transmitting data in web applications (e.g., sending some data from the server to the client, so it can be displayed on a web page, or vice versa).Including parsing JSON so you can access data within it, and creating JSON
Instead of using JSON data as a string you can directly add the JSON
data or use the double quote.if you are storing the JSON object of object in a variable using single quote, You just > write add the JSON data string in a single line or else use the double quote.
Example
<!DOCTYPE html>
<html>
<body>
<h2>Testing JSON .</h2>
<p id="demo"></p>
<script>
var myJSON = '{"LevelOne": {"LevelTwo": {"location": { "campus": "SouthWest","building": "Biggest","floor": "1st", "room": "101"},"quantity": "1","section": "ABC","task": "abc123456zyx"}}}';
var myObj = JSON.parse(myJSON);
document.getElementById("demo").innerHTML = myObj.LevelOne.LevelTwo.location;
</script>
</body>
</html>
You can get the result.!
Another Solution
myJSON.LevelOne.LevelTwo.location is OBJECT
var myJSON =
{ LevelOne:
{ LevelTwo:
{ location:
{ campus: "SouthWest", building: "Biggest", floor: "1st", room: "101"}
, quantity: "1"
, section: "ABC"
, task: "abc123456zyx"
}
}
}
console.log ( myJSON.LevelOne.LevelTwo.location )
// ===> OBJECT
How can I extract the value between the tags (49% in message) using Javascript?
I will first parse the JSON with JSON.parse.
{
"id": "189180337778229046",
"from": {
"name": "My FB page",
"category": "Automobiles and parts",
"id": "189180337778229046"
},
"subject": "A good deal",
"message": "<p><strong>49%</strong></p><p>This is a good deal for you.</p>",
"icon": "http://static.ak.fbcdn.net/rsrc.php/v1/yY/r/d1gBp2bDGEuh.gif",
"created_time": "2011-11-02T10:49:56+0000",
"updated_time": "2011-11-02T10:49:56+0000"
}
A simple RegExp will do. Assume obj to hold the value of the parsed JSON string:
var percent = obj.message.match(/<strong>(.*?)<\/strong>/i)[1]; //Returns 49%
Easiest will be to use jQuery which looks like you're already using: (as JSON.parse is part of it)
var myValue = $(data.message).find("strong").eq(0).text();
You can do :
var pattern = /([0-9]+%)/gi;
var result = obj.message.match(pattern);
This will only match integer numbers though..