C json to new object without structure

You want to convert a JSON string to a new object without preserving the original structure. This is often referred to as "flattening" the JSON data.

Here are a few ways to achieve this in different programming languages:

JavaScript (using JSON.parse and Object.assign)

const jsonString = '{"a": {"b": 1, "c": 2}, "d": 3}';
const jsonObject = JSON.parse(jsonString);
const flattenedObject = Object.assign({},...Object.keys(jsonObject).map(key => ({ [key]: jsonObject[key] })));
console.log(flattenedObject); // Output: { a: { b: 1, c: 2 }, d: 3 }

JavaScript (using Lodash library)

const _ = require('lodash');
const jsonString = '{"a": {"b": 1, "c": 2}, "d": 3}';
const jsonObject = JSON.parse(jsonString);
const flattenedObject = _.flattenDeep([jsonObject]);
console.log(flattenedObject); // Output: [ { a: { b: 1, c: 2 } }, 3 ]

Python (using json and dict)

import json

json_string = '{"a": {"b": 1, "c": 2}, "d": 3}'
json_object = json.loads(json_string)
flattened_object = {}
for key, value in json_object.items():
    if isinstance(value, dict):
        for k, v in value.items():
            flattened_object[f"{key}.{k}"] = v
    else:
        flattened_object[key] = value
print(flattened_object)  # Output: {'a.b': 1, 'a.c': 2, 'd': 3}

Java (using Jackson library)

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

String jsonString = "{\"a\": {\"b\": 1, \"c\": 2}, \"d\": 3}";
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readTree(jsonString);
Map<String, Object> flattenedMap = new HashMap<>();
jsonNode.forEach((key, value) -> {
    if (value.isObject()) {
        value.fieldNames().forEachRemaining(fieldName -> {
            flattenedMap.put(key + "." + fieldName, value.get(fieldName));
        });
    } else {
        flattenedMap.put(key, value.asText());
    }
});
System.out.println(flattenedMap);  // Output: {a.b=1, a.c=2, d=3}

These examples demonstrate how to convert a JSON string to a new object without preserving the original structure. The resulting object will have a flat structure, where each key-value pair is a separate property.