How to add new data to existing JSON in Webflow without replacing the entire file for displaying user data? Answered
General question for the peanut gallery. Is it possible to write to json without having to replace the entire json? Like just adding a new key value pair that doesn’t replace what’s already there?
Comments
8 comments
Hello Edward 👋
It looks like you can. Test it out and report back for confirmation 😀
Thanks JS. I’ve read that documentation. It doesn’t deal specifically with adding to JSON non-destructively, but I will run further tests
Myself, ChatGPT 3.5 and ChatGPT 4 ran a dozen or so tests to try and update a single key:value pair in a multi-layered JSON, but no dice. Used your documentation example, below. I imagine it’s possible, but not sure what the correct syntax is. Not being able to target specific categories and subcategories of the JSON file non-destructively would render the JSON file nearly useless.
[ {“username”: “nicolascodes”,
“location”: {
“city”: “NYC”,
“state”: “New York”
},
“skills”: [“graphql”, “react”, “tailwindcss”]
}]
If there is a special syntax for me to change JUST New York to Florida (for example), that syntax should be included in the documentation. I think it would be super helpful.
yeah that get and update memberJSON on the notion page really needs some more information. run a bunch more tests. any new object you add just replaces the entire JSON with that one key:value pair even if there were more.
According to stack overflow it’s kinda complicated, need to GET the entire json, convert it, then you edit the bit you want, then convert it back, then UPDATE. Not for the squeamish.
okay, ive got it to work. every use case requires different code. updating a value, updating a key, adding a new key:value pair, deleting a key:value pair. i will share my scripts with MS to update the notion page if you guys are interested
oh wow thanks for reporting back! Tagging Josh Lopez
We recently added a new advanced doc for this. Please check out this article for more information.
I'd like to use my users' custom JSON to display user data in Weblow.
Is there a way to do this: https://docs.memberstack.com/hc/en-us/articles/9872336561179--Code-Member-Metadata-Attributes with Member's JSON? Any tips?
Hey Felipe Izquierdo 👋
Great question! You can use the DOM Package that is inside the Webflow script to get the JSON data of the logged in member.
window.$memberstackDom.getMemberJSON();
Once you get the JSON you can add to the page using custom code.
Here is a simple example:
<div id="location"></div> // a div with id of location on the page // Define an async function to get member information async function getMemberInfo() { try { // Use Memberstack's function to get the member JSON let memberJSON = await window.$memberstackDom.getMemberJSON(); // Get the location data from the member JSON let locationData = memberJSON.data.location; // Format the location data as a string let locationString = `${locationData.city}, ${locationData.state}`; // Find the div with the ID 'location' in your HTML let locationDiv = document.getElementById('location'); // Set the text content of the div to the location string locationDiv.textContent = locationString; } catch(error) { console.error("An error occurred: ", error); } } // Call the function getMemberInfo();{ data: { username: "josh", location: { city: "Butte", state: "Montana" }, skills: ["graphql", "react", "tailwindcss"], } }Hey Felipe Izquierdo!
This code is adapted slightly from the metadata code. To show something from the member's JSON on an element, add the attribute data-ms-member-json on an element, and for the value, put the key.
For example, if you want to show their company name, you would put:
data-ms-member-json=company-name
If you want to access something nested in an object (i.e. a state inside of a location object), then you would put this:
data-ms-member-json=location.state
Felipe Izquierdo check my comment history for a pdf of more in depth json data manipulation examples
Please sign in to leave a comment.