Single quotes are added to start and end of this JSON, after returning it to view as here.
I tried removing the first and last character with JavaScript's substring() method to parse it with JSON.parse(), but it couldn't be removed.
What's the correct way to parsing it?
For those who wants the JSON;
"[{\"ParameterType\":\"Parametre Türü1\",\"DisplayType\":\"ComboBox\",\"Parameters\":[{\"Name\":\"Parametre1\",\"Ids\":[\"9786057751744\",\"9786257069335\",\"9786057580399\",\"0\",\"0\",\"0\"]},{\"Name\":\"Parametre2\",\"Ids\":[\"0\",\"0\",\"0\",\"0\",\"9789751969194\",\"0\"]}]},{\"ParameterType\":\"Parametre Türü2\",\"DisplayType\":\"CheckBox\",\"Parameters\":[{\"Name\":\"Param1\",\"Ids\":[\"9786057751744\",\"9786257069335\",\"9786057580399\",\"0\",\"0\",\"0\"]},{\"Name\":\"Param2\",\"Ids\":[\"0\",\"0\",\"0\",\"0\",\"9789751969194\",\"0\"]}]},{\"ParameterType\":\"Parametre Türü3\",\"DisplayType\":\"ComboBox\",\"Parameters\":[{\"Name\":\"1\",\"Ids\":[\"9786057751744\",\"0\",\"0\",\"0\"]},{\"Name\":\"2\",\"Ids\":[\"9786257069335\",\"0\",\"0\",\"9789751969194\"]},{\"Name\":\"3\",\"Ids\":[\"9786057580399\",\"0\",\"0\",\"0\"]}]}]"
JSON encoded data does not need to be an array, object, it could also be a single string:
console.log(JSON.parse("\"test\""))
And that's what you got. It is a single string. That this string by itself also contains JSON encoded data indicates that the data is encoded twice.
If you do JSON.parse(JSON.parse(data)) you will get the object.
But instead of parsing the data twice, you should find the reason why it was encoded twice and fix that.
This snippet shows what happens if you encode the data twice, and it looks like the data you have shown:
let data = [{"ParameterType":"Parametre Türü1"}]
let encodedOnce = JSON.stringify(data)
let encodedTwice = JSON.stringify(encodedOnce)
console.dir(encodedTwice)
let decodedOnce = JSON.parse(encodedTwice)
let decodedTwice = JSON.parse(decodedOnce)
console.dir(decodedTwice)
Maybe try with JSONStringify()
Related
I have a JSON file that was processor generated with lines like this
jsonData: "{data: [350.23,250.32,150.34,340.50,236.70,370.45,380.55]}"
I can target the 'jsonData' object but that returns everything within the double quotes as a string.
I tried ...dataset[0].jsonData[8] which returns the '3' from the first value. I guess I could throw the mixed strings into a JS function and use regex to remove the extra stuff, but thats probably the hackyest way to do this.
Whats the easiest way to target the values only?
If you want to interact with it like the list I would consider something like
var list = jsonData.split("[")[1].split("]")[0].split(",")
Console.log(list);
The console reads:
[
'350.23', '250.32',
'150.34', '340.50',
'236.70', '370.45',
'380.55'
]
From here you can use list[3] to get 340.50
If you don't want to spend the time fixing your JSON just do this:
let values = "{data: [350.23,250.32,150.34,340.50,236.70,370.45,380.55]}".split(',').map(_ => _.replace(/[^0-9.]/g,''))
console.log(values)
I'm trying to save a string which is encoded as json into another json string.
In particular, I have to be able to handle empty objects: "{}".
PHP:
$sVal = "{}";
$jsonString = "{\"Var2\":\"My first var\", \"Var2\":\"My second var\", \"MyBlankObject\":\"{}\"}"
...
Javascript:
var oMyJSON = JSON.parse('< ?= $jsonString;? >');
I get a JSON parse error saying an unexpected { was found.
I can see the code in Chrome's debugger.
The brackets are simply removed and in the client side (javascript ) code, the object is replaced with the word null. That's not valid JSON.
,"Properties":null,
This causes javascript to crash.
If I try to json_encode it on the server side (PHP) I get double quotes on each side of the brackets.
,"Properties":""{}"",
I get the same thing if I just add the double quotes: ""{}""
Of course, this causes javascript to crash too.
Once in the client and I have the JSON object intact, I need to be able to extract the 'string' held in the property: MyBlankObject and then decode that JSON into a seperate JSON object.
Everything I've tried fails. How can I accomplish this?
You can define the object using PHP notation, and let json_encode encode it for you.
$phpArray = [
'Var2' => 'My first var',
'Var2' => 'My second var',
'MyBlankObject' => new \stdClass
];
And then in the JavaScript:
var oMyJSON = JSON.parse('<?= json_encode($phpArray); ?>');
I have the following json string that should be inserted directly into the database in a single column:
const jsString = JSON.stringify({"escaping":"d\"on't"});
const insertion = [{"a":2,"json":jsString}];
const query = pgp.helpers.insert(insertion,["a","json"],"tbl");
however what actually ends up in the database is:
{"escaping":"d"on't"}
removing the escaping \ in d"on't and making the string invalid json. Is there some way to avoid this?
This would be beneficial since it would be nice if my valid json would remain so.
Don't stringify your data into JSON, use it directly
Set column formatting as JSON, using :json modifier
const data = {escaping: "d\"on't"};
const insertion = [{a:2, json:data}];
const query = pgp.helpers.insert(insertion, ["a", "json:json"], "tbl");
But if your data is always an object, then you don't even need to use the :json modifier, it will be automatically formatted as correct JSON.
For more details see the flexibility of the ColumnSet type. You can manipulate your input data in every thinkable way.
This string in js const jsString = JSON.stringify({"escaping":"d\\\"on't"}); will result in this {"escaping":"d\\\"on't"}
While this string in js const jsString = JSON.stringify({"escaping":"don't"}); will result in this {"escaping":"don't"}
I was getting list in array form. So at first place i converted array list to string-
var myJsonString = JSON.stringify(result);
myJsonString="[{"productId":"PI_NAME",
"firstName":null,
"lastName":null,
"customer":null
},
{"productId":"PI_NAME",
"firstName":null,
"lastName":null,
"customer":null
}]"
But again i need to convert myJsonString to Json format, What i need to do? I mean i need to replace 1st" and last ", I guess
You need to call parse now.
JSON.parse(myJsonString)
First, if you ever find yourself building a JSON string by concatenating strings, know that this is probably the wrong approach.
I don't really understand how the first line of your code relates to the second, in that you are not doing anything with JSON-encoded string output from result, but instead just overwriting this on the following line.
So, I am going to limit my answer to show how you could better form JSON from an object/array definition like you have. That might look like this:
// build data structure first
// in this example we are using javascript array and object literal notation.
var objArray = [
{
"productId":"PI_NAME",
"firstName":null,
"lastName":null,
"customer":null
},{
"productId":"PI_NAME",
"firstName":null,
"lastName":null,
"customer":null
}
];
// now that your data structure is built, encoded it to JSON
var JsonString = JSON.stringify(objArray);
Now if you want to work with JSON-encoded data, You just do the opposite:
var newObjArray = JSON.parse(JsonString);
These are really the only two commands you should ever use in javascript when encoding/decoding JSON. You should not try to manually build or modify JSON strings, unless you have a very specific reason to do so.
I have a PHP script - it returns a file like this after POSTing to it with the help of $.post() using jQuery.
13.0519 77.5416
13.028 77.5409
12.9787 77.5724
12.9317 77.6227
How do I go through the lines of data returned in the $.post().done(function(data){}) in Javascript ?
You can split returned text by newline character and then loop over resulting array of lines, probably splitting one more time by space to get array of numbers:
$.post(/*...*/).done(function(data) {
var lines = data.split(/\n/);
lines.forEach(function(line) {
console.log(line.split(' '));
});
});
The best way is to have PHP return JSON (with an array of these).
If not possible, however, then you can get the string and split it on new lines, to achieve a similar effect. Something along the lines of
var arrayOfResults = postData.split("\n");
Then just iterate and do whatever is needed.