I am receiving JSON data from an endpoint using Ajax.
const jsData = '[ {"x": 1, "y": 1}, {"x": 2, "y": 2}, … ]';
I am converting the data to an immutable object:
const imData = Immutable.fromJSON( jsData )
I want to create a record set base on this immutable object
const Rec = Immutable.Record( imData)
const rec = new Rec();
This error is thrown Uncaught TypeError: Cannot read property 'get' of undefined.
NOTE: Why am I not directly passing the jsData to be the default values of the record set ?
I don't want this to be possible: rec[0].x = 1.
What would be the correct way to convert the received data to a record set that is completely immutable ?
Your JSON data sample is invalid JSON.
You are not posting code sample which produces the error. Message says
Cannot read property 'get' of undefined. while in your samples you don't call get on anything.
Use objects to create Record. https://facebook.github.io/immutable-js/docs/#/Record
A record is similar to a JS object, but enforces a specific set of
allowed string keys, and has default values.
const jsData = '[{"x":1,"y":1},{"x":2,"y":2}]';
const imData = JSON.parse(jsData)
console.log(imData)
const Rec = Immutable.Record( imData[0])
const rec = new Rec();
console.log(rec.get('x'));
https://jsfiddle.net/sh637wsm/
Related
I have no idea about JSON (nor YAML), so I would like to start with a very simple 'identity' operation: formed by a two-pair object, how can I return another JSON that makes the identity of the value of one of the two pairs?
For example, given {"a": a, "b":b}, how can I read that and return a JSON whose content is {"b'":b}? I only think I know how to represent the input JSON (and the output JSON), but I don't know how to relate them so that the input 'b' is also in the output.
I am looking for an "implementation" of this that is something like a function that receives a JSON and returns a JSON. I do not know if this can be done within JSON or we need a programming language and use a toJSON like.
Any help? Also, where can I learn similar very fundamental concepts of JSON?
Here is a JavaScript solution. You can try this out in your browser console.
let json = "{ \"a\": \"a\", \"b\": \"b\" }";
let obj = JSON.parse(json); // { a: 'a', b: 'b' }
let entries = Object.entries(obj); // [ ['a', 'a'], ['b', 'b'] ]
let secondEntry = entries[1]; // ['b', 'b']
let newObj = Object.fromEntries([secondEntry]); // { b: 'b' }
let newJson = JSON.stringify(newObj) // "{ \"b\": \"b\" }"
This should also work in Python:
#given a JSON and a position to copy,
#it (line by line) performs:
#(1) transforms it to a Python dictionary
#(2) takes its key
#(3) takes its value
#(4) creates the new key with '
#(5) creates a dictionary with (key':val)
#(6) translated the new dict to a JSON
#(7) returns the JSON
def take_secondValue(orig_JSON, pos):
to_Dict = json.loads(orig_JSON)
ident_key = list(to_Dict.keys())[pos]
ident_val = list(to_Dict.values())[pos]
new_key = ident_key+"'"
new_dict = {new_key : ident_val}
out_JSON = json.dumps(new_dict)
return out_JSON
I am trying to delete multiple computed properties from an object at once using destructuring. Something like this
const a = {b: 1, c: 2 , d: 3};
const forbiddenKeys = [ "b", "c"]; // pretend this is computed
const { ...forbiddenKeys, ...rest } = a; // gives "Uncaught SyntaxError: Rest element must be last element"
My plan was to use the rest variable after these operations to get the rest of the object that is not contained in forbiddenKeys. Is there a way to do this so that it works like the "Rest in object destructuring" section here? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Computed_object_property_names_and_destructuring
I have trying to get collection of data from json object using map and assign key, pair reference in loop but its not working out for me. I can read value in iterations but I need to assign to key and value where I need help. its not recognising "key" and "value"
var j1 = _preDefineAnswerOptions.map(function(item){
return ["key": item.preDefineAnswerId, "value": item.text];
});
data-source structure
You need to return an object. Use {} around the key and value.
var j1 = _preDefineAnswerOptions.map(function(item){
return {"key": item.preDefineAnswerId, "value": item.text};
});
I am trying to create a json from this String
var json = { data: [v1,v2], con: [begin: "test1", end: "test2"] };
What is wrong with the String? I get an error SyntaxError: Unexpected token :It is not possible to set a key for the value test1 and test2?
In JavaScript:
An object literal, which uses the {} syntax, consists of a collection of property: value pairs.
An array literal, which uses the [] syntax, consists of a collection of values.
[begin: "test1", end: "test2"]
You have used the array syntax here, but have tried to put property: value pairs inside it.
This causes your error, the : isn't expected.
You probably want to use the {} there. Alternatively, you want to remove begin: and end:.
This has nothing to do with JSON or strings. It is simply JavaScript.
You are instantiating a javascript Object.
const toto = {};
is the same as
const toto = new Object();
This is a String javascript object, which hold a string representation of a json.
const toto = "{ \"key\": \"value\" }";
Try
var json = { data: ["v1","v2"], con: [{begin: "test1"}, {end: "test2"}] };
It looks like you might have a blocking error.
I'm using node-orm2 to persist a model to a mysql db column. It works great, except I want the written json pretty printed, so I can easily read it when I view the DB.
Is this possible?
you can use JSON stringify with params:
var obj = {manu: true, gili: "abc"}
var str = JSON.stringify(obj, null, 4); // spacing = 4