This question already has answers here:
How to parse JSON using Node.js? [closed]
(31 answers)
Closed 4 years ago.
I want to validate a JSON object without the help of any libraries in nodejs.
Just by parsing it, using JSON.parse function:
function isValidJSON(text){
try{
JSON.parse(text);
return true;
}
catch (error){
return false;
}
}
console.log(isValidJSON("hello")); // false
console.log(isValidJSON("{}")); // true
Related
This question already has answers here:
Is it possible to partially decode and update JSON? (go)
(2 answers)
How to parse a complicated JSON with Go unmarshal?
(3 answers)
golang | unmarshalling arbitrary data
(2 answers)
How to parse JSON in golang without unmarshaling twice
(3 answers)
Decoding generic JSON objects to one of many formats
(1 answer)
Closed 9 months ago.
I have a JSON in the following format:
{
"fieldA": {
"dynamicField": "string value",
"moreDynamicField": ["or", "array", "of", "strings"],
"allFieldsAreDynamic": "values in str or arr[str] only"
},
"fieldB": {
"dynamicField": "sameAsThoseInFieldA",
}
}
The outer fields are fixed, but each outer field's value is a dynamic object. Therefore, I attempted to unmarshall this data into the following Go struct:
type JsonData struct {
fieldA map[string]interface{}
fieldB map[string]interface{}
}
func main() {
var data JsonData
json.Unmarshal([]byte(`{"fieldA": {"x":"x", "y":["x","y"]}, "fieldB": {"a":"b", "c":"d"}}`), &data)
fmt.Println(data)
}
However, the above code produces empty Go array with no error: {map[] map[]}. I also tried fieldA map[string]map[string]any but the result is the same.
I didn't spot useful information on the json package documentation about this nested unmarshalling. Plus, will there be a performance drop using pure map[string]interface{} for the entire JSON data? Some benchmarking blogs and reddit tales gives contradictory conclusions...
This question already has answers here:
How do I use JSTL to output a value from a JSON string?
(1 answer)
Display JSON object using JSP/ JSTL tags in UI? [duplicate]
(1 answer)
How to parse JSON in Java
(36 answers)
Closed 3 years ago.
I know this topic was discussed pretty often and I guess it is not a diffucult thing.
I want to use a JSON Object from my session in a JSP. The Object has the following structur:
{
"addedUsers":[
{
"city":"Los Angeles",
"name":"Doe",
"forname":"John",
"title":"Dr.",
"userId":2
}
],
"allUsers":[
{
"city":"Los Angeles",
"name":"Doe",
"forname":"John",
"title":"Dr.",
"userId":2
},
{
"city":"New York",
"name":"Peter",
"forname":"Parker",
"title":"Dr.",
"userId":3
}
]
}
Now I want to grab the Objects by name for example doing a for each on the "addedUsers" Object and grab the properties. It is important not just to iterate over the whole Object. I have to call them by name.
This question already has answers here:
Golang parse a json with DYNAMIC key [duplicate]
(1 answer)
How to parse/deserialize dynamic JSON
(4 answers)
Marshal dynamic JSON field tags in Go
(1 answer)
How to Unmarshal jSON with dynamic key which can't be captured as a `json` in struct: GOlang [duplicate]
(1 answer)
Closed 4 years ago.
I'm receiving a json object that has a known-static structure inside a key that varies between 10 different values.
Consider lastname can be any in a list of 10 lastnames:
var lastnames = [...]string { "Smith", "Johnson", "Williams", "Brown", "Jones", "Miller", "Davis", "Garcia", "Rodriguez", "Wilson" }
Now, this is how the json looks:
{
(lastname here):
{
"position": value,
"user_email": value
}
}
I tried to unmarshall it using the following structs, but I only get null values:
type Inside struct {
Rol string `json:"position"`
Email string `json:"user_email"`
}
type Outside struct {
Key Inside
}
...
var outside Outside
json.Unmarshal([]byte(body), &outside)
Is it possible to unmarshall this directly without creating 10 different structs? Is there possible workaround?
This question already has answers here:
Safely turning a JSON string into an object
(28 answers)
Closed 5 years ago.
I have this JS object:
{ validator: myValidator }
myValidator is a java JAVASCRIPT function NAME that will be declared somewhere else. I am planning to use it like:
<TableHeaderColumn dataField='status' editable={ { validator: myValidator } }>Job Status</TableHeaderColumn>
where TableHeaderColumn is a react component. So, the question is: What is the JSON string that after using JSON.parse or a similar command I will obtain the { validator: myValidator } object where myValidator is "the name of a function", not a string. This is not clear for me inclusive at the referenced solution.
To convert a JS Object to JSON, you can use json = JSON.stringify(jsObject)
To convert JSON to a JS Object, just use jsObject = JSON.parse(json)
This question already has an answer here:
Getting value from object in javascript
(1 answer)
Closed 6 years ago.
{
"id": 83,
"key": "hello",
"en": "Hello",
"mm": "This is greeting",
"created_at": "2016-12-05T10:14:02.928Z",
"updated_at": "2017-01-31T02:57:11.181Z"
}
I'm trying to get value from mm key in NodeJS. I've tried to get translation["mm"] but it does not work at all. Please help me how to get mm from above data.
My current node version is 5.11
Edited The Answer. Now Its working for above object in question
You can use following function to access the keys of JSON. I have returned 'mm' key specifically.
function jsonParser(stringValue) {
var string = JSON.stringify(stringValue);
var objectValue = JSON.parse(string);
return objectValue['mm'];
}
JSON is an interchange format, meaning it's only a text representing data in a format that many applications can read and translate into it's own language.
Therefore, using node.js you should parse it first (translate it to a javascript object) and store the result in a variable:
var obj = JSON.parse(yourJSONString);
Then, you can ask for any of it properties:
var mm = obj.mm;